Search in sources :

Example 1 with PngChunkPLTE

use of ar.com.hjg.pngj.chunks.PngChunkPLTE in project xDrip by NightscoutFoundation.

the class SimpleImageEncoder method encodeIndexedPNG.

public byte[] encodeIndexedPNG(int[] pixels, int width, int height, boolean color, int bits) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int[] palette = getPalette();
    boolean alpha = Color.alpha(palette[0]) == 0;
    boolean grayscale = !color;
    // Log.d(TAG, "encodeIndexedPNG: color = "+color +", alpha ="+alpha+", grayscale = "+grayscale);
    // ImageInfo imageInfo = new ImageInfo(width, height, bits, alpha, grayscale, color);
    ImageInfo imageInfo = new ImageInfo(width, height, bits, false, grayscale, color);
    PngWriter writer = new PngWriter(bos, imageInfo);
    writer.getPixelsWriter().setDeflaterCompLevel(9);
    if (color) {
        PngChunkPLTE paletteChunk = writer.getMetadata().createPLTEChunk();
        paletteChunk.setNentries(palette.length);
        for (int i = 0; i < palette.length; i++) {
            int c = palette[i];
            paletteChunk.setEntry(i, Color.red(c), Color.green(c), Color.blue(c));
        }
    }
    if (alpha) {
        PngChunkTRNS trnsChunk = writer.getMetadata().createTRNSChunk();
        if (color) {
            trnsChunk.setIndexEntryAsTransparent(0);
        } else {
            trnsChunk.setGray(1);
        }
    } else {
        quantize(pixels, imageInfo.cols);
    }
    ImageLineInt line = new ImageLineInt(imageInfo);
    for (int y = 0; y < imageInfo.rows; y++) {
        int[] lineData = line.getScanline();
        for (int x = 0; x < imageInfo.cols; x++) {
            int pixel = pixels[y * imageInfo.cols + x];
            // lineData[x] = getNearestColorIndex(pixel) ^ (x % 2) ^ (y % 2);
            lineData[x] = getNearestColorIndex(pixel);
        }
        writer.writeRow(line);
    }
    writer.end();
    return bos.toByteArray();
}
Also used : PngChunkTRNS(ar.com.hjg.pngj.chunks.PngChunkTRNS) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageInfo(ar.com.hjg.pngj.ImageInfo) PngChunkPLTE(ar.com.hjg.pngj.chunks.PngChunkPLTE) PngWriter(ar.com.hjg.pngj.PngWriter) ImageLineInt(ar.com.hjg.pngj.ImageLineInt)

Example 2 with PngChunkPLTE

use of ar.com.hjg.pngj.chunks.PngChunkPLTE in project xDrip-plus by jamorham.

the class SimpleImageEncoder method encodeIndexedPNG.

public byte[] encodeIndexedPNG(int[] pixels, int width, int height, boolean color, int bits) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int[] palette = getPalette();
    boolean alpha = Color.alpha(palette[0]) == 0;
    boolean grayscale = !color;
    // Log.d(TAG, "encodeIndexedPNG: color = "+color +", alpha ="+alpha+", grayscale = "+grayscale);
    // ImageInfo imageInfo = new ImageInfo(width, height, bits, alpha, grayscale, color);
    ImageInfo imageInfo = new ImageInfo(width, height, bits, false, grayscale, color);
    PngWriter writer = new PngWriter(bos, imageInfo);
    writer.getPixelsWriter().setDeflaterCompLevel(9);
    if (color) {
        PngChunkPLTE paletteChunk = writer.getMetadata().createPLTEChunk();
        paletteChunk.setNentries(palette.length);
        for (int i = 0; i < palette.length; i++) {
            int c = palette[i];
            paletteChunk.setEntry(i, Color.red(c), Color.green(c), Color.blue(c));
        }
    }
    if (alpha) {
        PngChunkTRNS trnsChunk = writer.getMetadata().createTRNSChunk();
        if (color) {
            trnsChunk.setIndexEntryAsTransparent(0);
        } else {
            trnsChunk.setGray(1);
        }
    } else {
        quantize(pixels, imageInfo.cols);
    }
    ImageLineInt line = new ImageLineInt(imageInfo);
    for (int y = 0; y < imageInfo.rows; y++) {
        int[] lineData = line.getScanline();
        for (int x = 0; x < imageInfo.cols; x++) {
            int pixel = pixels[y * imageInfo.cols + x];
            // lineData[x] = getNearestColorIndex(pixel) ^ (x % 2) ^ (y % 2);
            lineData[x] = getNearestColorIndex(pixel);
        }
        writer.writeRow(line);
    }
    writer.end();
    return bos.toByteArray();
}
Also used : PngChunkTRNS(ar.com.hjg.pngj.chunks.PngChunkTRNS) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageInfo(ar.com.hjg.pngj.ImageInfo) PngChunkPLTE(ar.com.hjg.pngj.chunks.PngChunkPLTE) PngWriter(ar.com.hjg.pngj.PngWriter) ImageLineInt(ar.com.hjg.pngj.ImageLineInt)

Example 3 with PngChunkPLTE

use of ar.com.hjg.pngj.chunks.PngChunkPLTE in project imageio-ext by geosolutions-it.

the class PNGWriter method writePNG.

public RenderedImage writePNG(RenderedImage image, OutputStream outStream, float quality, FilterType filterType, Map<String, String> text) throws Exception {
    // compute the compression level similarly to what the Clib code does
    int level = Math.round(9 * (1f - quality));
    // get the optimal scanline provider for this image
    RenderedImage original = image;
    ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
    if (scanlines == null) {
        throw new IllegalArgumentException("Could not find a scanline extractor for " + original);
    }
    // encode using the PNGJ library and the GeoServer own scanline providers
    ColorModel colorModel = image.getColorModel();
    boolean indexed = colorModel instanceof IndexColorModel;
    ImageInfo ii = getImageInfo(image, scanlines, colorModel, indexed);
    PngWriter pw = new PngWriter(outStream, ii);
    pw.setShouldCloseStream(false);
    try {
        pw.setCompLevel(level);
        pw.setFilterType(filterType);
        ChunksListForWrite chunkList = pw.getChunksList();
        PngMetadata metadata = pw.getMetadata();
        if (indexed) {
            IndexColorModel icm = (IndexColorModel) colorModel;
            PngChunkPLTE palette = metadata.createPLTEChunk();
            int ncolors = icm.getMapSize();
            palette.setNentries(ncolors);
            for (int i = 0; i < ncolors; i++) {
                final int red = icm.getRed(i);
                final int green = icm.getGreen(i);
                final int blue = icm.getBlue(i);
                palette.setEntry(i, red, green, blue);
            }
            if (icm.hasAlpha()) {
                PngChunkTRNS transparent = new PngChunkTRNS(ii);
                int[] alpha = new int[ncolors];
                for (int i = 0; i < ncolors; i++) {
                    final int a = icm.getAlpha(i);
                    alpha[i] = a;
                }
                transparent.setPalletteAlpha(alpha);
                chunkList.queue(transparent);
            }
        }
        if (text != null && !text.isEmpty()) {
            Iterator<Entry<String, String>> entrySetIterator = text.entrySet().iterator();
            while (entrySetIterator.hasNext()) {
                Entry<String, String> entrySet = entrySetIterator.next();
                metadata.setText(entrySet.getKey(), entrySet.getValue(), true, false);
            }
        }
        // write out the actual image lines
        for (int row = 0; row < image.getHeight(); row++) {
            pw.writeRow(scanlines);
        }
        pw.end();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to encode the PNG", e);
        throw e;
    } finally {
        pw.close();
    }
    return image;
}
Also used : PngWriter(ar.com.hjg.pngj.PngWriter) PngMetadata(ar.com.hjg.pngj.chunks.PngMetadata) Entry(java.util.Map.Entry) ChunksListForWrite(ar.com.hjg.pngj.chunks.ChunksListForWrite) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) PngChunkTRNS(ar.com.hjg.pngj.chunks.PngChunkTRNS) RenderedImage(java.awt.image.RenderedImage) ImageInfo(ar.com.hjg.pngj.ImageInfo) PngChunkPLTE(ar.com.hjg.pngj.chunks.PngChunkPLTE) IndexColorModel(java.awt.image.IndexColorModel)

Example 4 with PngChunkPLTE

use of ar.com.hjg.pngj.chunks.PngChunkPLTE in project J2ME-Loader by nikita36078.

the class PNGUtils method fixPNG.

private static Bitmap fixPNG(InputStream stream) throws IOException {
    PngReaderInt reader = new PngReaderInt(stream);
    reader.setCrcCheckDisabled();
    ImageInfo imageInfo = reader.imgInfo;
    int width = imageInfo.cols;
    int height = imageInfo.rows;
    PngChunkTRNS trns = reader.getMetadata().getTRNS();
    PngChunkPLTE plte = reader.getMetadata().getPLTE();
    ImageLineSetDefault<ImageLineInt> lineSet = (ImageLineSetDefault) reader.readRows();
    int[] pix = new int[width * height];
    int[] buf = new int[width];
    for (int i = 0; i < height; i++) {
        ImageLineInt lineInt = lineSet.getImageLine(i);
        ImageLineHelper.scaleUp(lineInt);
        int[] r = lineToARGB32(lineInt, plte, trns, buf);
        for (int j = 0; j < width; j++) {
            pix[i * width + j] = r[j];
        }
    }
    reader.end();
    return Bitmap.createBitmap(pix, width, height, Bitmap.Config.ARGB_8888);
}
Also used : ImageLineSetDefault(ar.com.hjg.pngj.ImageLineSetDefault) PngChunkTRNS(ar.com.hjg.pngj.chunks.PngChunkTRNS) ImageInfo(ar.com.hjg.pngj.ImageInfo) PngChunkPLTE(ar.com.hjg.pngj.chunks.PngChunkPLTE) PngReaderInt(ar.com.hjg.pngj.PngReaderInt) ImageLineInt(ar.com.hjg.pngj.ImageLineInt)

Aggregations

ImageInfo (ar.com.hjg.pngj.ImageInfo)4 PngChunkPLTE (ar.com.hjg.pngj.chunks.PngChunkPLTE)4 PngChunkTRNS (ar.com.hjg.pngj.chunks.PngChunkTRNS)4 ImageLineInt (ar.com.hjg.pngj.ImageLineInt)3 PngWriter (ar.com.hjg.pngj.PngWriter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImageLineSetDefault (ar.com.hjg.pngj.ImageLineSetDefault)1 PngReaderInt (ar.com.hjg.pngj.PngReaderInt)1 ChunksListForWrite (ar.com.hjg.pngj.chunks.ChunksListForWrite)1 PngMetadata (ar.com.hjg.pngj.chunks.PngMetadata)1 ColorModel (java.awt.image.ColorModel)1 IndexColorModel (java.awt.image.IndexColorModel)1 RenderedImage (java.awt.image.RenderedImage)1 Entry (java.util.Map.Entry)1