| 2962 | "Error: Processing can only read its own TIFF files."; |
| 2963 | |
| 2964 | static protected PImage loadTIFF(byte tiff[]) { |
| 2965 | if ((tiff[42] != tiff[102]) || // width/height in both places |
| 2966 | (tiff[43] != tiff[103])) { |
| 2967 | System.err.println(TIFF_ERROR); |
| 2968 | return null; |
| 2969 | } |
| 2970 | |
| 2971 | int width = |
| 2972 | ((tiff[30] & 0xff) << 8) | (tiff[31] & 0xff); |
| 2973 | int height = |
| 2974 | ((tiff[42] & 0xff) << 8) | (tiff[43] & 0xff); |
| 2975 | |
| 2976 | int count = |
| 2977 | ((tiff[114] & 0xff) << 24) | |
| 2978 | ((tiff[115] & 0xff) << 16) | |
| 2979 | ((tiff[116] & 0xff) << 8) | |
| 2980 | (tiff[117] & 0xff); |
| 2981 | if (count != width * height * 3) { |
| 2982 | System.err.println(TIFF_ERROR + " (" + width + ", " + height +")"); |
| 2983 | return null; |
| 2984 | } |
| 2985 | |
| 2986 | // check the rest of the header |
| 2987 | for (int i = 0; i < TIFF_HEADER.length; i++) { |
| 2988 | if ((i == 30) || (i == 31) || (i == 42) || (i == 43) || |
| 2989 | (i == 102) || (i == 103) || |
| 2990 | (i == 114) || (i == 115) || (i == 116) || (i == 117)) continue; |
| 2991 | |
| 2992 | if (tiff[i] != TIFF_HEADER[i]) { |
| 2993 | System.err.println(TIFF_ERROR + " (" + i + ")"); |
| 2994 | return null; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | PImage outgoing = new PImage(width, height, RGB); |
| 2999 | int index = 768; |
| 3000 | count /= 3; |
| 3001 | for (int i = 0; i < count; i++) { |
| 3002 | outgoing.pixels[i] = |
| 3003 | 0xFF000000 | |
| 3004 | (tiff[index++] & 0xff) << 16 | |
| 3005 | (tiff[index++] & 0xff) << 8 | |
| 3006 | (tiff[index++] & 0xff); |
| 3007 | } |
| 3008 | return outgoing; |
| 3009 | } |
| 3010 | |
| 3011 | |
| 3012 | protected boolean saveTIFF(OutputStream output) { |