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();
}
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();
}
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;
}
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);
}
Aggregations