Search in sources :

Example 11 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project openolat by klemens.

the class ImageHelperImpl method writeTo.

/**
 * Can change this to choose a better compression level as the default
 * @param image
 * @param scaledImage
 * @return
 */
public static boolean writeTo(BufferedImage image, File scaledImage, Size scaledSize, String outputFormat) {
    try {
        if (!StringHelper.containsNonWhitespace(outputFormat)) {
            outputFormat = OUTPUT_FORMAT;
        }
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
        if (writers.hasNext()) {
            ImageWriter writer = writers.next();
            ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
            IIOImage iiOImage = new IIOImage(image, null, null);
            ImageOutputStream iOut = new FileImageOutputStream(scaledImage);
            writer.setOutput(iOut);
            writer.write(null, iiOImage, iwp);
            writer.dispose();
            iOut.flush();
            iOut.close();
            return true;
        } else {
            return ImageIO.write(image, outputFormat, scaledImage);
        }
    } catch (IOException e) {
        return false;
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 12 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project jdk8u_jdk by JetBrains.

the class ImageStreamFromRAF method main.

public static void main(String[] args) {
    try {
        File f = new File("ImageInputStreamFromRAF.tmp");
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        ImageInputStream istream = ImageIO.createImageInputStream(raf);
        ImageOutputStream ostream = ImageIO.createImageOutputStream(raf);
        f.delete();
        if (istream == null) {
            throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!");
        }
        if (ostream == null) {
            throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!");
        }
        if (!(istream instanceof FileImageInputStream)) {
            throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!");
        }
        if (!(ostream instanceof FileImageOutputStream)) {
            throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!");
        }
    } catch (IOException ioe) {
        throw new RuntimeException("Unexpected IOException: " + ioe);
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) RandomAccessFile(java.io.RandomAccessFile) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 13 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project litiengine by gurkenlabs.

the class ImageSerializer method saveImage.

public static void saveImage(final String fileName, final BufferedImage image, ImageFormat imageFormat) {
    try {
        final File file = new File(fileName);
        final String extension = FileUtilities.getExtension(fileName);
        Iterator<ImageWriter> iter = null;
        if (canWriteFormat(extension)) {
            iter = ImageIO.getImageWritersByFormatName(extension);
        } else {
            iter = ImageIO.getImageWritersByFormatName(imageFormat.toString());
        }
        final ImageWriter writer = iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        file.getParentFile().mkdirs();
        try (final FileImageOutputStream output = new FileImageOutputStream(file.getAbsoluteFile())) {
            writer.setOutput(output);
            final IIOImage outimage = new IIOImage(image, null, null);
            writer.write(null, outimage, iwp);
            writer.dispose();
        }
    } catch (final IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) IIOImage(javax.imageio.IIOImage)

Example 14 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project Galacticraft by micdoodle8.

the class MapUtil method writeImgToFile.

@SideOnly(Side.CLIENT)
public static void writeImgToFile(BufferedImage img, String name) {
    if (GalacticraftCore.enableJPEG) {
        File folder = new File(FMLClientHandler.instance().getClient().mcDataDir, "assets/galacticraftMaps");
        try {
            ImageOutputStream outputStreamA = new FileImageOutputStream(new File(folder, name));
            GalacticraftCore.jpgWriter.setOutput(outputStreamA);
            GalacticraftCore.jpgWriter.write(null, new IIOImage(img, null, null), GalacticraftCore.writeParam);
            outputStreamA.close();
        } catch (Exception e) {
        }
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 15 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project Galacticraft by micdoodle8.

the class MapUtil method getOverworldImageFromRaw.

@SideOnly(Side.CLIENT)
public static void getOverworldImageFromRaw(int cx, int cz, byte[] raw) throws IOException {
    File folder = MapUtil.getClientMapsFolder();
    if (raw.length == OVERWORLD_LARGEMAP_WIDTH * OVERWORLD_LARGEMAP_HEIGHT * 2) {
        if (folder != null) {
            File file0 = new File(folder, "overworldRaw.bin");
            if (!file0.exists() || (file0.canRead() && file0.canWrite())) {
                FileUtils.writeByteArrayToFile(file0, raw);
            } else {
                System.err.println("Cannot write to file %minecraft%/assets/galacticraftMaps/overworldRaw.bin");
            }
        } else {
            System.err.println("No folder for file %minecraft%/assets/galacticraftMaps/overworldRaw.bin");
        }
        // raw is a WIDTH_WORLD x HEIGHT_WORLD array of 2 byte entries: biome type followed by height
        // Here we will make a texture from that, but twice as large: 4 pixels for each data point, it just looks better that way when the texture is used
        BufferedImage worldImageLarge = new BufferedImage(OVERWORLD_LARGEMAP_WIDTH * 2, OVERWORLD_LARGEMAP_HEIGHT * 2, BufferedImage.TYPE_INT_RGB);
        ArrayList<Integer> cols = new ArrayList<Integer>();
        int lastcol = -1;
        int idx = 0;
        for (int x = 0; x < OVERWORLD_LARGEMAP_WIDTH; x++) {
            for (int z = 0; z < OVERWORLD_LARGEMAP_HEIGHT; z++) {
                int arrayIndex = (x * OVERWORLD_LARGEMAP_HEIGHT + z) * 2;
                int biome = ((int) raw[arrayIndex]) & 255;
                int height = ((int) raw[arrayIndex + 1]) & 255;
                if (height < OCEAN_HEIGHT && biome != 2 && biome != 10) {
                    // Includes ponds, lakes and rivers in other biomes
                    biome = 0;
                }
                if (height < DEEP_OCEAN && biome == 0) {
                    biome = 24;
                }
                worldImageLarge.setRGB(x * 2, z * 2, convertBiomeColour(biome, height));
                worldImageLarge.setRGB(x * 2, z * 2 + 1, convertBiomeColour(biome, height));
                worldImageLarge.setRGB(x * 2 + 1, z * 2, convertBiomeColour(biome, height));
                worldImageLarge.setRGB(x * 2 + 1, z * 2 + 1, convertBiomeColour(biome, height));
            }
        }
        // Write it to a .jpg file on client for beta preview
        if (GalacticraftCore.enableJPEG && folder != null) {
            ImageOutputStream outputStream = new FileImageOutputStream(new File(folder, "large.jpg"));
            GalacticraftCore.jpgWriter.setOutput(outputStream);
            GalacticraftCore.jpgWriter.write(null, new IIOImage(worldImageLarge, null, null), GalacticraftCore.writeParam);
            outputStream.close();
        }
    } else // This is the dimensions of the Overworld texture map
    if (raw.length == OVERWORLD_TEXTURE_WIDTH * OVERWORLD_TEXTURE_HEIGHT * 2) {
        // raw is a WIDTH_STD x HEIGHT_STD array of 2 byte entries: biome type followed by height
        BufferedImage worldImage = new BufferedImage(OVERWORLD_TEXTURE_WIDTH, OVERWORLD_TEXTURE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        ArrayList<Integer> cols = new ArrayList<Integer>();
        int lastcol = -1;
        int idx = 0;
        for (int x = 0; x < OVERWORLD_TEXTURE_WIDTH; x++) {
            for (int z = 0; z < OVERWORLD_TEXTURE_HEIGHT; z++) {
                int arrayIndex = (x * OVERWORLD_TEXTURE_HEIGHT + z) * 2;
                int biome = ((int) raw[arrayIndex]) & 255;
                int height = ((int) raw[arrayIndex + 1]) & 255;
                if (height < OCEAN_HEIGHT && biome != 2 && biome != 10) {
                    // Includes ponds, lakes and rivers in other biomes
                    biome = 0;
                }
                if (height < DEEP_OCEAN && biome == 0) {
                    biome = 24;
                }
                worldImage.setRGB(x, z, convertBiomeColour(biome, height));
            }
        }
        IResourceManager rm = Minecraft.getMinecraft().getResourceManager();
        BufferedImage paletteImage = null;
        try {
            InputStream in = rm.getResource(new ResourceLocation(Constants.ASSET_PREFIX, "textures/gui/celestialbodies/earth.png")).getInputStream();
            paletteImage = ImageIO.read(in);
            in.close();
            paletteImage.getHeight();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        BufferedImage result = convertTo12pxTexture(worldImage, paletteImage);
        if (result != null) {
            if (ClientProxyCore.overworldTextureWide == null) {
                ClientProxyCore.overworldTextureWide = new DynamicTextureProper(OVERWORLD_TEXTURE_WIDTH, OVERWORLD_TEXTURE_HEIGHT);
            }
            if (ClientProxyCore.overworldTextureClient == null) {
                ClientProxyCore.overworldTextureClient = new DynamicTextureProper(OVERWORLD_TEXTURE_HEIGHT, OVERWORLD_TEXTURE_HEIGHT);
            }
            ClientProxyCore.overworldTextureWide.update(result);
            ClientProxyCore.overworldTextureClient.update(result);
            ClientProxyCore.overworldTexturesValid = true;
        }
    } else if (folder != null) {
        File file0 = makeFileName(folder, cx, cz);
        if (!file0.exists() || (file0.canRead() && file0.canWrite())) {
            FileUtils.writeByteArrayToFile(file0, raw);
        }
    } else {
        System.err.println("No folder %minecraft%/assets/galacticraftMaps for local map file.");
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) DynamicTextureProper(micdoodle8.mods.galacticraft.core.client.DynamicTextureProper) BufferedImage(java.awt.image.BufferedImage) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) ResourceLocation(net.minecraft.util.ResourceLocation) IResourceManager(net.minecraft.client.resources.IResourceManager) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Aggregations

FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)21 File (java.io.File)18 ImageWriter (javax.imageio.ImageWriter)14 IOException (java.io.IOException)13 IIOImage (javax.imageio.IIOImage)13 ImageWriteParam (javax.imageio.ImageWriteParam)10 BufferedImage (java.awt.image.BufferedImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)8 Rectangle (java.awt.Rectangle)4 Iterator (java.util.Iterator)3 ImageReader (javax.imageio.ImageReader)3 FileImageInputStream (javax.imageio.stream.FileImageInputStream)3 Test (org.junit.Test)3 Graphics2D (java.awt.Graphics2D)2 Robot (java.awt.Robot)2 BufferedInputStream (java.io.BufferedInputStream)2 URLConnection (java.net.URLConnection)2 DataFormatException (java.util.zip.DataFormatException)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)2