1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.image;
24  
25  import com.liferay.portal.kernel.image.SpriteProcessor;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ArrayUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.PropertiesUtil;
31  import com.liferay.portal.kernel.util.SortedProperties;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  
36  import java.awt.Point;
37  import java.awt.Transparency;
38  import java.awt.image.ColorModel;
39  import java.awt.image.DataBuffer;
40  import java.awt.image.DataBufferByte;
41  import java.awt.image.IndexColorModel;
42  import java.awt.image.Raster;
43  import java.awt.image.RenderedImage;
44  import java.awt.image.SampleModel;
45  
46  import java.io.File;
47  import java.io.IOException;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  import java.util.Properties;
53  
54  import javax.imageio.ImageIO;
55  
56  import javax.media.jai.JAI;
57  import javax.media.jai.LookupTableJAI;
58  import javax.media.jai.PlanarImage;
59  import javax.media.jai.RasterFactory;
60  import javax.media.jai.RenderedOp;
61  import javax.media.jai.TiledImage;
62  import javax.media.jai.operator.FileLoadDescriptor;
63  import javax.media.jai.operator.LookupDescriptor;
64  import javax.media.jai.operator.MosaicDescriptor;
65  import javax.media.jai.operator.TranslateDescriptor;
66  
67  /**
68   * <a href="SpriteProcessorImpl.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Brian Wing Shun Chan
71   *
72   */
73  public class SpriteProcessorImpl implements SpriteProcessor {
74  
75      static {
76          System.setProperty("com.sun.media.jai.disableMediaLib", "true");
77      }
78  
79      public Properties generate(
80              List<File> images, String spriteFileName,
81              String spritePropertiesFileName, String spritePropertiesRootPath,
82              int maxHeight, int maxWidth, int maxSize)
83          throws IOException {
84  
85          if (images.size() < 1) {
86              return null;
87          }
88  
89          if (spritePropertiesRootPath.endsWith(StringPool.SLASH) ||
90              spritePropertiesRootPath.endsWith(StringPool.BACK_SLASH)) {
91  
92              spritePropertiesRootPath = spritePropertiesRootPath.substring(
93                  0, spritePropertiesRootPath.length() - 1);
94          }
95  
96          File dir = images.get(0).getParentFile();
97  
98          File spritePropertiesFile = new File(
99              dir.toString() + StringPool.SLASH + spritePropertiesFileName);
100 
101         boolean build = false;
102 
103         long lastModified = 0;
104 
105         if (spritePropertiesFile.exists()) {
106             lastModified = spritePropertiesFile.lastModified();
107 
108             for (File image : images) {
109                 if (image.lastModified() > lastModified) {
110                     build = true;
111 
112                     break;
113                 }
114             }
115         }
116         else {
117             build = true;
118         }
119 
120         if (!build) {
121             String spritePropertiesString = FileUtil.read(spritePropertiesFile);
122 
123             if (Validator.isNull(spritePropertiesString)) {
124                 return null;
125             }
126             else {
127                 return PropertiesUtil.load(spritePropertiesString);
128             }
129         }
130 
131         List<RenderedImage> renderedImages = new ArrayList<RenderedImage>();
132 
133         Properties spriteProperties = new SortedProperties();
134 
135         float x = 0;
136         float y = 0;
137 
138         for (File file : images) {
139             if (file.length() > maxSize) {
140                 continue;
141             }
142 
143             try {
144                 RenderedOp renderedOp = FileLoadDescriptor.create(
145                     file.toString(), null, null, null);
146 
147                 RenderedImage renderedImage = convert(renderedOp);
148 
149                 int height = renderedImage.getHeight();
150                 int width = renderedImage.getWidth();
151 
152                 if ((height <= maxHeight) && (width <= maxWidth)) {
153                     renderedImage = TranslateDescriptor.create(
154                         renderedImage, x, y, null, null);
155 
156                     renderedImages.add(renderedImage);
157 
158                     String key = StringUtil.replace(
159                         file.toString(), StringPool.BACK_SLASH,
160                         StringPool.SLASH);
161 
162                     key = key.substring(
163                         spritePropertiesRootPath.toString().length());
164 
165                     String value = (int)y + "," + height + "," + width;
166 
167                     spriteProperties.setProperty(key, value);
168 
169                     y += renderedOp.getHeight();
170                 }
171             }
172             catch (Exception e) {
173                 if (_log.isWarnEnabled()) {
174                     _log.warn("Unable to process " + file);
175                 }
176 
177                 if (_log.isDebugEnabled()) {
178                     _log.debug(e, e);
179                 }
180             }
181         }
182 
183         if (renderedImages.size() <= 1) {
184             renderedImages.clear();
185             spriteProperties.clear();
186         }
187         else {
188             RenderedOp renderedOp = MosaicDescriptor.create(
189                 renderedImages.toArray(
190                     new RenderedImage[renderedImages.size()]),
191                 MosaicDescriptor.MOSAIC_TYPE_OVERLAY, null, null, null, null,
192                 null);
193 
194             File spriteFile = new File(
195                 dir.toString() + StringPool.SLASH + spriteFileName);
196 
197             ImageIO.write(renderedOp, "png", spriteFile);
198 
199             if (lastModified > 0) {
200                 spriteFile.setLastModified(lastModified);
201             }
202         }
203 
204         FileUtil.write(
205             spritePropertiesFile, PropertiesUtil.toString(spriteProperties));
206 
207         if (lastModified > 0) {
208             spritePropertiesFile.setLastModified(lastModified);
209         }
210 
211         return spriteProperties;
212     }
213 
214     protected RenderedImage convert(RenderedOp renderedOp) throws Exception {
215         RenderedImage renderedImage = renderedOp;
216 
217         int height = renderedOp.getHeight();
218         int width = renderedOp.getWidth();
219 
220         SampleModel sampleModel = renderedOp.getSampleModel();
221         ColorModel colorModel = renderedOp.getColorModel();
222 
223         Raster raster = renderedOp.getData();
224 
225         DataBuffer dataBuffer = raster.getDataBuffer();
226 
227         if (colorModel instanceof IndexColorModel) {
228             IndexColorModel indexColorModel = (IndexColorModel)colorModel;
229 
230             int mapSize = indexColorModel.getMapSize();
231 
232             byte[][] data = new byte[4][mapSize];
233 
234             indexColorModel.getReds(data[0]);
235             indexColorModel.getGreens(data[1]);
236             indexColorModel.getBlues(data[2]);
237             indexColorModel.getAlphas(data[3]);
238 
239             LookupTableJAI lookupTableJAI = new LookupTableJAI(data);
240 
241             renderedImage = LookupDescriptor.create(
242                 renderedOp, lookupTableJAI, null);
243         }
244         else if (sampleModel.getNumBands() == 2) {
245             List<Byte> bytesList = new ArrayList<Byte>(
246                 height * width * _NUM_OF_BANDS);
247 
248             List<Byte> tempBytesList = new ArrayList<Byte>(_NUM_OF_BANDS);
249 
250             for (int i = 0; i < dataBuffer.getSize(); i++) {
251                 int mod = (i + 1) % 2;
252 
253                 int elemPos = i;
254 
255                 if (mod == 0) {
256                     tempBytesList.add((byte)dataBuffer.getElem(elemPos - 1));
257                     tempBytesList.add((byte)dataBuffer.getElem(elemPos - 1));
258                 }
259 
260                 tempBytesList.add((byte)dataBuffer.getElem(elemPos));
261 
262                 if (mod == 0) {
263                     Collections.reverse(tempBytesList);
264 
265                     bytesList.addAll(tempBytesList);
266 
267                     tempBytesList.clear();
268                 }
269             }
270 
271             byte[] data = ArrayUtil.toArray(
272                 bytesList.toArray(new Byte[bytesList.size()]));
273 
274             DataBuffer newDataBuffer = new DataBufferByte(data, data.length);
275 
276             renderedImage = createRenderedImage(
277                 renderedOp, height, width, newDataBuffer);
278         }
279         else if (colorModel.getTransparency() != Transparency.TRANSLUCENT) {
280             List<Byte> bytesList = new ArrayList<Byte>(
281                 height * width * _NUM_OF_BANDS);
282 
283             List<Byte> tempBytesList = new ArrayList<Byte>(_NUM_OF_BANDS);
284 
285             for (int i = 0; i < dataBuffer.getSize(); i++) {
286                 int mod = (i + 1) % 3;
287 
288                 int elemPos = i;
289 
290                 tempBytesList.add((byte)dataBuffer.getElem(elemPos));
291 
292                 if (mod == 0) {
293                     tempBytesList.add((byte)255);
294 
295                     Collections.reverse(tempBytesList);
296 
297                     bytesList.addAll(tempBytesList);
298 
299                     tempBytesList.clear();
300                 }
301             }
302 
303             byte[] data = ArrayUtil.toArray(
304                 bytesList.toArray(new Byte[bytesList.size()]));
305 
306             DataBuffer newDataBuffer = new DataBufferByte(data, data.length);
307 
308             renderedImage = createRenderedImage(
309                 renderedOp, height, width, newDataBuffer);
310         }
311 
312         return renderedImage;
313     }
314 
315     protected RenderedImage createRenderedImage(
316         RenderedOp renderedOp, int height, int width, DataBuffer dataBuffer) {
317 
318         SampleModel sampleModel =
319             RasterFactory.createPixelInterleavedSampleModel(
320                 DataBuffer.TYPE_BYTE, width, height, _NUM_OF_BANDS);
321         ColorModel colorModel = PlanarImage.createColorModel(sampleModel);
322 
323         TiledImage tiledImage = new TiledImage(
324             0, 0, width, height, 0, 0, sampleModel, colorModel);
325 
326         Raster raster = RasterFactory.createWritableRaster(
327             sampleModel, dataBuffer, new Point(0, 0));
328 
329         tiledImage.setData(raster);
330 
331         if (false) {
332             JAI.create("filestore", tiledImage, "test.png", "PNG");
333 
334             printImage(renderedOp);
335             printImage(tiledImage);
336         }
337 
338         return tiledImage;
339     }
340 
341     protected void printImage(PlanarImage planarImage) {
342         SampleModel sampleModel = planarImage.getSampleModel();
343 
344         int height = planarImage.getHeight();
345         int width = planarImage.getWidth();
346         int numOfBands = sampleModel.getNumBands();
347 
348         int[] pixels = new int[height * width * numOfBands];
349 
350         Raster raster = planarImage.getData();
351 
352         raster.getPixels(0, 0, width, height, pixels);
353 
354         int offset = 0;
355 
356         for (int h = 0; h < height; h++) {
357              for (int w = 0; w < width; w++) {
358                 offset = (h * width * numOfBands) + (w * numOfBands);
359 
360                 System.out.print("[" + w + ", " + h + "] = ");
361 
362                 for (int b = 0; b < numOfBands; b++) {
363                     System.out.print(pixels[offset + b] + " ");
364                 }
365             }
366 
367             System.out.println();
368         }
369     }
370 
371     private static final int _NUM_OF_BANDS = 4;
372 
373     private static Log _log = LogFactoryUtil.getLog(SpriteProcessorImpl.class);
374 
375 }