Exemplo n.º 1
0
 public void write(File out, boolean allowOverwrite) throws IOException {
   final ImageInfo imi =
       new ImageInfo(
           pixelWidth,
           pixelHeight,
           8,
           (4 == bytesPerPixel) ? true : false); // 8 bits per channel, no alpha
   // open image for writing to a output stream
   final OutputStream outs =
       new BufferedOutputStream(IOUtil.getFileOutputStream(out, allowOverwrite));
   try {
     final PngWriter png = new PngWriter(outs, imi);
     // add some optional metadata (chunks)
     png.getMetadata().setDpi(dpi[0], dpi[1]);
     png.getMetadata().setTimeNow(0); // 0 seconds fron now = now
     png.getMetadata().setText(PngChunkTextVar.KEY_Title, "JogAmp PNGImage");
     // png.getMetadata().setText("my key", "my text");
     final boolean hasAlpha = 4 == bytesPerPixel;
     final ImageLine l1 = new ImageLine(imi);
     if (isGLOriented) {
       // start at last pixel at end-of-buffer, reverse read (OpenGL bottom-left -> PNG top-left
       // origin)
       int dataOff =
           (pixelWidth * bytesPerPixel * (pixelHeight - 1))
               + // full lines - 1 line
               ((pixelWidth - 1) * bytesPerPixel); // one line - 1 pixel
       for (int row = 0; row < pixelHeight; row++) {
         int lineOff =
             (pixelWidth - 1)
                 * bytesPerPixel; // start w/ last pixel in line, reverse store (OpenGL bottom-left
                                  // -> PNG top-left origin)
         if (1 == bytesPerPixel) {
           for (int j = pixelWidth - 1; j >= 0; j--) {
             l1.scanline[lineOff--] = data.get(dataOff--); // // Luminance, 1 bytesPerPixel
           }
         } else {
           for (int j = pixelWidth - 1; j >= 0; j--) {
             dataOff = setPixelRGBA8(l1, lineOff, data, dataOff, hasAlpha);
             lineOff -= bytesPerPixel;
           }
         }
         png.writeRow(l1, row);
       }
     } else {
       int dataOff =
           0; // start at first pixel at start-of-buffer, normal read (same origin: top-left)
       for (int row = 0; row < pixelHeight; row++) {
         int lineOff = 0; // start w/ first pixel in line, normal store (same origin: top-left)
         if (1 == bytesPerPixel) {
           for (int j = pixelWidth - 1; j >= 0; j--) {
             l1.scanline[lineOff++] = data.get(dataOff++); // // Luminance, 1 bytesPerPixel
           }
         } else {
           for (int j = pixelWidth - 1; j >= 0; j--) {
             dataOff = setPixelRGBA8(l1, lineOff, data, dataOff, hasAlpha);
             lineOff += bytesPerPixel;
           }
         }
         png.writeRow(l1, row);
       }
     }
     png.end();
   } finally {
     IOUtil.close(outs, false);
   }
 }