org.shaman.rpg.editor.textures.impl.TextureIOTest.java Source code

Java tutorial

Introduction

Here is the source code for org.shaman.rpg.editor.textures.impl.TextureIOTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.shaman.rpg.editor.textures.impl;

import com.jme3.gde.core.assets.ProjectAssetManager;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.apache.commons.io.*;
import org.junit.*;
import org.netbeans.api.annotations.common.StaticResource;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.shaman.rpg.editor.textures.TextureIO;

import static org.junit.Assert.*;

/**
 *
 * @author Sebastian Weiss
 */
public class TextureIOTest {
    private static final Logger LOG = Logger.getLogger(TextureIOTest.class.getName());
    private static final String ASSET_DIR = "assets";

    @StaticResource
    private static final String JPEG_TEXTURE = "org/shaman/rpg/editor/textures/impl/Ninja.jpg";
    @StaticResource
    private static final String PNG_TEXTURE = "org/shaman/rpg/editor/textures/impl/PrehistoricGrass.png";
    @StaticResource
    private static final String DDS_A8R8G8B8 = "org/shaman/rpg/editor/textures/impl/Sinbad.dds";
    @StaticResource
    private static final String DDS_DXT1 = "org/shaman/rpg/editor/textures/impl/TestDXT1.dds";

    private static Path projectPath;
    private static FileObject projectDir;
    private static Project project;
    private static ProjectAssetManager manager;
    private static TextureIO textureIO;

    public TextureIOTest() {
    }

    @BeforeClass
    public static void createProject() throws IOException {
        //create temp dir
        projectPath = Files.createTempDirectory("textureIOproject");
        projectDir = FileUtil.toFileObject(projectPath.toFile().getCanonicalFile());
        //setup project
        FileObject assetDir = projectDir.createFolder(ASSET_DIR);
        manager = new ProjectAssetManager(assetDir);
        project = manager.getProject();
        textureIO = TextureIO.create(project);

        LOG.info("dummy project created: " + projectPath);
    }

    @AfterClass
    public static void deleteProject() throws IOException {
        //delete temp dir
        FileUtils.deleteDirectory(projectPath.toFile());
        LOG.info("dummy project deleted");
    }

    private void assertColorEquals(int rgbExpected, int rgbActual, double delta) {
        Color exp = new Color(rgbExpected);
        float[] expA = exp.getComponents(null);
        Color act = new Color(rgbActual);
        float[] actA = act.getComponents(null);
        for (int i = 0; i < 3; ++i) {
            assertEquals(expA[i] * expA[3], actA[i] * actA[3], delta); //multiply with alpha
        }
    }

    private void assertImageEquals(BufferedImage expected, BufferedImage actual, double delta) {
        assertEquals(expected.getWidth(), actual.getWidth());
        assertEquals(expected.getHeight(), actual.getHeight());
        for (int x = 0; x < expected.getWidth(); ++x) {
            for (int y = 0; y < expected.getHeight(); ++y) {
                assertColorEquals(expected.getRGB(x, y), actual.getRGB(x, y), delta);
            }
        }
    }

    @Test
    public void testReadTexture() throws IOException {
        //copy test files
        FileObject dds1 = manager.createAsset("Textures1/dds1.dds");
        FileObject dds2 = manager.createAsset("Textures1/dds2.dds");
        FileObject jpg = manager.createAsset("Textures1/jpg.jpg");
        FileObject png = manager.createAsset("Textures1/png.png");
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(DDS_DXT1);
                OutputStream out = dds1.getOutputStream()) {
            IOUtils.copy(in, out);
        }
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(DDS_A8R8G8B8);
                OutputStream out = dds2.getOutputStream()) {
            IOUtils.copy(in, out);
        }
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(JPEG_TEXTURE);
                OutputStream out = jpg.getOutputStream()) {
            IOUtils.copy(in, out);
        }
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(PNG_TEXTURE);
                OutputStream out = png.getOutputStream()) {
            IOUtils.copy(in, out);
        }

        //test if file formats are correctly read
        try {
            TextureIO.TextureSettings settings = new TextureIO.TextureSettings();
            BufferedImage imgDDS1 = textureIO.readTexture(dds1, settings);
            assertNotNull(imgDDS1);
            assertEquals(TextureIO.FileFormat.DDS, settings.fileFormat);
            assertEquals(TextureIO.Compression.DXT1, settings.compression);
            assertEquals(TextureIO.PixelFormat.A8R8G8B8, settings.pixelFormat);
            BufferedImage imgDDS2 = textureIO.readTexture("Textures1/dds2.dds", settings);
            assertNotNull(imgDDS2);
            assertEquals(TextureIO.FileFormat.DDS, settings.fileFormat);
            assertEquals(TextureIO.Compression.NO_COMPRESSION, settings.compression);
            assertEquals(TextureIO.PixelFormat.A8R8G8B8, settings.pixelFormat);
            BufferedImage imgJPG = textureIO.readTexture(jpg, settings);
            assertNotNull(imgJPG);
            assertEquals(TextureIO.FileFormat.JPG, settings.fileFormat);
            assertEquals(TextureIO.Compression.NO_COMPRESSION, settings.compression);
            assertEquals(TextureIO.PixelFormat.A8R8G8B8, settings.pixelFormat);
            BufferedImage imgPNG = textureIO.readTexture("Textures1/png.png", settings);
            assertNotNull(imgPNG);
            assertEquals(TextureIO.FileFormat.PNG, settings.fileFormat);
            assertEquals(TextureIO.Compression.NO_COMPRESSION, settings.compression);
            assertEquals(TextureIO.PixelFormat.A8R8G8B8, settings.pixelFormat);

        } finally {
            //delete files
            try {
                dds1.delete();
                dds2.delete();
                jpg.delete();
                png.delete();
            } catch (Exception e) {
                LOG.log(Level.WARNING, "unable to delete files", e);
            }
        }
    }

    @Test
    public void testReadWriteDDS() throws IOException {
        //copy source file
        FileObject source = manager.createAsset("Textures3/source3.jpg");
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(JPEG_TEXTURE);
                OutputStream out = source.getOutputStream()) {
            IOUtils.copy(in, out);
        }
        BufferedImage sourceImg = ImageIO.read(TextureIOTest.class.getClassLoader().getResource(JPEG_TEXTURE));
        //save to dds
        TextureIO.TextureSettings settings = new TextureIO.TextureSettings(TextureIO.FileFormat.DDS,
                TextureIO.Compression.NO_COMPRESSION, TextureIO.PixelFormat.A8R8G8B8, true);
        String name1 = textureIO.createTexture(source, "Textures3", "dds1", settings);
        settings.compression = TextureIO.Compression.DXT5;
        String name2 = textureIO.createTexture(sourceImg, "Textures3", "dds2", settings);

        //read them in
        BufferedImage dds1 = textureIO.readTexture(name1, null);
        BufferedImage dds2 = textureIO.readTexture(name2, null);

        //save them again
        settings.fileFormat = TextureIO.FileFormat.PNG;
        settings.compression = TextureIO.Compression.NO_COMPRESSION;
        textureIO.createTexture(dds1, "Textures3", "dds1", settings);
        textureIO.createTexture(dds2, "Textures3", "dds2", settings);

        //assertImageEquals(sourceImg, dds1, 0.01);
        //assertImageEquals(sourceImg, dds2, 0.01);
    }

    @Test
    public void testWriteTextures() throws IOException, InterruptedException {
        //copy source file
        FileObject source = manager.createAsset("Textures2/source.png");
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(PNG_TEXTURE);
                OutputStream out = source.getOutputStream()) {
            IOUtils.copy(in, out);
        }
        FileObject source2 = manager.createAsset("Textures2/source2.jpg");
        try (InputStream in = TextureIOTest.class.getClassLoader().getResourceAsStream(JPEG_TEXTURE);
                OutputStream out = source2.getOutputStream()) {
            IOUtils.copy(in, out);
        }
        BufferedImage sourceImg = ImageIO.read(TextureIOTest.class.getClassLoader().getResource(PNG_TEXTURE));
        BufferedImage sourceImg2 = textureIO.readTexture(source, null);
        BufferedImage source2Img = ImageIO.read(TextureIOTest.class.getClassLoader().getResource(JPEG_TEXTURE));

        //wait a short time to sync
        Thread.sleep(100);

        //save in normal formats
        TextureIO.TextureSettings settings = new TextureIO.TextureSettings();
        settings.fileFormat = TextureIO.FileFormat.GIF;
        String name1 = textureIO.createTexture(sourceImg, "Textures2", "gif1", settings);
        settings.fileFormat = TextureIO.FileFormat.GIF;
        String name2 = textureIO.createTexture(source, "Textures2", "gif2", settings);
        settings.fileFormat = TextureIO.FileFormat.JPG;
        String name3 = textureIO.createTexture(sourceImg, "Textures2", "jpeg", settings);
        settings.fileFormat = TextureIO.FileFormat.PNG;
        String name4 = textureIO.createTexture(sourceImg2, "Textures2", "png", settings);

        //save in dds formats
        settings.fileFormat = TextureIO.FileFormat.DDS;
        settings.createMipMaps = true;
        settings.pixelFormat = TextureIO.PixelFormat.A8R8G8B8;
        settings.compression = TextureIO.Compression.NO_COMPRESSION;
        String name5 = textureIO.createTexture(source2Img, "Textures2", "dds1", settings);
        settings.compression = TextureIO.Compression.DXT1;
        settings.createMipMaps = false;
        String name6 = textureIO.createTexture(source2Img, "Textures2", "dds2", settings);
        settings.compression = TextureIO.Compression.DXT3;
        settings.createMipMaps = true;
        String name7 = textureIO.createTexture(source2, "Textures2", "dds3", settings);
        settings.compression = TextureIO.Compression.DXT5;
        settings.createMipMaps = false;
        String name8 = textureIO.createTexture(source2, "Textures2", "dds4", settings);
        settings.compression = TextureIO.Compression.NO_COMPRESSION;
        settings.createMipMaps = true;
        String name9 = textureIO.createTexture(source2, "Textures2", "dds5", settings);

        //load them and test
        //assertImageEquals(sourceImg, textureIO.readTexture(name1, null), 0.01);
        //assertImageEquals(sourceImg, textureIO.readTexture(name2, null), 0.05);
        //assertImageEquals(sourceImg, textureIO.readTexture(name3, null), 0.05);
        assertImageEquals(sourceImg, textureIO.readTexture(name4, null), 0.01);
        //assertImageEquals(sourceImg, textureIO.readTexture(name5, null), 0.2);
        //assertImageEquals(source2Img, textureIO.readTexture(name6, null), 0.2);
        //assertImageEquals(source2Img, textureIO.readTexture(name7, null), 0.2);
        //assertImageEquals(source2Img, textureIO.readTexture(name8, null), 0.2);
        //assertImageEquals(source2Img, textureIO.readTexture(name9, null), 0.2);
    }
}