Hi Android code because I didn’t notice. I was searching for the error and found that without noticing.
This is the Processing version, I assume equal: https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java#L2951
I’ll include here the relevant code:
static byte[] TIFF_HEADER = {
77, 77, 0, 42, 0, 0, 0, 8, 0, 9, 0, -2, 0, 4, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 3, 0, 0, 0, 1,
0, 0, 0, 0, 1, 2, 0, 3, 0, 0, 0, 3, 0, 0, 0, 122, 1, 6, 0, 3, 0,
0, 0, 1, 0, 2, 0, 0, 1, 17, 0, 4, 0, 0, 0, 1, 0, 0, 3, 0, 1, 21,
0, 3, 0, 0, 0, 1, 0, 3, 0, 0, 1, 22, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0,
1, 23, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 8, 0, 8
};
static final String TIFF_ERROR =
"Error: Processing can only read its own TIFF files.";
static protected PImage loadTIFF(byte[] tiff) {
if ((tiff[42] != tiff[102]) || // width/height in both places
(tiff[43] != tiff[103])) {
System.err.println(TIFF_ERROR);
return null;
}
int width =
((tiff[30] & 0xff) << 8) | (tiff[31] & 0xff);
int height =
((tiff[42] & 0xff) << 8) | (tiff[43] & 0xff);
int count =
((tiff[114] & 0xff) << 24) |
((tiff[115] & 0xff) << 16) |
((tiff[116] & 0xff) << 8) |
(tiff[117] & 0xff);
if (count != width * height * 3) {
System.err.println(TIFF_ERROR + " (" + width + ", " + height +")");
return null;
}
// check the rest of the header
for (int i = 0; i < TIFF_HEADER.length; i++) {
if ((i == 30) || (i == 31) || (i == 42) || (i == 43) ||
(i == 102) || (i == 103) ||
(i == 114) || (i == 115) || (i == 116) || (i == 117)) continue;
if (tiff[i] != TIFF_HEADER[i]) {
System.err.println(TIFF_ERROR + " (" + i + ")");
return null;
}
}
So one can see there 3 reasons for the error:
- byte 42 must be equal to 102, byte 43 must be equal to 103.
-
count
must be equal width * height * 3
- most header bytes must match
A different approach: I found this:
Which would easily load lots of image types.
In my sketch I create a code
folder and drop these inside:
common-image-3.5.jar
common-io-3.5.jar
common-lang-3.5.jar
imageio-core-3.5.jar
imageio-metadata-3.5.jar
imageio-tiff-3.5.jar
This program loads and shows the tiff:
import java.awt.image.*;
import javax.imageio.ImageIO;
PImage img;
void setup() {
size(640, 400);
try {
BufferedImage tiff = ImageIO.read(new File("/tmp/a.tif"));
byte[] px = ((DataBufferByte) tiff.getRaster().getDataBuffer()).getData();
img = createImage(tiff.getWidth(), tiff.getHeight(), RGB);
img.loadPixels();
for(int i=0; i<px.length; i+=4) {
img.pixels[i/4] = (px[i+3] << 16) + (px[i+2] << 8) + px[i+1];
}
img.updatePixels();
}
catch(Exception e) {
e.printStackTrace();
}
}
void draw() {
image(img, 0, 0);
}
But I’m doing something wrong. Most of the image looks fine, but the pure bright colors don’t. Left gimp, right Processing.
I’ll let others finish the job