MCPcopy Index your code
hub / github.com/processing/processing / loadImageTGA

Method loadImageTGA

core/src/processing/core/PApplet.java:5764–5952  ·  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

(String filename)

Source from the content-addressed store, hash-verified

5762 * Please help!
5763 */
5764 protected PImage loadImageTGA(String filename) throws IOException {
5765 InputStream is = createInput(filename);
5766 if (is == null) return null;
5767
5768 byte header[] = new byte[18];
5769 int offset = 0;
5770 do {
5771 int count = is.read(header, offset, header.length - offset);
5772 if (count == -1) return null;
5773 offset += count;
5774 } while (offset < 18);
5775
5776 /*
5777 header[2] image type code
5778 2 (0x02) - Uncompressed, RGB images.
5779 3 (0x03) - Uncompressed, black and white images.
5780 10 (0x0A) - Run-length encoded RGB images.
5781 11 (0x0B) - Compressed, black and white images. (grayscale?)
5782
5783 header[16] is the bit depth (8, 24, 32)
5784
5785 header[17] image descriptor (packed bits)
5786 0x20 is 32 = origin upper-left
5787 0x28 is 32 + 8 = origin upper-left + 32 bits
5788
5789 7 6 5 4 3 2 1 0
5790 128 64 32 16 8 4 2 1
5791 */
5792
5793 int format = 0;
5794
5795 if (((header[2] == 3) || (header[2] == 11)) && // B&W, plus RLE or not
5796 (header[16] == 8) && // 8 bits
5797 ((header[17] == 0x8) || (header[17] == 0x28))) { // origin, 32 bit
5798 format = ALPHA;
5799
5800 } else if (((header[2] == 2) || (header[2] == 10)) && // RGB, RLE or not
5801 (header[16] == 24) && // 24 bits
5802 ((header[17] == 0x20) || (header[17] == 0))) { // origin
5803 format = RGB;
5804
5805 } else if (((header[2] == 2) || (header[2] == 10)) &&
5806 (header[16] == 32) &&
5807 ((header[17] == 0x8) || (header[17] == 0x28))) { // origin, 32
5808 format = ARGB;
5809 }
5810
5811 if (format == 0) {
5812 System.err.println("Unknown .tga file format for " + filename);
5813 //" (" + header[2] + " " +
5814 //(header[16] & 0xff) + " " +
5815 //hex(header[17], 2) + ")");
5816 return null;
5817 }
5818
5819 int w = ((header[13] & 0xff) << 8) + (header[12] & 0xff);
5820 int h = ((header[15] & 0xff) << 8) + (header[14] & 0xff);
5821 PImage outgoing = createImage(w, h, format);

Callers 1

loadImageMethod · 0.95

Calls 6

createInputMethod · 0.95
createImageMethod · 0.95
arraycopyMethod · 0.80
readMethod · 0.45
printlnMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected