本文整理汇总了Java中ar.com.hjg.pngj.PngWriter.end方法的典型用法代码示例。如果您正苦于以下问题:Java PngWriter.end方法的具体用法?Java PngWriter.end怎么用?Java PngWriter.end使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ar.com.hjg.pngj.PngWriter
的用法示例。
在下文中一共展示了PngWriter.end方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeMaskedTwoBitPng
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
public static void writeMaskedTwoBitPng(Bitmap blackAndWhiteBitmap, OutputStream stream, boolean transparentBlack)
{
LightBitmap lightBitmap = new LightBitmap(blackAndWhiteBitmap);
ImageInfo imageInfo = new ImageInfo(lightBitmap.getWidth(), lightBitmap.getHeight(), 1, false, true, false);
PngWriter pngWriter = new PngWriter(stream, imageInfo);
PngChunkTRNS transparencyChunk = pngWriter.getMetadata().createTRNSChunk();
transparencyChunk.setGray(transparentBlack ? 0 : 1);
for (int y = 0; y < lightBitmap.getHeight(); y++)
{
ImageLineByte imageLine = new ImageLineByte(imageInfo);
for (int x = 0; x < lightBitmap.getWidth(); x++)
{
int pixel = lightBitmap.getPixel(x, y) & 0x00FFFFFF;
int r = Color.red(pixel);
imageLine.getScanline()[x] = (byte) (r > 255 / 2 ? 1 : 0);
}
pngWriter.writeRow(imageLine, y);
}
pngWriter.end();
}
示例2: drawTile
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
/**
* Draw an elevation image tile from the flat array of "unsigned short"
* pixel values of length tileWidth * tileHeight where each pixel is at: (y
* * tileWidth) + x
*
* @param pixelValues "unsigned short" pixel values of length tileWidth * tileHeight
* @param tileWidth tile width
* @param tileHeight tile height
* @return elevation image tile
*/
public ElevationPngImage drawTile(short[] pixelValues, int tileWidth,
int tileHeight) {
ElevationPngImage image = createImage(tileWidth, tileHeight);
PngWriter writer = image.getWriter();
for (int y = 0; y < tileHeight; y++) {
ImageLineInt row = new ImageLineInt(writer.imgInfo, new int[tileWidth]);
int[] rowLine = row.getScanline();
for (int x = 0; x < tileWidth; x++) {
short pixelValue = pixelValues[(y * tileWidth) + x];
setPixelValue(rowLine, x, pixelValue);
}
writer.writeRow(row);
}
writer.end();
image.flushStream();
return image;
}
示例3: createMap
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
public static void createMap(MapScript script) {
PngWriter map = new PngWriter(createRelativeFile(script.getOutputFile()), new ImageInfo(script.getWidth(), script.getHeight(), 8, false));
map.setShouldCloseStream(true);
System.out.println("Initializing overlays...");
MapOverlay.initOverlays(script);
System.out.println("Initializing scaler...");
MapScaler.initScaler(script, map.imgInfo);
System.out.println("Initializing importer...");
MapImporter.initConverter();
System.out.println("Initializing exporter...");
MapExporter.init(script.getExports());
System.out.println("Creating map...");
MapMerger.mergeTiles(script, map);
System.out.println("Saving map...");
MapScaler.saveScaler();
map.end();
}
示例4: writePngImage
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
/**
* Writes a {@link BufferedImage} to the specified {@link OutputStream} using the PNGJ library
* which is much faster than Java's ImageIO library.
*
* This implementation was copied from
* <a href="https://github.com/leonbloy/pngj/wiki/Snippets">
* https://github.com/leonbloy/pngj/wiki/Snippets
* </a>.
*
* @param bufferedImage image to write.
* @param compressionLevel 0 (no compression) - 9 (max compression)
* @param filterType internal prediction filter type.
* @param outputStream target stream.
*
* @throws IOException
* if the image is not ARGB or it's data buffer contains the wrong number of banks.
*/
public static void writePngImage(final BufferedImage bufferedImage,
final int compressionLevel,
final FilterType filterType,
final OutputStream outputStream)
throws IOException {
if (bufferedImage.getType() != BufferedImage.TYPE_INT_ARGB) {
throw new IOException("invalid image type (" + bufferedImage.getType() +
"), must be BufferedImage.TYPE_INT_ARGB");
}
final ImageInfo imageInfo = new ImageInfo(bufferedImage.getWidth(), bufferedImage.getHeight(), 8, true);
final PngWriter pngWriter = new PngWriter(outputStream, imageInfo);
pngWriter.setCompLevel(compressionLevel);
pngWriter.setFilterType(filterType);
final DataBufferInt dataBuffer =((DataBufferInt) bufferedImage.getRaster().getDataBuffer());
if (dataBuffer.getNumBanks() != 1) {
throw new IOException("invalid number of banks (" + dataBuffer.getNumBanks() + "), must be 1");
}
final SinglePixelPackedSampleModel sampleModel = (SinglePixelPackedSampleModel) bufferedImage.getSampleModel();
final ImageLineInt line = new ImageLineInt(imageInfo);
final int[] data = dataBuffer.getData();
for (int row = 0; row < imageInfo.rows; row++) {
int elem = sampleModel.getOffset(0, row);
for (int col = 0; col < imageInfo.cols; col++) {
final int sample = data[elem++];
ImageLineHelper.setPixelRGBA8(line, col, sample);
}
pngWriter.writeRow(line, row);
}
pngWriter.end();
// // This looked like a nicer option, but only works for DataBufferByte (not DataBufferInt)
// final ImageLineSetARGBbi lines = new ImageLineSetARGBbi(bufferedImage, imageInfo);
// pngWriter.writeRows(lines);
// pngWriter.end();
}
示例5: convertToPNG
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
public void convertToPNG(Dds dds, OutputStream outputStream, String swizzle) throws IOException {
DdsHeader header = dds.getHeader();
FormatDecoder decoder = Decoders.getDecoder(dds);
ImageInfo imageInfo = new ImageInfo(header.getDwWidth(), header.getDwHeight(), 8, true);
PngWriter pngWriter = new PngWriter(outputStream, imageInfo);
ImageLineInt imageLine = new ImageLineInt(imageInfo);
for (int[] ints : decoder) {
swizzle(ints, swizzle);
ImageLineHelper.setPixelsRGBA8(imageLine, ints);
pngWriter.writeRow(imageLine);
}
pngWriter.end();
}
示例6: timePNGJEncode
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
@Test
public void timePNGJEncode() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
boolean grayscale = !indexed && colorModel.getNumColorComponents() == 1;
ImageInfo ii = new ImageInfo(image.getWidth(), image.getHeight(), 8, hasAlpha, grayscale,
indexed);
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(4);
// pw.setDeflaterStrategy(Deflater.NO_COMPRESSION);
pw.setFilterType(ar.com.hjg.pngj.FilterType.getByVal(filterType.getType()));
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getNumComponents();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
palette.setEntry(i, icm.getRed(i), icm.getGreen(i), icm.getBlue(i));
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
alpha[i] = icm.getAlpha(i);
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] png = bos.toByteArray();
collectPng("pngj", png);
}
示例7: timePNGJEncode
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
public void timePNGJEncode(ar.com.hjg.pngj.FilterType filterType, String name) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
boolean grayscale = !indexed && colorModel.getNumColorComponents() == 1;
ImageInfo ii = new ImageInfo(image.getWidth(), image.getHeight(), 8, hasAlpha, grayscale,
indexed);
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(compression);
pw.setFilterType(filterType);
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getNumComponents();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
palette.setEntry(i, icm.getRed(i), icm.getGreen(i), icm.getBlue(i));
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
alpha[i] = icm.getAlpha(i);
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] png = bos.toByteArray();
collectPng(name, png);
}
示例8: encodeIndexedPNG
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
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();
}
示例9: pregenTexture
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
private void pregenTexture(boolean[][] chars) throws IOException {
final int totalChars = this.intervalsTotalSize;
int w = powerOf2((int) (Math.ceil(Math.sqrt(totalChars) * charW)));
int h = powerOf2((int) (Math.ceil(Math.sqrt(totalChars) * charH)));
int maxIndexW = (int) Math.floor(((double) w) / ((double) charW)) - 1;
int maxIndexH = (int) Math.floor(((double) h) / ((double) charH)) - 1;
if (w > h) {
System.out.println("w > h");
h = powerOf2((int) (Math.ceil((((double)totalChars)/((double)(maxIndexW))) * charH)));
maxIndexH = (int) Math.floor(((double) h) / ((double) charH)) - 1;
} else {
System.out.println("w <= h");
w = powerOf2((int) (Math.ceil((((double)totalChars)/((double)(maxIndexH))) * charW)));
maxIndexW = (int) Math.floor(((double) w) / ((double) charW)) - 1;
}
// final int h = powerOf2((int) (Math.ceil(Math.sqrt(totalChars) * charH)));
System.out.println(((int)Math.ceil(Math.sqrt(totalChars) * charW)) + " * " + ((int)Math.ceil(Math.sqrt(totalChars) * charH)) + " --> " + w + " * " + h);
File f = Files.createTempFile("texture-font-", ".png").toFile();
final FileOutputStream outputStream = new FileOutputStream(f);
final ImageInfo imi = new ImageInfo(w, h, 8, true); // 8 bits per channel, alpha
// open image for writing to a output stream
final PngWriter png = new PngWriter(outputStream, imi);
for (int y = 0; y < png.imgInfo.rows; y++) {
ImageLineInt iline = new ImageLineInt(imi);
int[] xValues = new int[imi.cols];
for (int indexX = 0; indexX <= maxIndexW; indexX++) {// this line will be written to all rows
final int charY = (y % charH);
final int indexY = (y - charY)/charH;
final int i = indexY * (maxIndexW+1) + indexX - this.minCharIndex;
boolean[] currentChar;
if (i < totalChars && (currentChar=chars[i]) != null) {
for (int charX = 0; charX < charW; charX++) {
if (i >= 0 & i < totalChars && currentChar != null && currentChar[charX + charY * charW]) {
xValues[indexX * charW + charX] = 0xFFFFFFFF;
}
// ImageLineHelper.setPixelRGBA8(iline, x, color, color, color, color);
}
}
}
ImageLineHelper.setPixelsRGBA8(iline, xValues);
if (y % 10 == 0) {
System.out.println(y + "/" + png.imgInfo.rows);
}
png.writeRow(iline);
}
chars = null;
png.end();
Utils.gc();
try {
memoryWidth = w;
memoryHeight = h;
memoryWidthOfEachColumn = maxIndexW + 1;
textureW = w;
textureH = h;
outputStream.flush();
outputStream.close();
Utils.gc();
this.tmpFont = f;
} catch (GLException | IOException e) {
e.printStackTrace();
}
}
示例10: encodeIndexedPNG
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
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);
}
writer.writeRow(line);
}
writer.end();
return bos.toByteArray();
}
示例11: roundTripPNGJ
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
private void roundTripPNGJ(BufferedImage original, RenderedImage source) throws IOException {
ColorModel colorModel = source.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(source);
int numColorComponents = colorModel.getNumColorComponents();
boolean grayscale = !indexed && numColorComponents < 3;
byte bitDepth = scanlines.getBitDepth();
ImageInfo ii = new ImageInfo(source.getWidth(), source.getHeight(), bitDepth, hasAlpha, grayscale,
indexed);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(4);
pw.setFilterType(ar.com.hjg.pngj.FilterType.FILTER_NONE);
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().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);
pw.getChunksList().queue(transparent);
}
}
for (int row = 0; row < source.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] bytes = bos.toByteArray();
writeToFile(new File("./target/roundTripNone", sourceFile.getName()), bytes);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(bis);
assertEquals(original.getWidth(), image.getWidth());
assertEquals(original.getHeight(), image.getHeight());
// we cannot match in output the image types generated by the CLIB reader, they are not
// really matching the PNG data model
if(!clibAvailable) {
assertEquals(original.getSampleModel(), image.getSampleModel());
assertEquals(original.getColorModel(), image.getColorModel());
}
for (int x = 0; x < original.getWidth(); x++) {
for (int y = 0; y < original.getHeight(); y++) {
int rgbOriginal = original.getRGB(x, y);
int rgbActual = image.getRGB(x, y);
assertEquals("Comparison failed at " + x + "," + y, rgbOriginal, rgbActual);
}
}
}
示例12: timePNGJEncode
import ar.com.hjg.pngj.PngWriter; //导入方法依赖的package包/类
@Test
public void timePNGJEncode() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
boolean hasAlpha = colorModel.hasAlpha();
boolean grayscale = !indexed && colorModel.getNumColorComponents() == 1;
ImageInfo ii = new ImageInfo(image.getWidth(), image.getHeight(), 8, hasAlpha, grayscale,
indexed);
PngWriter pw = new PngWriter(bos, ii);
pw.setCompLevel(compression);
// pw.setDeflaterStrategy(Deflater.NO_COMPRESSION);
pw.setFilterType(filterType);
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = pw.getMetadata().createPLTEChunk();
int ncolors = icm.getNumComponents();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
palette.setEntry(i, icm.getRed(i), icm.getGreen(i), icm.getBlue(i));
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
alpha[i] = icm.getAlpha(i);
}
transparent.setPalletteAlpha(alpha);
pw.getChunksList().queue(transparent);
}
}
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
byte[] png = bos.toByteArray();
collectPng("pngj", png);
}