Skip to content

Commit

Permalink
PNGJ: Bump to git sha1 a0b1101ba2d37de39428ed55c8189502e24a3125 of ht…
Browse files Browse the repository at this point in the history
  • Loading branch information
sgothel committed Jul 2, 2013
1 parent 35fcf49 commit 51427b9
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 157 deletions.
24 changes: 17 additions & 7 deletions src/jogl/classes/jogamp/opengl/util/pngj/FilterType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jogamp.opengl.util.pngj;

import java.util.HashMap;

/**
* Internal PNG predictor filter, or strategy to select it.
*
Expand All @@ -26,15 +28,18 @@ public enum FilterType {
*/
FILTER_PAETH(4),
/**
* Default strategy: select one of the above filters depending on global image parameters
* Default strategy: select one of the above filters depending on global
* image parameters
*/
FILTER_DEFAULT(-1),
/**
* Aggressive strategy: select one of the above filters trying each of the filters (every 8 rows)
* Aggressive strategy: select one of the above filters trying each of the
* filters (every 8 rows)
*/
FILTER_AGGRESSIVE(-2),
/**
* Very aggressive strategy: select one of the above filters trying each of the filters (for every row!)
* Very aggressive strategy: select one of the above filters trying each of
* the filters (for every row!)
*/
FILTER_VERYAGGRESSIVE(-3),
/**
Expand All @@ -52,12 +57,17 @@ private FilterType(int val) {
this.val = val;
}

public static FilterType getByVal(int i) {
private static HashMap<Integer, FilterType> byVal;

static {
byVal = new HashMap<Integer, FilterType>();
for (FilterType ft : values()) {
if (ft.val == i)
return ft;
byVal.put(ft.val, ft);
}
return null;
}

public static FilterType getByVal(int i) {
return byVal.get(i);
}

}
26 changes: 16 additions & 10 deletions src/jogl/classes/jogamp/opengl/util/pngj/ImageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Simple immutable wrapper for basic image info.
* <p>
* Some parameters are redundant, but the constructor receives an 'orthogonal' subset.
* Some parameters are redundant, but the constructor receives an 'orthogonal'
* subset.
* <p>
* ref: http://www.w3.org/TR/PNG/#11IHDR
*/
Expand All @@ -23,14 +24,15 @@ public class ImageInfo {
public final int rows;

/**
* Bits per sample (per channel) in the buffer (1-2-4-8-16). This is 8-16 for RGB/ARGB images, 1-2-4-8 for
* grayscale. For indexed images, number of bits per palette index (1-2-4-8)
* Bits per sample (per channel) in the buffer (1-2-4-8-16). This is 8-16
* for RGB/ARGB images, 1-2-4-8 for grayscale. For indexed images, number of
* bits per palette index (1-2-4-8)
*/
public final int bitDepth;

/**
* Number of channels, as used internally: 3 for RGB, 4 for RGBA, 2 for GA (gray with alpha), 1 for grayscale or
* indexed.
* Number of channels, as used internally: 3 for RGB, 4 for RGBA, 2 for GA
* (gray with alpha), 1 for grayscale or indexed.
*/
public final int channels;

Expand All @@ -50,7 +52,8 @@ public class ImageInfo {
public final boolean indexed;

/**
* Flag: true if image internally uses less than one byte per sample (bit depth 1-2-4)
* Flag: true if image internally uses less than one byte per sample (bit
* depth 1-2-4)
*/
public final boolean packed;

Expand All @@ -75,10 +78,12 @@ public class ImageInfo {
public final int samplesPerRow;

/**
* Amount of "packed samples" : when several samples are stored in a single byte (bitdepth 1,2 4) they are counted
* as one "packed sample". This is less that samplesPerRow only when bitdepth is 1-2-4 (flag packed = true)
* Amount of "packed samples" : when several samples are stored in a single
* byte (bitdepth 1,2 4) they are counted as one "packed sample". This is
* less that samplesPerRow only when bitdepth is 1-2-4 (flag packed = true)
* <p>
* This equals the number of elements in the scanline array if working with packedMode=true
* This equals the number of elements in the scanline array if working with
* packedMode=true
* <p>
* For internal use, client code should rarely access this.
*/
Expand All @@ -99,7 +104,8 @@ public ImageInfo(int cols, int rows, int bitdepth, boolean alpha) {
* @param rows
* Height in pixels
* @param bitdepth
* Bits per sample, in the buffer : 8-16 for RGB true color and greyscale
* Bits per sample, in the buffer : 8-16 for RGB true color and
* greyscale
* @param alpha
* Flag: has an alpha channel (RGBA or GA)
* @param grayscale
Expand Down
42 changes: 27 additions & 15 deletions src/jogl/classes/jogamp/opengl/util/pngj/ImageLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/**
* Lightweight wrapper for an image scanline, used for read and write.
* <p>
* This object can be (usually it is) reused while iterating over the image lines.
* This object can be (usually it is) reused while iterating over the image
* lines.
* <p>
* See <code>scanline</code> field, to understand the format.
*/
Expand All @@ -18,21 +19,25 @@ public class ImageLine {
private int rown = 0;

/**
* The 'scanline' is an array of integers, corresponds to an image line (row).
* The 'scanline' is an array of integers, corresponds to an image line
* (row).
* <p>
* Except for 'packed' formats (gray/indexed with 1-2-4 bitdepth) each <code>int</code> is a "sample" (one for
* channel), (0-255 or 0-65535) in the corresponding PNG sequence: <code>R G B R G B...</code> or
* Except for 'packed' formats (gray/indexed with 1-2-4 bitdepth) each
* <code>int</code> is a "sample" (one for channel), (0-255 or 0-65535) in
* the corresponding PNG sequence: <code>R G B R G B...</code> or
* <code>R G B A R G B A...</tt>
* or <code>g g g ...</code> or <code>i i i</code> (palette index)
* <p>
* For bitdepth=1/2/4 , and if samplesUnpacked=false, each value is a PACKED byte!
* For bitdepth=1/2/4 , and if samplesUnpacked=false, each value is a PACKED
* byte!
* <p>
* To convert a indexed line to RGB balues, see <code>ImageLineHelper.palIdx2RGB()</code> (you can't do the reverse)
* To convert a indexed line to RGB balues, see
* <code>ImageLineHelper.palIdx2RGB()</code> (you can't do the reverse)
*/
public final int[] scanline;
/**
* Same as {@link #scanline}, but with one byte per sample. Only one of scanline and scanlineb is valid - this
* depends on {@link #sampleType}
* Same as {@link #scanline}, but with one byte per sample. Only one of
* scanline and scanlineb is valid - this depends on {@link #sampleType}
*/
public final byte[] scanlineb;

Expand All @@ -53,10 +58,11 @@ public enum SampleType {
public final SampleType sampleType;

/**
* true: each element of the scanline array represents a sample always, even for internally packed PNG formats
* true: each element of the scanline array represents a sample always, even
* for internally packed PNG formats
*
* false: if the original image was of packed type (bit depth less than 8) we keep samples packed in a single array
* element
* false: if the original image was of packed type (bit depth less than 8)
* we keep samples packed in a single array element
*/
public final boolean samplesUnpacked;

Expand All @@ -70,11 +76,14 @@ public ImageLine(ImageInfo imgInfo) {
/**
*
* @param imgInfo
* Inmutable ImageInfo, basic parameter of the image we are reading or writing
* Inmutable ImageInfo, basic parameter of the image we are
* reading or writing
* @param stype
* INT or BYTE : this determines which scanline is the really used one
* INT or BYTE : this determines which scanline is the really
* used one
* @param unpackedMode
* If true, we use unpacked format, even for packed original images
* If true, we use unpacked format, even for packed original
* images
*
*/
public ImageLine(ImageInfo imgInfo, SampleType stype, boolean unpackedMode) {
Expand Down Expand Up @@ -226,7 +235,10 @@ static void unpackInplaceByte(final ImageInfo iminfo, final byte[] src, final by
}
}

/** size original: samplesPerRow sizeFinal: samplesPerRowPacked (trailing elements are trash!) **/
/**
* size original: samplesPerRow sizeFinal: samplesPerRowPacked (trailing
* elements are trash!)
**/
static void packInplaceByte(final ImageInfo iminfo, final byte[] src, final byte[] dst, final boolean scaled) {
final int bitDepth = iminfo.bitDepth;
if (bitDepth >= 8)
Expand Down
32 changes: 21 additions & 11 deletions src/jogl/classes/jogamp/opengl/util/pngj/ImageLineHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import jogamp.opengl.util.pngj.chunks.PngChunkTRNS;

/**
* Bunch of utility static methods to process/analyze an image line at the pixel level.
* Bunch of utility static methods to process/analyze an image line at the pixel
* level.
* <p>
* Not essential at all, some methods are probably to be removed if future releases.
* Not essential at all, some methods are probably to be removed if future
* releases.
* <p>
* WARNING: most methods for getting/setting values work currently only for integer base imageLines
* WARNING: most methods for getting/setting values work currently only for
* integer base imageLines
*/
public class ImageLineHelper {

Expand All @@ -18,15 +21,17 @@ public class ImageLineHelper {
private final static double BIG_VALUE_NEG = Double.MAX_VALUE * (-0.5);

/**
* Given an indexed line with a palette, unpacks as a RGB array, or RGBA if a non nul PngChunkTRNS chunk is passed
* Given an indexed line with a palette, unpacks as a RGB array, or RGBA if
* a non nul PngChunkTRNS chunk is passed
*
* @param line
* ImageLine as returned from PngReader
* @param pal
* Palette chunk
* @param buf
* Preallocated array, optional
* @return R G B (A), one sample 0-255 per array element. Ready for pngw.writeRowInt()
* @return R G B (A), one sample 0-255 per array element. Ready for
* pngw.writeRowInt()
*/
public static int[] palette2rgb(ImageLine line, PngChunkPLTE pal, PngChunkTRNS trns, int[] buf) {
boolean isalpha = trns != null;
Expand All @@ -53,9 +58,12 @@ public static int[] palette2rgb(ImageLine line, PngChunkPLTE pal, int[] buf) {
return palette2rgb(line, pal, null, buf);
}

/** what follows is pretty uninteresting/untested/obsolete, subject to change */
/**
* Just for basic info or debugging. Shows values for first and last pixel. Does not include alpha
* what follows is pretty uninteresting/untested/obsolete, subject to change
*/
/**
* Just for basic info or debugging. Shows values for first and last pixel.
* Does not include alpha
*/
public static String infoFirstLastPixels(ImageLine line) {
return line.imgInfo.channels == 1 ? String.format("first=(%d) last=(%d)", line.scanline[0],
Expand All @@ -71,8 +79,8 @@ public static String infoFull(ImageLine line) {
}

/**
* Computes some statistics for the line. Not very efficient or elegant, mainly for tests. Only for RGB/RGBA Outputs
* values as doubles (0.0 - 1.0)
* Computes some statistics for the line. Not very efficient or elegant,
* mainly for tests. Only for RGB/RGBA Outputs values as doubles (0.0 - 1.0)
*/
static class ImageLineStats {
public double[] prom = { 0.0, 0.0, 0.0, 0.0 }; // channel averages
Expand Down Expand Up @@ -237,9 +245,11 @@ public static int clampTo_128_127(int x) {
/**
* Unpacks scanline (for bitdepth 1-2-4) into a array <code>int[]</code>
* <p>
* You can (OPTIONALLY) pass an preallocated array, that will be filled and returned. If null, it will be allocated
* You can (OPTIONALLY) pass an preallocated array, that will be filled and
* returned. If null, it will be allocated
* <p>
* If <code>scale==true<code>, it scales the value (just a bit shift) towards 0-255.
* If
* <code>scale==true<code>, it scales the value (just a bit shift) towards 0-255.
* <p>
* You probably should use {@link ImageLine#unpackToNewImageLine()}
*
Expand Down
24 changes: 15 additions & 9 deletions src/jogl/classes/jogamp/opengl/util/pngj/ImageLines.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import jogamp.opengl.util.pngj.ImageLine.SampleType;

/**
* Wraps in a matrix a set of image rows, not necessarily contiguous - but equispaced.
* Wraps in a matrix a set of image rows, not necessarily contiguous - but
* equispaced.
*
* The fields mirrors those of {@link ImageLine}, and you can access each row as a ImageLine backed by the matrix row,
* see {@link #getImageLineAtMatrixRow(int)}
* The fields mirrors those of {@link ImageLine}, and you can access each row as
* a ImageLine backed by the matrix row, see
* {@link #getImageLineAtMatrixRow(int)}
*/
public class ImageLines {

Expand All @@ -23,7 +25,8 @@ public class ImageLines {
public final byte[][] scanlinesb;

/**
* Allocates a matrix to store {@code nRows} image rows. See {@link ImageLine} and {@link PngReader#readRowsInt()}
* Allocates a matrix to store {@code nRows} image rows. See
* {@link ImageLine} and {@link PngReader#readRowsInt()}
* {@link PngReader#readRowsByte()}
*
* @param imgInfo
Expand Down Expand Up @@ -54,8 +57,9 @@ public ImageLines(ImageInfo imgInfo, SampleType stype, boolean unpackedMode, int
}

/**
* Warning: this always returns a valid matrix row (clamping on 0 : nrows-1, and rounding down) Eg:
* rowOffset=4,rowStep=2 imageRowToMatrixRow(17) returns 6 , imageRowToMatrixRow(1) returns 0
* Warning: this always returns a valid matrix row (clamping on 0 : nrows-1,
* and rounding down) Eg: rowOffset=4,rowStep=2 imageRowToMatrixRow(17)
* returns 6 , imageRowToMatrixRow(1) returns 0
*/
public int imageRowToMatrixRow(int imrow) {
int r = (imrow - rowOffset) / rowStep;
Expand Down Expand Up @@ -86,9 +90,11 @@ public int matrixRowToImageRow(int mrow) {
* Returns a ImageLine is backed by the matrix, no allocation done
*
* @param mrow
* Matrix row, from 0 to nRows This is not necessarily the image row, see
* {@link #imageRowToMatrixRow(int)} and {@link #matrixRowToImageRow(int)}
* @return A new ImageLine, backed by the matrix, with the correct ('real') rownumber
* Matrix row, from 0 to nRows This is not necessarily the image
* row, see {@link #imageRowToMatrixRow(int)} and
* {@link #matrixRowToImageRow(int)}
* @return A new ImageLine, backed by the matrix, with the correct ('real')
* rownumber
*/
public ImageLine getImageLineAtMatrixRow(int mrow) {
if (mrow < 0 || mrow > nRows)
Expand Down
23 changes: 14 additions & 9 deletions src/jogl/classes/jogamp/opengl/util/pngj/PngHelperInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,23 @@ public static void readBytes(InputStream is, byte[] b, int offset, int len) {
}
}

public static void skipBytes(InputStream is, int len) {
byte[] buf = new byte[8192 * 4];
int read, remain = len;
public static void skipBytes(InputStream is, long len) {
try {
while (remain > 0) {
read = is.read(buf, 0, remain > buf.length ? buf.length : remain);
if (read < 0)
throw new PngjInputException("error reading (skipping) : EOF");
remain -= read;
while (len > 0) {
long n1 = is.skip(len);
if (n1 > 0) {
len -= n1;
} else if (n1 == 0) { // should we retry? lets read one byte
if (is.read() == -1) // EOF
break;
else
len--;
} else
// negative? this should never happen but...
throw new IOException("skip() returned a negative value ???");
}
} catch (IOException e) {
throw new PngjInputException("error reading (skipping)", e);
throw new PngjInputException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private IdatChunkInfo(int len, long offset) {
List<IdatChunkInfo> foundChunksInfo = new ArrayList<IdatChunkInfo>();

/**
* Constructor must be called just after reading length and id of first IDAT chunk
* Constructor must be called just after reading length and id of first IDAT
* chunk
**/
PngIDatChunkInputStream(InputStream iStream, int lenFirstChunk, long offset) {
this.offset = offset;
Expand Down Expand Up @@ -95,7 +96,8 @@ private void endChunkGoForNext() {
}

/**
* sometimes last row read does not fully consumes the chunk here we read the reamaing dummy bytes
* sometimes last row read does not fully consumes the chunk here we read
* the reamaing dummy bytes
*/
void forceChunkEnd() {
if (!ended) {
Expand All @@ -108,7 +110,8 @@ void forceChunkEnd() {
}

/**
* This can return less than len, but never 0 Returns -1 if "pseudo file" ended prematurely. That is our error.
* This can return less than len, but never 0 Returns -1 if "pseudo file"
* ended prematurely. That is our error.
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
Expand Down
Loading

0 comments on commit 51427b9

Please sign in to comment.