|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.texture.plugins; |
| 33 | + |
| 34 | +import com.jme3.texture.Image; |
| 35 | +import org.junit.jupiter.api.Assertions; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link StbImageLoader}, covering formats whose channel count is |
| 42 | + * not reported by the decoder's {@code info()} method (e.g. TGA). |
| 43 | + */ |
| 44 | +public class StbImageLoaderTest { |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a minimal, valid TGA image as a byte array. |
| 48 | + * |
| 49 | + * @param width image width in pixels |
| 50 | + * @param height image height in pixels |
| 51 | + * @param bpp bits per pixel: 24 (RGB) or 32 (RGBA) |
| 52 | + * @return raw TGA bytes |
| 53 | + */ |
| 54 | + private static byte[] makeTga(int width, int height, int bpp) { |
| 55 | + int bytesPerPixel = bpp / 8; |
| 56 | + int pixelDataSize = width * height * bytesPerPixel; |
| 57 | + // TGA header is 18 bytes, followed by pixel data |
| 58 | + byte[] tga = new byte[18 + pixelDataSize]; |
| 59 | + |
| 60 | + tga[0] = 0; // ID length |
| 61 | + tga[1] = 0; // Color map type: none |
| 62 | + tga[2] = 2; // Image type: uncompressed true-color |
| 63 | + // Color map spec (5 bytes): all zero |
| 64 | + // Image spec: |
| 65 | + tga[8] = 0; tga[9] = 0; // X origin |
| 66 | + tga[10] = 0; tga[11] = 0; // Y origin |
| 67 | + tga[12] = (byte) (width & 0xFF); tga[13] = (byte) ((width >> 8) & 0xFF); |
| 68 | + tga[14] = (byte) (height & 0xFF); tga[15] = (byte) ((height >> 8) & 0xFF); |
| 69 | + tga[16] = (byte) bpp; // Bits per pixel |
| 70 | + tga[17] = 0x20; // Image descriptor: top-left origin |
| 71 | + |
| 72 | + // Fill pixel data with a simple pattern (stored as BGR or BGRA in TGA) |
| 73 | + for (int i = 0; i < width * height; i++) { |
| 74 | + int base = 18 + i * bytesPerPixel; |
| 75 | + tga[base] = (byte) 0xFF; // B |
| 76 | + tga[base + 1] = (byte) 0x80; // G |
| 77 | + tga[base + 2] = (byte) 0x40; // R |
| 78 | + if (bpp == 32) { |
| 79 | + tga[base + 3] = (byte) 0xFF; // A |
| 80 | + } |
| 81 | + } |
| 82 | + return tga; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Verifies that a 24-bit (RGB) TGA image loads without throwing an exception |
| 87 | + * and is decoded as {@link Image.Format#RGB8}. |
| 88 | + * |
| 89 | + * <p>This is a regression test for the bug where TGA images caused an |
| 90 | + * {@code IOException("Unsupported number of channels: 0")} because the |
| 91 | + * stb-image {@code TgaDecoder.info()} did not set the channel count. |
| 92 | + */ |
| 93 | + @Test |
| 94 | + public void testLoad24BitTga() throws IOException { |
| 95 | + byte[] tgaData = makeTga(2, 2, 24); |
| 96 | + StbImageLoader loader = new StbImageLoader(); |
| 97 | + Image image = loader.load(tgaData, false); |
| 98 | + |
| 99 | + Assertions.assertNotNull(image, "Image must not be null"); |
| 100 | + Assertions.assertEquals(Image.Format.RGB8, image.getFormat(), |
| 101 | + "24-bit TGA should be decoded as RGB8"); |
| 102 | + Assertions.assertEquals(2, image.getWidth()); |
| 103 | + Assertions.assertEquals(2, image.getHeight()); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Verifies that a grayscale (8-bit, 1-channel) TGA image loads correctly |
| 108 | + * as {@link Image.Format#Luminance8}. |
| 109 | + */ |
| 110 | + @Test |
| 111 | + public void testLoadGrayscaleTga() throws IOException { |
| 112 | + // Build a 1-channel (grayscale) TGA: image type 3 = uncompressed grayscale |
| 113 | + int width = 2, height = 2; |
| 114 | + byte[] tga = new byte[18 + width * height]; |
| 115 | + tga[0] = 0; // ID length |
| 116 | + tga[1] = 0; // Color map type: none |
| 117 | + tga[2] = 3; // Image type: uncompressed grayscale |
| 118 | + tga[12] = (byte) width; |
| 119 | + tga[14] = (byte) height; |
| 120 | + tga[16] = 8; // 8 bits per pixel |
| 121 | + tga[17] = 0x20; // top-left origin |
| 122 | + // Fill pixel data |
| 123 | + for (int i = 0; i < width * height; i++) { |
| 124 | + tga[18 + i] = (byte) (i * 64); |
| 125 | + } |
| 126 | + |
| 127 | + StbImageLoader loader = new StbImageLoader(); |
| 128 | + Image image = loader.load(tga, false); |
| 129 | + |
| 130 | + Assertions.assertNotNull(image, "Image must not be null"); |
| 131 | + Assertions.assertEquals(Image.Format.Luminance8, image.getFormat(), |
| 132 | + "Grayscale TGA should be decoded as Luminance8"); |
| 133 | + Assertions.assertEquals(width, image.getWidth()); |
| 134 | + Assertions.assertEquals(height, image.getHeight()); |
| 135 | + } |
| 136 | +} |
0 commit comments