KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > ImageLoader


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2005 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.awt.*;
26 import java.awt.image.*;
27 import java.util.*;
28
29 /**
30  * Loads an image from a file without using the javax.imageio package. That
31  * package creates some performance problems with JPEG files that contain
32  * color profile information. This class work fine in a headless environment.
33  */

34 public class ImageLoader implements ImageConsumer {
35   boolean completed, notStarted = true;
36   Image imgImage;
37   int[] imgPixels;
38   int width, height;
39
40   /**
41    * Creates an image loader for the given file path.
42    *
43    * @param imageFilePath the image file path
44    */

45   public ImageLoader(String JavaDoc imageFilePath) {
46     imgImage = Toolkit.getDefaultToolkit().getImage(imageFilePath);
47   }
48
49   /**
50    * Checks if the images has loaded.
51    *
52    * @return true if the image has been loaded completely.
53    */

54   public boolean check() {
55     if (notStarted) {
56       imgImage.getSource().startProduction(this);
57       notStarted = false;
58     }
59
60     return completed;
61   }
62
63   /**
64    * @return the width of the image.
65    */

66   public int getWidth() {
67     return width;
68   }
69
70   /**
71    * @return the height of the image.
72    */

73   public int getHeight() {
74     return height;
75   }
76
77   public void setDimensions(int width, int height) {
78     this.width = width;
79     this.height = height;
80     imgPixels = new int[width * height];
81   }
82
83   public void setProperties(Hashtable props) {
84     // empty
85
}
86
87   public void setColorModel(ColorModel model) {
88     // empty
89
}
90
91   public void setHints(int hintflags) {
92     // empty
93
}
94
95   public void setPixels(int x, int y, int w, int h, ColorModel model,
96                         byte pixels[], int off, int scansize) {
97     int palette = 1;
98     int sx;
99
100     if (model instanceof IndexColorModel) {
101       palette = ((IndexColorModel) model).getMapSize();
102     }
103
104     for (int i = 0; i < h; i++) {
105       sx = off;
106
107       for (int j = 0; j < w; j++) {
108         int idx = pixels[sx++];
109
110         while (idx < 0) {
111           idx += palette;
112         }
113
114         imgPixels[(i + y) * width + j + x] = model.getRGB(idx % palette);
115       }
116
117       off += scansize;
118     }
119   }
120
121   public void setPixels(int x, int y, int w, int h, ColorModel model,
122                         int pixels[], int off, int scansize) {
123     int sx;
124
125     for (int i = 0; i < h; i++) {
126       sx = off;
127
128       for (int j = 0; j < w; j++) {
129         imgPixels[(i + y) * width + j + x] = model.getRGB(pixels[sx++]);
130       }
131
132       off += scansize;
133     }
134   }
135
136   public void imageComplete(int status) {
137     completed = true;
138     imgImage.getSource().removeConsumer(this);
139     imgImage.flush();
140     imgImage = null;
141   }
142
143   /**
144    * Returns a new image.
145    *
146    * @return the image.
147    */

148   public Image getImage() {
149     waitForImage();
150
151     return Toolkit.getDefaultToolkit().createImage(
152       new MemoryImageSource(width, height, imgPixels, 0, width)
153     );
154   }
155
156   /**
157    * Returns the image as an array of ARGB pixels.
158    *
159    * @return an array of pixels
160    */

161   public int[] getPixels() {
162     waitForImage();
163     return imgPixels;
164   }
165
166   /**
167    * Waits until the image has been loaded completely.
168    */

169   public void waitForImage() {
170     while (!check()) {
171       try {
172         Thread.sleep(500L);
173       } catch (InterruptedException JavaDoc ex) {}
174     }
175   }
176 }
177
Popular Tags