MCPcopy Create free account
hub / github.com/benfry/processing4 / loadTGA

Method loadTGA

core/src/processing/core/PImage.java:2820–2995  ·  view source on GitHub ↗

Targa image loader for RLE-compressed TGA files. Rewritten for 0115 to read/write RLE-encoded targa images. For 0125, non-RLE encoded images are now supported, along with images whose y-order is reversed (which is standard for TGA files). A version of this function is in MovieMaker.java. Any

(InputStream input)

Source from the content-addressed store, hash-verified

2818 * Please help!
2819 */
2820 static public PImage loadTGA(InputStream input) throws IOException { // ignore
2821 byte[] header = new byte[18];
2822 int offset = 0;
2823 do {
2824 int count = input.read(header, offset, header.length - offset);
2825 if (count == -1) return null;
2826 offset += count;
2827 } while (offset < 18);
2828
2829 /*
2830 header[2] image type code
2831 2 (0x02) - Uncompressed, RGB images.
2832 3 (0x03) - Uncompressed, black and white images.
2833 10 (0x0A) - Run-length encoded RGB images.
2834 11 (0x0B) - Compressed, black and white images. (grayscale?)
2835
2836 header[16] is the bit depth (8, 24, 32)
2837
2838 header[17] image descriptor (packed bits)
2839 0x20 is 32 = origin upper-left
2840 0x28 is 32 + 8 = origin upper-left + 32 bits
2841
2842 7 6 5 4 3 2 1 0
2843 128 64 32 16 8 4 2 1
2844 */
2845
2846 int format = 0;
2847
2848 if (((header[2] == 3) || (header[2] == 11)) && // B&W, plus RLE or not
2849 (header[16] == 8) && // 8 bits
2850 ((header[17] == 0x8) || (header[17] == 0x28))) { // origin, 32 bit
2851 format = ALPHA;
2852
2853 } else if (((header[2] == 2) || (header[2] == 10)) && // RGB, RLE or not
2854 (header[16] == 24) && // 24 bits
2855 ((header[17] == 0x20) || (header[17] == 0))) { // origin
2856 format = RGB;
2857
2858 } else if (((header[2] == 2) || (header[2] == 10)) &&
2859 (header[16] == 32) &&
2860 ((header[17] == 0x8) || (header[17] == 0x28))) { // origin, 32
2861 format = ARGB;
2862 }
2863
2864 if (format == 0) {
2865 System.err.println("Unknown .tga file format");
2866 return null;
2867 }
2868
2869 int w = ((header[13] & 0xff) << 8) + (header[12] & 0xff);
2870 int h = ((header[15] & 0xff) << 8) + (header[14] & 0xff);
2871 PImage outgoing = new PImage(w, h, format);
2872
2873 // where "reversed" means upper-left corner (normal for most of
2874 // the modernized world, but "reversed" for the tga spec)
2875 //boolean reversed = (header[17] & 0x20) != 0;
2876 // https://github.com/processing/processing/issues/1682
2877 boolean reversed = (header[17] & 0x20) == 0;

Callers 1

loadImageMethod · 0.95

Calls 4

arraycopyMethod · 0.80
readMethod · 0.45
printlnMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected