EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [ImageToolsTest.java]

nameclass, %method, %block, %line, %
ImageToolsTest.java100% (1/1)90%  (35/39)78%  (1365/1748)79%  (236.3/300)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ImageToolsTest100% (1/1)90%  (35/39)78%  (1365/1748)79%  (236.3/300)
showImage (RenderedImage): void 0%   (0/1)0%   (0/38)0%   (0/13)
testColorFillBorder (): void 0%   (0/1)0%   (0/19)0%   (0/7)
testFillColorValues (): void 0%   (0/1)0%   (0/17)0%   (0/5)
testGrayFillColorValues (): void 0%   (0/1)0%   (0/13)0%   (0/4)
testGetImageReader (): void 100% (1/1)40%  (24/60)57%  (8/14)
testBrokenEmptyImage (): void 100% (1/1)50%  (9/18)67%  (4/6)
testGetSize (String): void 100% (1/1)52%  (33/63)68%  (8.1/12)
testFillColorValues (String, int): void 100% (1/1)54%  (151/279)60%  (22.3/37)
assertPixelEquals (RenderedImage, int, int, String): void 100% (1/1)55%  (45/82)70%  (6.3/9)
<static initializer> 100% (1/1)68%  (17/25)70%  (1.4/2)
testColorFillBorder (String): void 100% (1/1)87%  (103/119)87%  (19.1/22)
assertActualSize (RenderedImage): void 100% (1/1)91%  (21/23)96%  (2.9/3)
testCreateColorBox (String): void 100% (1/1)92%  (47/51)95%  (9.5/10)
testScaleFitHeight (): void 100% (1/1)93%  (123/132)97%  (13.5/14)
testScaleFitWidth (): void 100% (1/1)93%  (123/132)97%  (13.5/14)
testScaleFit (): void 100% (1/1)94%  (137/145)98%  (12.7/13)
ImageToolsTest (String): void 100% (1/1)100% (31/31)100% (9/9)
assertFit (RenderedImage, boolean): void 100% (1/1)100% (35/35)100% (5/5)
testCreateColorBox (): void 100% (1/1)100% (13/13)100% (5/5)
testCreateImageInputStream (): void 100% (1/1)100% (27/27)100% (9/9)
testGetDeltaRotation (): void 100% (1/1)100% (57/57)100% (9/9)
testGetFixedRotation (): void 100% (1/1)100% (49/49)100% (9/9)
testGetGIF89Size (): void 100% (1/1)100% (4/4)100% (2/2)
testGetGif87aSize (): void 100% (1/1)100% (4/4)100% (2/2)
testGetIbmTiffSize (): void 100% (1/1)100% (4/4)100% (2/2)
testGetImageFormat (): void 100% (1/1)100% (21/21)100% (6/6)
testGetImageFormat (String, String): void 100% (1/1)100% (22/22)100% (4/4)
testGetJpeg2000Size (): void 100% (1/1)100% (4/4)100% (2/2)
testGetJpegSize (): void 100% (1/1)100% (4/4)100% (2/2)
testGetLeftRotation (): void 100% (1/1)100% (25/25)100% (5/5)
testGetMacTiffSize (): void 100% (1/1)100% (4/4)100% (2/2)
testGetNoSize (): void 100% (1/1)100% (10/10)100% (3/3)
testGetPngSize (): void 100% (1/1)100% (4/4)100% (2/2)
testGetRightRotation (): void 100% (1/1)100% (25/25)100% (5/5)
testIsCompressedFormat (): void 100% (1/1)100% (21/21)100% (6/6)
testIsCompressedFormat (String, boolean): void 100% (1/1)100% (36/36)100% (5/5)
testRotated (): void 100% (1/1)100% (18/18)100% (4/4)
testScaleActual (): void 100% (1/1)100% (47/47)100% (7/7)
testSqueezePerformance (): void 100% (1/1)100% (67/67)100% (10/10)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 Thomas Aglassinger
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16package net.sf.jomic.tools;
17 
18import java.awt.Color;
19import java.awt.Dimension;
20import java.awt.Transparency;
21import java.awt.color.ColorSpace;
22import java.awt.image.BufferedImage;
23import java.awt.image.ColorModel;
24import java.awt.image.ComponentColorModel;
25import java.awt.image.DataBuffer;
26import java.awt.image.RenderedImage;
27import java.io.File;
28import java.io.IOException;
29import java.util.NoSuchElementException;
30 
31import javax.imageio.ImageReader;
32import javax.imageio.stream.ImageInputStream;
33import javax.media.jai.BorderExtenderConstant;
34import javax.media.jai.JAI;
35import javax.media.jai.ParameterBlockJAI;
36import javax.media.jai.PlanarImage;
37import javax.media.jai.util.ImagingException;
38import javax.swing.JFrame;
39 
40import junit.framework.TestCase;
41import net.sf.jomic.jaiunit.Tools;
42import net.sf.wraplog.Logger;
43 
44import com.sun.media.jai.widget.DisplayJAI;
45 
46/**
47 *  Test case for ImageTools.
48 *
49 * @author    Thomas Aglassinger
50 */
51public class ImageToolsTest extends TestCase
52{
53    /**
54     *  Number of bits to use per pixel.
55     */
56    private static final int BITS_PER_COMPONENT = 8;
57    private static final int STANDARD_IMAGE_HEIGHT = 640;
58    private static final int STANDARD_IMAGE_WIDTH = 480;
59 
60    private static Logger logger = Logger.getLogger(ImageToolsTest.class);
61 
62    private FileTools fileTools;
63    private ImageTools imageTools;
64    private RenderedImage source;
65    private int sourceHeight;
66    private int sourceWidth;
67    private StringTools stringTools;
68    private TestTools testTools;
69 
70    public ImageToolsTest(String arg0)
71        throws IOException {
72        super(arg0);
73        testTools = TestTools.instance();
74        source = testTools.getTestImage();
75        sourceWidth = source.getWidth();
76        sourceHeight = source.getHeight();
77        fileTools = FileTools.instance();
78        imageTools = ImageTools.instance();
79        stringTools = StringTools.instance();
80    }
81 
82    public void testBrokenEmptyImage()
83        throws IOException {
84        try {
85            imageTools.readImage(testTools.getTestFile(TestTools.TEST_IMAGE_BROKEN_EMPTY_NAME));
86        } catch (ImagingException expectedError) {
87            if (logger.isDebugEnabled()) {
88                logger.debug("ignoring expected error", expectedError);
89            }
90        }
91    }
92 
93    public void testColorFillBorder() {
94        testColorFillBorder("#000000");
95        testColorFillBorder("#ff0000");
96        testColorFillBorder("#00ff00");
97        testColorFillBorder("#0000ff");
98        testColorFillBorder("#ffffff");
99        testColorFillBorder("#123456");
100    }
101 
102    public void testCreateColorBox() {
103        testCreateColorBox("#123456");
104        testCreateColorBox("#808080");
105        testCreateColorBox("#ffffff");
106        testCreateColorBox("#000000");
107    }
108 
109    public void testCreateImageInputStream()
110        throws IOException {
111        ImageInputStream in;
112 
113        File imageFile = testTools.getTestImageFile();
114 
115        in = imageTools.createImageInputStream(imageFile);
116        assertNotNull(in);
117        in.close();
118 
119        File textFile = testTools.getTestTextFile();
120 
121        in = imageTools.createImageInputStream(textFile);
122        assertNotNull(in);
123        in.close();
124    }
125 
126    public void testFillColorValues()
127        throws IOException {
128        testFillColorValues("#123456", ColorSpace.CS_sRGB);
129        testFillColorValues("#808080", ColorSpace.CS_sRGB);
130        testFillColorValues("#ffffff", ColorSpace.CS_sRGB);
131        testFillColorValues("#000000", ColorSpace.CS_sRGB);
132    }
133 
134    public void testGetDeltaRotation() {
135        assertEquals(-1, imageTools.getDeltaRotation(0, -1));
136        assertEquals(2, imageTools.getDeltaRotation(0, 2));
137        assertEquals(0, imageTools.getDeltaRotation(0, 0));
138        assertEquals(-1, imageTools.getDeltaRotation(2, 1));
139        assertEquals(1, imageTools.getDeltaRotation(2, -1));
140        assertEquals(0, imageTools.getDeltaRotation(-1, -1));
141        assertEquals(2, imageTools.getDeltaRotation(-1, 1));
142        assertEquals(0, imageTools.getDeltaRotation(1, 1));
143    }
144 
145    public void testGetFixedRotation() {
146        assertEquals(-1, imageTools.getFixedRotation(-1));
147        assertEquals(0, imageTools.getFixedRotation(0));
148        assertEquals(1, imageTools.getFixedRotation(1));
149        assertEquals(2, imageTools.getFixedRotation(2));
150        assertEquals(-1, imageTools.getFixedRotation(3));
151        assertEquals(2, imageTools.getFixedRotation(-2));
152        assertEquals(1, imageTools.getFixedRotation(-3));
153        assertEquals(0, imageTools.getFixedRotation(-4));
154    }
155 
156    public void testGetGIF89Size()
157        throws IOException {
158        testGetSize(TestTools.TEST_IMAGE_GIF89A);
159    }
160 
161    public void testGetGif87aSize()
162        throws IOException {
163        testGetSize(TestTools.TEST_IMAGE_GIF87A);
164    }
165 
166    public void testGetIbmTiffSize()
167        throws IOException {
168        testGetSize(TestTools.TEST_IMAGE_IBM_TIFF);
169    }
170 
171    public void testGetImageFormat()
172        throws IOException {
173        testGetImageFormat("jpeg", TestTools.TEST_IMAGE_JPG_NAME);
174        testGetImageFormat("png", TestTools.TEST_IMAGE_PNG_NAME);
175        testGetImageFormat("jpeg", TestTools.TEST_IMAGE_DISGUISED_JPG);
176        testGetImageFormat("png", TestTools.TEST_IMAGE_DISGUISED_PNG);
177        testGetImageFormat(null, TestTools.TEST_TEXT_NAME);
178    }
179 
180    public void testGetImageFormat(String expectedFormat, String fileName)
181        throws IOException {
182        File imageFile = testTools.getTestFile(fileName);
183        String actualFormat = imageTools.getImageFormat(imageFile);
184 
185        assertEquals("image format must match: " + imageFile, expectedFormat, actualFormat);
186    }
187 
188    public void testGetImageReader()
189        throws IOException {
190        ImageInputStream imageStream;
191        ImageReader reader;
192 
193        // Get a reader for an actual image.
194        imageStream = imageTools.createImageInputStream(testTools.getTestImageFile());
195        try {
196            reader = imageTools.getImageReader(imageStream);
197            assertNotNull(reader);
198            reader.dispose();
199        } finally {
200            imageStream.close();
201        }
202 
203        // Attempt to get a reader for a text file, which won't work.
204        File testTextFile = testTools.getTestTextFile();
205 
206        imageStream = imageTools.createImageInputStream(testTextFile);
207        try {
208            reader = imageTools.getImageReader(imageStream);
209            fail("must not find reader for text file: " + testTextFile);
210        } catch (NoSuchElementException expectedError) {
211            // Ignore expected error.
212        } finally {
213            imageStream.close();
214        }
215    }
216 
217    public void testGetJpeg2000Size()
218        throws IOException {
219        testGetSize(TestTools.TEST_IMAGE_JP2_NAME);
220    }
221 
222    public void testGetJpegSize()
223        throws IOException {
224        testGetSize(TestTools.TEST_IMAGE_JPG_NAME);
225    }
226 
227    public void testGetLeftRotation() {
228        assertEquals(2, imageTools.getLeftRotation(-1));
229        assertEquals(-1, imageTools.getLeftRotation(0));
230        assertEquals(0, imageTools.getLeftRotation(1));
231        assertEquals(1, imageTools.getLeftRotation(2));
232    }
233 
234    public void testGetMacTiffSize()
235        throws IOException {
236        testGetSize(TestTools.TEST_IMAGE_MAC_TIFF);
237    }
238 
239    public void testGetNoSize()
240        throws IOException {
241        Dimension noSize = imageTools.getImageDimension(testTools.getTestTextFile());
242 
243        assertNull(noSize);
244    }
245 
246    public void testGetPngSize()
247        throws IOException {
248        testGetSize(TestTools.TEST_IMAGE_PNG_NAME);
249    }
250 
251    public void testGetRightRotation() {
252        assertEquals(0, imageTools.getRightRotation(-1));
253        assertEquals(1, imageTools.getRightRotation(0));
254        assertEquals(2, imageTools.getRightRotation(1));
255        assertEquals(-1, imageTools.getRightRotation(2));
256    }
257 
258    public void testGrayFillColorValues()
259        throws IOException {
260        testFillColorValues("#808080", ColorSpace.CS_GRAY);
261        testFillColorValues("#ffffff", ColorSpace.CS_GRAY);
262        testFillColorValues("#000000", ColorSpace.CS_GRAY);
263    }
264 
265    public void testIsCompressedFormat()
266        throws IOException {
267        testIsCompressedFormat(TestTools.TEST_IMAGE_JPG_NAME, true);
268        testIsCompressedFormat(TestTools.TEST_IMAGE_PNG_NAME, true);
269        testIsCompressedFormat(TestTools.TEST_IMAGE_GIF87A, true);
270        testIsCompressedFormat(TestTools.TEST_IMAGE_GIF89A, true);
271        testIsCompressedFormat(TestTools.TEST_IMAGE_MAC_TIFF, false);
272    }
273 
274    public void testRotated()
275        throws IOException {
276        RenderedImage image = testTools.getTestImage();
277 
278        for (int rotation = -1; rotation < 3; rotation += 1) {
279            RenderedImage rotatedImage = imageTools.getRotatedImage(image, rotation);
280 
281            if (false) {
282                showImage(rotatedImage);
283            }
284        }
285    }
286 
287    public void testScaleActual() {
288        RenderedImage target;
289 
290        target = imageTools.getSqueezed(source, sourceHeight, sourceWidth,
291                ImageTools.SCALE_ACTUAL);
292        assertActualSize(target);
293 
294        target = imageTools.getSqueezed(source, sourceHeight, sourceWidth,
295                ImageTools.SCALE_ACTUAL);
296        assertActualSize(target);
297 
298        target = imageTools.getSqueezed(source, sourceHeight / 3, sourceWidth / 2,
299                ImageTools.SCALE_ACTUAL);
300        assertActualSize(target);
301    }
302 
303    public void testScaleFit() {
304        RenderedImage scaled;
305 
306        scaled = imageTools.getSqueezed(source, sourceWidth, sourceHeight, ImageTools.SCALE_FIT);
307        assertFit(scaled, scaled.getWidth() == sourceWidth);
308        assertFit(scaled, scaled.getHeight() == sourceHeight);
309 
310        scaled = imageTools.getSqueezed(source, 2 * sourceWidth, sourceHeight,
311                ImageTools.SCALE_FIT);
312        assertFit(scaled, scaled.getWidth() == sourceWidth);
313        assertFit(scaled, scaled.getHeight() == sourceHeight);
314 
315        scaled = imageTools.getSqueezed(source, sourceWidth, 2 * sourceHeight,
316                ImageTools.SCALE_FIT);
317        assertFit(scaled, scaled.getWidth() == sourceWidth);
318        assertFit(scaled, scaled.getHeight() == sourceHeight);
319 
320        scaled = imageTools.getSqueezed(source, sourceWidth / 2, 2 * sourceHeight,
321                ImageTools.SCALE_FIT);
322        assertFit(scaled, scaled.getWidth() == sourceWidth / 2);
323        assertFit(scaled, scaled.getHeight() == sourceHeight / 2);
324    }
325 
326    public void testScaleFitHeight() {
327        RenderedImage scaled;
328 
329        scaled = imageTools.getSqueezed(source, sourceWidth / 10, sourceHeight,
330                ImageTools.SCALE_HEIGHT);
331        assertFit(scaled, scaled.getHeight() == sourceHeight);
332 
333        scaled = imageTools.getSqueezed(source, sourceWidth * 10, sourceHeight,
334                ImageTools.SCALE_HEIGHT);
335        assertFit(scaled, scaled.getHeight() == sourceHeight);
336 
337        for (int i = -5; i <= 5; i += 1) {
338            int width = sourceWidth + i;
339 
340            assert width > 0;
341            scaled = imageTools.getSqueezed(source, width, sourceHeight,
342                    ImageTools.SCALE_HEIGHT);
343            assertFit(scaled, scaled.getHeight() == sourceHeight);
344        }
345 
346        scaled = imageTools.getSqueezed(source, 1, sourceHeight,
347                ImageTools.SCALE_HEIGHT);
348        assertFit(scaled, scaled.getHeight() == sourceHeight);
349 
350        scaled = imageTools.getSqueezed(source, Integer.MAX_VALUE, sourceHeight,
351                ImageTools.SCALE_HEIGHT);
352        assertFit(scaled, scaled.getHeight() == sourceHeight);
353    }
354 
355    public void testScaleFitWidth() {
356        RenderedImage scaled;
357 
358        scaled = imageTools.getSqueezed(source, sourceWidth, sourceHeight / 10,
359                ImageTools.SCALE_WIDTH);
360        assertFit(scaled, scaled.getWidth() == sourceWidth);
361 
362        scaled = imageTools.getSqueezed(source, sourceWidth, sourceHeight * 10,
363                ImageTools.SCALE_WIDTH);
364        assertFit(scaled, scaled.getWidth() == sourceWidth);
365 
366        for (int i = -5; i <= 5; i += 1) {
367            int height = sourceHeight + i;
368 
369            assert height > 0;
370            scaled = imageTools.getSqueezed(source, sourceWidth, height,
371                    ImageTools.SCALE_WIDTH);
372            assertFit(scaled, scaled.getWidth() == sourceWidth);
373        }
374 
375        scaled = imageTools.getSqueezed(source, sourceWidth, 1,
376                ImageTools.SCALE_WIDTH);
377        assertFit(scaled, scaled.getWidth() == sourceWidth);
378 
379        scaled = imageTools.getSqueezed(source, sourceWidth, Integer.MAX_VALUE,
380                ImageTools.SCALE_WIDTH);
381        assertFit(scaled, scaled.getWidth() == sourceWidth);
382    }
383 
384    /**
385     *  Test that getSqueezed is reasonably fast compared to reading an image, even if the render
386     *  settings are high quality.
387     */
388    public void testSqueezePerformance()
389        throws IOException {
390        long startTimeMillis = System.currentTimeMillis();
391        RenderedImage image = imageTools.readImage(testTools.getTestFile("scaled_slowly.jpg"));
392        long durationForRead = System.currentTimeMillis() - startTimeMillis;
393 
394        logger.info("reading lasted " + durationForRead + " ms");
395 
396        startTimeMillis = System.currentTimeMillis();
397        image = imageTools.getSqueezed(image, (int) Math.round(0.23 * image.getWidth()),
398                2 * image.getHeight(), ImageTools.SCALE_FIT);
399 
400        long durationForSqueeze = System.currentTimeMillis() - startTimeMillis;
401 
402        logger.info("squeeze lasted " + durationForSqueeze + " ms");
403        testTools.assertLessThan(durationForSqueeze, durationForRead);
404    }
405 
406    private void assertActualSize(RenderedImage target) {
407        assertFit(target, target.getWidth() == sourceWidth);
408        assertFit(target, target.getHeight() == sourceHeight);
409    }
410 
411    private void assertFit(RenderedImage image, boolean condition) {
412        String message = "source = " + sourceWidth + "x" + sourceHeight + " -> scaled="
413                + image.getWidth() + "x" + image.getHeight();
414 
415        if (logger.isInfoEnabled()) {
416            logger.info(message);
417        }
418        assertTrue(message, condition);
419    }
420 
421    private void assertPixelEquals(RenderedImage image, int x, int y, String expectedColorText) {
422        assert image != null;
423        assert (x >= 0) && (x < image.getWidth()) : "x=" + x;
424        assert (y >= 0) && (y < image.getHeight()) : "y=" + y;
425        assert expectedColorText != null;
426        assert image instanceof BufferedImage : "image must be a BufferedImage (for now)";
427        int rgba = ((BufferedImage) image).getRGB(x, y);
428        String actualColorText = stringTools.colorString(rgba);
429 
430        assertEquals(expectedColorText, actualColorText);
431    }
432 
433    private void showImage(RenderedImage both) {
434        JFrame frame = new JFrame(getClass().getName());
435        DisplayJAI imageView = new DisplayJAI();
436 
437        frame.getContentPane().add(imageView);
438        imageView.set(both);
439        frame.pack();
440        frame.setVisible(true);
441        testTools.waitSomeTime();
442        try {
443            Thread.sleep(5000);
444        } catch (InterruptedException error) {
445            logger.warn("interrupted", error);
446        }
447        frame.dispose();
448    }
449 
450    private void testColorFillBorder(String expectedColorText) {
451        assert expectedColorText != null;
452        Tools jaiUnitTools = Tools.instance();
453        RenderedImage image = jaiUnitTools.createTestImage(
454                STANDARD_IMAGE_WIDTH, STANDARD_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB, "test", "border at bottom");
455        Color color = Color.decode(expectedColorText);
456        int bottomPad = 10;
457        double[] fillValues = imageTools.fillColorValues(color, image.getColorModel());
458        ParameterBlockJAI pbBorder = new ParameterBlockJAI("border");
459 
460        pbBorder.addSource(image);
461        pbBorder.setParameter("leftPad", 0);
462        pbBorder.setParameter("rightPad", 0);
463        pbBorder.setParameter("topPad", 0);
464        pbBorder.setParameter("bottomPad", bottomPad);
465        pbBorder.setParameter("type", new BorderExtenderConstant(fillValues));
466 
467        RenderedImage imageWithBorder = JAI.create("border", pbBorder);
468        BufferedImage bufferedImageWithBorder = PlanarImage.wrapRenderedImage(imageWithBorder).getAsBufferedImage();
469 
470        if (logger.isDebugEnabled()) {
471            logger.debug("image=" + image);
472        }
473        int argbBorder = bufferedImageWithBorder.getRGB(2, image.getHeight() + 2);
474        String actualColorText = stringTools.colorString(argbBorder);
475 
476        jaiUnitTools.showImage(imageWithBorder, ImageToolsTest.class, "testColorFillBorder");
477        assertEquals("actualColorText", expectedColorText, actualColorText);
478    }
479 
480    private void testCreateColorBox(String colorText) {
481        assert colorText != null;
482        Color color = Color.decode(colorText);
483        RenderedImage image = imageTools.createColorBox(STANDARD_IMAGE_WIDTH, STANDARD_IMAGE_HEIGHT, color);
484 
485        assertEquals(STANDARD_IMAGE_WIDTH, image.getWidth());
486        assertEquals(STANDARD_IMAGE_HEIGHT, image.getHeight());
487        assertPixelEquals(image, 1, 1, colorText);
488        assertPixelEquals(image, STANDARD_IMAGE_WIDTH - 2, 1, colorText);
489        assertPixelEquals(image, 1, STANDARD_IMAGE_HEIGHT - 2, colorText);
490        assertPixelEquals(image, STANDARD_IMAGE_WIDTH - 2, STANDARD_IMAGE_HEIGHT - 2, colorText);
491    }
492 
493    private void testFillColorValues(String colorText, int colorSpaceType)
494        throws IOException {
495        assert colorText != null;
496        RenderedImage image = null;
497 
498        image = imageTools.readImage(testTools.getTestImageFile());
499        if (logger.isDebugEnabled()) {
500            logger.debug("color image: " + image + ", type=" + image.getColorModel().getTransferType());
501        }
502        Color color = Color.decode(colorText);
503        ColorSpace colorSpace = ColorSpace.getInstance(colorSpaceType);
504        int componentCount = colorSpace.getNumComponents();
505        int[] bits = new int[componentCount];
506 
507        for (int i = 0; i < componentCount; i += 1) {
508            bits[i] = BITS_PER_COMPONENT;
509        }
510 
511        ColorModel colorModel = new ComponentColorModel(colorSpace,
512                false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
513        double[] fillColorValues = imageTools.fillColorValues(color, colorModel);
514 
515        assertEquals("fillColorValues.length", componentCount, fillColorValues.length);
516 
517        int rgb = 0;
518 
519        for (int i = 0; i < componentCount; i += 1) {
520            double fillColorValue = fillColorValues[i];
521            float minValue = colorSpace.getMinValue(i);
522            float maxValue = colorSpace.getMaxValue(i);
523            double range = maxValue - minValue;
524 
525            if (logger.isInfoEnabled()) {
526                logger.info("fillColorValues[" + i + "]: " + minValue + " <= " + fillColorValue + " <= " + maxValue);
527            }
528            assert fillColorValue <= maxValue
529                    : "fillColorValues[" + i + "]=" + fillColorValue + " but must be <= " + maxValue;
530            assert fillColorValue >= minValue
531                    : "fillColorValues[" + i + "]=" + fillColorValue + " but must be >= " + minValue;
532            int component = (int) (fillColorValue * 255.0 / range);
533 
534            if (logger.isInfoEnabled()) {
535                logger.info("rgb=" + rgb + ", c=" + component);
536                logger.info("rgb*256=" + (rgb * 256));
537                logger.info("rgb*256+c=" + (rgb * 256 + component));
538            }
539            rgb = rgb * 256 + component;
540        }
541        if (componentCount == 1) {
542            rgb = rgb * 0x10000 + rgb * 0x100 + rgb;
543        }
544        String resultColorText = stringTools.colorString(rgb);
545 
546        if (logger.isInfoEnabled()) {
547            logger.info(colorText + " -> " + resultColorText);
548        }
549        assertEquals("resultColorText", colorText, resultColorText);
550    }
551 
552    private void testGetSize(String imageName)
553        throws IOException {
554        assert imageName != null;
555        assert imageName.indexOf('.') >= 0;
556 
557        File file = testTools.getTestFile(imageName);
558 
559        try {
560            Dimension dimension = imageTools.getImageDimension(file);
561 
562            assertNotNull("dimension", dimension);
563            assertEquals(STANDARD_IMAGE_WIDTH, dimension.width);
564            assertEquals(STANDARD_IMAGE_HEIGHT, dimension.height);
565        } catch (NoSuchElementException error) {
566            String lastSuffix = fileTools.getSuffix(file);
567 
568            logger.warn("cannot get image dimension of \""
569                    + file + "\": ImageIO plug-in for suffix \"."
570                    + lastSuffix + "\" should be installed.");
571        }
572    }
573 
574    private void testIsCompressedFormat(String imageName, boolean expectedIsCompressed)
575        throws IOException {
576        File imageFile = testTools.getTestFile(imageName);
577        String imageFormat = imageTools.getImageFormat(imageFile);
578        boolean actualIsCompressed = imageTools.isCompressedImageFormat(imageFormat);
579 
580        assertEquals("compression of image format \"" + imageFormat + "\" from image file "
581                + stringTools.sourced(imageFile) + " must match", expectedIsCompressed, actualIsCompressed);
582    }
583}

[all classes][net.sf.jomic.tools]
EMMA 2.0.4217 (C) Vladimir Roubtsov