Java Code Examples for javax.imageio.ImageWriteParam#setCompressionType()

The following examples show how to use javax.imageio.ImageWriteParam#setCompressionType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageGifFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes,
        ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 2
Source File: ImagePnmFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 3
Source File: ImagePngFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 4
Source File: ImageTiffFile.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static ImageWriteParam getPara(ImageAttributes attributes, ImageWriter writer) {
    try {
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (attributes != null && attributes.getCompressionType() != null) {
                param.setCompressionType(attributes.getCompressionType());
            } else {
                param.setCompressionType("LZW");
            }
            if (attributes != null && attributes.getQuality() > 0) {
                param.setCompressionQuality(attributes.getQuality() / 100.0f);
            } else {
                param.setCompressionQuality(1.0f);
            }
        }
        return param;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 5
Source File: Img.java    From java-tool with Apache License 2.0 6 votes vote down vote up
public void writeTo(OutputStream os, String mimeType) {
    ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType(mimeType)).next();
    dropAlphaChannelIfJPEG(writer);
    ImageWriteParam params = writer.getDefaultWriteParam();

    if (!Float.isNaN(compressionQuality) && params.canWriteCompressed()) {
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionType(params.getCompressionTypes()[0]);
        params.setCompressionQuality(compressionQuality);
    }

    ImageOutputStream ios = os(os);
    writer.setOutput(ios);
    IIOImage image = new IIOImage(get(), null, null);
    try {
        writer.write(null, image, params);
    } catch (IOException e) {
        throw ioException(e);
    }
    IO.flush(ios);
    writer.dispose();
}
 
Example 6
Source File: TestCompressionBI_BITFIELDS.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected ImageWriteParam prepareWriteParam(BufferedImage src) {
    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    wparam.setCompressionType("BI_BITFIELDS");

    return wparam;
}
 
Example 7
Source File: TiffSingleImageWriter.java    From sejda with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
ImageWriteParam newImageWriterParams(PdfToMultipleTiffParameters params) {
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(TIFF_COMPRESSION_TYPE_CACHE
            .get(ofNullable(params.getCompressionType()).orElse(TiffCompressionType.NONE)));
    return param;
}
 
Example 8
Source File: WebPWriterTest.java    From webp-imageio with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for
 * {@link com.luciad.imageio.webp.WebPWriter#write(javax.imageio.metadata.IIOMetadata, javax.imageio.IIOImage, javax.imageio.ImageWriteParam)}
 * .
 *
 * @throws IOException
 */
@Test(dataProvider = "createImagesWithCompressionOptions", enabled = true)
public void testImageWriterCompression(final RenderedImage im, final String compressionType,
      final float compressionQuality, final String outputName) throws IOException {
   final String extension = outputName.substring(outputName.lastIndexOf(".") + 1);

   // get writer
   final ImageWriter imgWriter = ImageIO.getImageWritersByFormatName(extension).next();
   final ImageWriteParam imgWriteParams = new WebPWriteParam(null);
   imgWriteParams.setCompressionType(compressionType);
   // 0~1
   imgWriteParams.setCompressionQuality(compressionQuality);
   final String testName = "CompressionOptions";
   final File file = createOutputFile(testName, outputName);
   final ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(file);
   try {
      imgWriter.setOutput(imageOutputStream);
      imgWriter.write(null, new IIOImage(im, null, null), imgWriteParams);
      final int length = (int) imageOutputStream.length();
      assertTrue(length > 0);
   } finally {
      try {
         imageOutputStream.close();
      } catch (final IOException e) {
      }
   }
}
 
Example 9
Source File: RLECompressionTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void doTest(int type) throws IOException {
    BufferedImage src = getTestImage(type);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    writer.setOutput(ios);

    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    switch(type) {
        case TEST_RLE8:
            wparam.setCompressionType("BI_RLE8");
            break;
        case TEST_RLE4:
            wparam.setCompressionType("BI_RLE4");
            break;
        default:
            throw new IllegalArgumentException("Wrong test type: " + type);
    }

    writer.write(null, new IIOImage(src, null, null), wparam);

    ios.close();
    baos.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

    BufferedImage dst = ImageIO.read(bais);

    checkResult(src, dst);
}
 
Example 10
Source File: RLECompressionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void doTest(int type) throws IOException {
    BufferedImage src = getTestImage(type);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    writer.setOutput(ios);

    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    switch(type) {
        case TEST_RLE8:
            wparam.setCompressionType("BI_RLE8");
            break;
        case TEST_RLE4:
            wparam.setCompressionType("BI_RLE4");
            break;
        default:
            throw new IllegalArgumentException("Wrong test type: " + type);
    }

    writer.write(null, new IIOImage(src, null, null), wparam);

    ios.close();
    baos.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

    BufferedImage dst = ImageIO.read(bais);

    checkResult(src, dst);
}
 
Example 11
Source File: BMPSubsamplingTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
Example 12
Source File: BMPSubsamplingTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
Example 13
Source File: ReaderListenersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(String compression) {
    try {
        BufferedImage img = createTestImage();

        ImageWriter iw = (ImageWriter)
            ImageIO.getImageWritersByFormatName("bmp").next();
        if (iw == null) {
            throw new RuntimeException("No writers for bmp format."
                                       + " Test failed.");
        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression);

        iw.write(null, new IIOImage(img, null, null), param);
        baos.close();

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        ImageReader ir = (ImageReader)
            ImageIO.getImageReadersByFormatName("bmp").next();
        if (ir == null) {
            throw new RuntimeException("No readers for bmp format."
                                       + " Test failed.");
        }

        IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter();
        IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter();
        ir.addIIOReadProgressListener(progressAdapter);
        ir.addIIOReadUpdateListener(updateAdapter);
        ir.setInput(ImageIO.createImageInputStream(bais));
        BufferedImage dst = ir.read(0);

        progressAdapter.checkResults();

        if (!updateAdapter.isImageUpdateUsed) {
            throw new RuntimeException("imageUpdate was not used."
                                       + " Test failed.");
        }
    } catch(IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed");
    }
}
 
Example 14
Source File: ReaderListenersTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(String compression) {
    try {
        BufferedImage img = createTestImage();

        ImageWriter iw = (ImageWriter)
            ImageIO.getImageWritersByFormatName("bmp").next();
        if (iw == null) {
            throw new RuntimeException("No writers for bmp format."
                                       + " Test failed.");
        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression);

        iw.write(null, new IIOImage(img, null, null), param);
        baos.close();

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        ImageReader ir = (ImageReader)
            ImageIO.getImageReadersByFormatName("bmp").next();
        if (ir == null) {
            throw new RuntimeException("No readers for bmp format."
                                       + " Test failed.");
        }

        IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter();
        IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter();
        ir.addIIOReadProgressListener(progressAdapter);
        ir.addIIOReadUpdateListener(updateAdapter);
        ir.setInput(ImageIO.createImageInputStream(bais));
        BufferedImage dst = ir.read(0);

        progressAdapter.checkResults();

        if (!updateAdapter.isImageUpdateUsed) {
            throw new RuntimeException("imageUpdate was not used."
                                       + " Test failed.");
        }
    } catch(IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed");
    }
}
 
Example 15
Source File: RleEncodingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(BufferedImage src,
                           String compressionType,
                           int compressionMode) throws IOException
{

    ImageWriter iw =  (ImageWriter)ImageIO.getImageWritersBySuffix("bmp").next();
    if (iw == null) {
        throw new RuntimeException("No available writer. Test failed.");
    }

    IIOImage iioImg = new IIOImage(src, null, null);
    ImageWriteParam param = iw.getDefaultWriteParam();


    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);

    System.out.println("Compression Type is " + compressionType);
    System.out.println("Compression Mode is " + compressionMode);

    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compressionType);
    if (compressionMode != ImageWriteParam.MODE_EXPLICIT) {
        param.setCompressionMode(compressionMode);
    }
    try {
        iw.write(null, iioImg, param);
    } catch (IOException e) {
        int bpp = src.getColorModel().getPixelSize();
        if (compressionMode == ImageWriteParam.MODE_EXPLICIT) {
            if ((compressionType.equals("BI_RLE4") && bpp != 4)
                || (compressionType.equals("BI_RLE8") && bpp != 8))
            {
                System.out.println("Can not encode "+ bpp+ "bpp image as"
                                  + compressionType);
                return;
            } else {
                throw new RuntimeException("Unable to encode "
                                           + bpp + "bpp image as "
                                           + compressionType
                                           + ". Test failed");
            }
        }
    }
    baos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);

    BufferedImage dst = ImageIO.read(iis);

    int w = src.getWidth();
    int h = src.getHeight();

    Object dstPixel = dst.getRaster().getDataElements(w/2, h/2, null);
    Object srcPixel = src.getRaster().getDataElements(w/2, h/2, null);

    if ( (src.getColorModel().getRed(srcPixel)
          != dst.getColorModel().getRed(dstPixel))
         || (src.getColorModel().getGreen(srcPixel)
             != dst.getColorModel().getGreen(dstPixel))
         || (src.getColorModel().getBlue(srcPixel)
             != dst.getColorModel().getBlue(dstPixel))
         || (src.getColorModel().getAlpha(srcPixel)
             != dst.getColorModel().getAlpha(dstPixel)) ) {

        showPixel(src, w/2, h/2);
        showPixel(dst, w/2, h/2);

        throw new RuntimeException(
            "Colors are different: " +
            Integer.toHexString(src.getColorModel().getRGB(srcPixel))
            + " and " +
            Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));
    }

}
 
Example 16
Source File: BMPSubsamplingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
Example 17
Source File: BMPSubsamplingTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
Example 18
Source File: BMPSubsamplingTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
Example 19
Source File: BMPSubsamplingTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}
 
Example 20
Source File: BMPSubsamplingTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {
    ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    imageWriteParam.setCompressionType(value);
    return imageWriteParam;
}