Reading PNG pixels using PNGJ without BufferedImage

I am trying to make equivalent code using PNGJ. I do not want to use ImageIO.readand BufferedImage.getRGB. Instead, I want to get pixels using PNGJ. The code below was my version of ImageIO + BufferedImage.

BufferedImage bi = ImageIO.read( imageFile );
...
int[] pixels = new int[arrSize];
bi.getRGB( 0, 0, width, height, pixels, 0, width );

I need something like PNGJ

lint = (ImageLineInt) reader.readRow();
scanLine = lint.getScanline();
...
???

Here are my test images:
ImageInfo [cols = 1530, rows = 1980, bitDepth = 8, channels = 3, bitspPixel = 24, bytesPixel = 3, bytesPerRow = 4590, samplesPerRow = 4590, samplesPerRowP = 4590, alpha = false, greyscale = false , indexed = false, packed = false]

+4
source share
3 answers

Zek, PNGJ BufferedImage: , ( PNG), , PNG .

, , , PNG- RGB, RGB () . PNG , ( , ). PNG , . Etc.

( RGB, ), ImageLineHelper . , convert2rgba() getPixelARGB8().

, - , - . RGB, - PNG , , ... (, 2 , 16 ? -, ... ..)? PNG, .

(, ... BufferedImage , , , ? )

(RGB, 8 , ) ( TRNS -, ), :

 for (int i = 0,j=0 ; i <  reader.imgInfo.cols; i++, j+=channels ) {
      int r = scanLine[j];
      int g = scanLine[j+1];
      int b = scanLine[j+2];
     // ...
 }

: PNGJ.

+4

, . https://code.google.com/p/pngj/wiki/FAQ

/ ? PRRReader readRow() IImageLine, PNG; . (PngReader PngReaderInt) ImageLineInt. scaline (getScanline()), int []. ; ( ) x .

- ImageLineByte, , , (: , 8- ; : , 16 , , Java).

:

RGB/RGBA RGB (A): R G B R G B... ( ) R G B A R G B A... ( ); : 0-255, = 8 (0-65535, - = 16, 0-15, - = 4 ..). , I.... G/GA G G G... ( ) G A G A... ( ).

, , BufferedImage ( Transparency.TRANSLUCENT) :

PngReader reader = new PngReader( inputPNGFile );
arrSize = (int) reader.imgInfo.getTotalPixels();

width = reader.imgInfo.cols;
height = reader.imgInfo.rows;
channels = reader.imgInfo.channels;    

ImageLineInt lint;
while ( reader.hasMoreRows() ) {
    lint = (ImageLineInt) reader.readRow();
    scanLine = lint.getScanline();

    for ( i = 0; i < width; i++ ) {
        offset = i * channels;

        // Adjust the following code depending on your source image.
        // I need the to set the alpha channel to 0xFF000000 since my destination image
        // is TRANSLUCENT : BufferedImage bi = CONFIG.createCompatibleImage( width, height, Transparency.TRANSLUCENT );
        // my source was 3 channels RGB without transparency
        nextPixel = ( scanLine[offset] << 16 ) | ( scanLine[offset + 1] << 8 ) | ( scanLine[offset + 2] ) | 0xFF000000;

        // I'm placing the pixels on a memory mapped file
        mem.putInt( nextPixel ); 
     }

}
+2

, BufferedImage. RGB . DataBuffer.

BufferedImage bi = ImageIO.read( imageFile );
byte[] pixels = ((DataBufferByte)bi.getRaster().getDataBuffer()).getData() ;

(Byte, Int Short).

0

Source: https://habr.com/ru/post/1616179/


All Articles