mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
support for reading tga images created by p5
This commit is contained in:
+113
-28
@@ -3024,16 +3024,22 @@ public class PApplet extends Applet
|
||||
* <A HREF="http://java.sun.com/j2se/1.3/docs/guide/2d/new_features.html">
|
||||
* also supported</A>.
|
||||
* <P>
|
||||
* Generally this should only be used during setup, re-loading
|
||||
* images inside draw() is likely to cause a significant delay
|
||||
* while memory is allocated and the thread blocks while waiting
|
||||
* Generally, loadImage() should only be used during setup, because
|
||||
* re-loading images inside draw() is likely to cause a significant
|
||||
* delay while memory is allocated and the thread blocks while waiting
|
||||
* for the image to load because loading is not asynchronous.
|
||||
* <P>
|
||||
* To load several images asynchronously, see more information in the
|
||||
* FAQ about writing your own threaded image loading method.
|
||||
* <P>
|
||||
* As of 0096, returns null if no image of that name is found,
|
||||
* rather than an error.
|
||||
* <P>
|
||||
* Release 0115 also provides support for reading TIFF images
|
||||
* that are created by Processing.
|
||||
* Release 0115 also provides support for reading TIFF and RLE-encoded
|
||||
* Targa (.tga) files written by Processing via save() and saveFrame().
|
||||
* Other TIFF and Targa files will probably not load, use a different
|
||||
* format (gif, jpg and png are safest bets) when creating images with
|
||||
* another application to use with Processing.
|
||||
* <P>
|
||||
* Also in release 0115, more image formats (BMP and others) can
|
||||
* be read when using Java 1.4 and later. Because many people still
|
||||
@@ -3066,7 +3072,12 @@ public class PApplet extends Applet
|
||||
}
|
||||
|
||||
if (filename.toLowerCase().endsWith(".tga")) {
|
||||
return loadImageTGA(filename);
|
||||
try {
|
||||
return loadImageTGA(filename);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
byte bytes[] = loadBytes(filename);
|
||||
@@ -3163,31 +3174,103 @@ public class PApplet extends Applet
|
||||
|
||||
|
||||
/**
|
||||
* Targa bitmap loader for 24/32bit RGB(A) [toxi 040304]
|
||||
* <p/>
|
||||
* [fry] this could be optimized to not use loadBytes
|
||||
* which would help out memory situations with large images
|
||||
* Targa image loader for RLE-compressed TGA files.
|
||||
* <P>
|
||||
* Rewritten for 0115 to read/write RLE-encoded targa images.
|
||||
*/
|
||||
protected PImage loadImageTGA(String filename) {
|
||||
// load image file as byte array
|
||||
byte[] buffer = loadBytes(filename);
|
||||
if (buffer == null) return null;
|
||||
protected PImage loadImageTGA(String filename) throws IOException {
|
||||
InputStream is = openStream(filename);
|
||||
if (is == null) return null;
|
||||
|
||||
// check if it's a TGA and has 8bits/colour channel
|
||||
//if (buffer[2] == 2 && buffer[17] == 8) {
|
||||
// [toxi20050929] changed format validation
|
||||
// only number of bits/pixel are checked now...
|
||||
if (buffer[2] == 2 && (buffer[16] == 24 || buffer[16]==32 )) {
|
||||
// get image dimensions
|
||||
int w = ((buffer[13] & 0xff) << 8) + (buffer[12] & 0xff);
|
||||
int h = ((buffer[15] & 0xff) << 8) + (buffer[14] & 0xff);
|
||||
// check if image has alpha
|
||||
boolean hasAlpha=(buffer[16] == 32);
|
||||
byte header[] = new byte[18];
|
||||
int offset = 0;
|
||||
do {
|
||||
int count = is.read(header, offset, header.length - offset);
|
||||
if (count == -1) return null;
|
||||
offset += count;
|
||||
} while (offset < 18);
|
||||
|
||||
// setup new image object
|
||||
PImage img = new PImage(w,h);
|
||||
img.format = (hasAlpha ? ARGB : RGB);
|
||||
int format = 0;
|
||||
if ((header[2] == 0x0B) &&
|
||||
(header[16] == 0x08) &&
|
||||
(header[17] == 0x28)) {
|
||||
format = ALPHA;
|
||||
|
||||
} else if ((header[2] == 0x0A) &&
|
||||
(header[16] == 24) &&
|
||||
(header[17] == 0x20)) {
|
||||
format = RGB;
|
||||
|
||||
} else if ((header[2] == 0x0A) &&
|
||||
(header[16] == 32) &&
|
||||
(header[17] == 0x28)) {
|
||||
format = ARGB;
|
||||
}
|
||||
if (format == 0) {
|
||||
System.err.println("Unknown .tga file format for " + filename);
|
||||
return null;
|
||||
}
|
||||
|
||||
// get image dimensions
|
||||
int w = ((header[13] & 0xff) << 8) + (header[12] & 0xff);
|
||||
int h = ((header[15] & 0xff) << 8) + (header[14] & 0xff);
|
||||
PImage outgoing = new PImage(w, h, format);
|
||||
|
||||
int index = 0;
|
||||
int px[] = outgoing.pixels;
|
||||
|
||||
while (index < px.length) {
|
||||
int num = is.read();
|
||||
boolean isRLE = (num & 0x80) != 0;
|
||||
if (isRLE) {
|
||||
num -= 127; // (num & 0x7F) + 1
|
||||
int pixel = 0;
|
||||
switch (format) {
|
||||
case ALPHA:
|
||||
pixel = is.read();
|
||||
break;
|
||||
case RGB:
|
||||
pixel = 0xFF000000 |
|
||||
is.read() | (is.read() << 8) | (is.read() << 16);
|
||||
//(is.read() << 16) | (is.read() << 8) | is.read();
|
||||
break;
|
||||
case ARGB:
|
||||
pixel = is.read() |
|
||||
(is.read() << 8) | (is.read() << 16) | (is.read() << 24);
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < num; i++) {
|
||||
px[index++] = pixel;
|
||||
if (index == px.length) break;
|
||||
}
|
||||
} else { // write up to 127 bytes as uncompressed
|
||||
num += 1;
|
||||
switch (format) {
|
||||
case ALPHA:
|
||||
for (int i = 0; i < num; i++) {
|
||||
px[index++] = is.read();
|
||||
}
|
||||
break;
|
||||
case RGB:
|
||||
for (int i = 0; i < num; i++) {
|
||||
px[index++] = 0xFF000000 |
|
||||
is.read() | (is.read() << 8) | (is.read() << 16);
|
||||
//(is.read() << 16) | (is.read() << 8) | is.read();
|
||||
}
|
||||
break;
|
||||
case ARGB:
|
||||
for (int i = 0; i < num; i++) {
|
||||
px[index++] = is.read() | //(is.read() << 24) |
|
||||
(is.read() << 8) | (is.read() << 16) | (is.read() << 24);
|
||||
//(is.read() << 16) | (is.read() << 8) | is.read();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return outgoing;
|
||||
|
||||
/*
|
||||
// targa's are written upside down, so we need to parse it in reverse
|
||||
int index = (h-1) * w;
|
||||
// actual bitmap data starts at byte 18
|
||||
@@ -3206,9 +3289,11 @@ public class PApplet extends Applet
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
System.err.println("loadImage(): bad targa image format");
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+8
-6
@@ -1919,17 +1919,19 @@ public class PImage implements PConstants, Cloneable {
|
||||
* Also figured out how to avoid parsing the image upside-down
|
||||
* (there's a header flag to set the image origin to top-left)
|
||||
* </p>
|
||||
* New version starting with rev 92 takes format setting into account:
|
||||
* <TT>ALPHA</TT> images written as 8bit grayscale (uses lowest byte)
|
||||
* <TT>RGB</TT> → 24 bits
|
||||
* <TT>ARGB</TT> → 32 bits
|
||||
* Starting with revision 0092, the format setting is taken into account:
|
||||
* <UL>
|
||||
* <LI><TT>ALPHA</TT> images written as 8bit grayscale (uses lowest byte)
|
||||
* <LI><TT>RGB</TT> → 24 bits
|
||||
* <LI><TT>ARGB</TT> → 32 bits
|
||||
* </UL>
|
||||
* all versions are RLE compressed
|
||||
* </p>
|
||||
* Contributed by toxi 8-10 May 2005, based on this RLE
|
||||
* <A HREF="http://www.wotsit.org/download.asp?f=tga">specification</A>
|
||||
*/
|
||||
protected boolean saveTGA(OutputStream output) {
|
||||
byte header[] = new byte[18];
|
||||
byte header[] = new byte[18];
|
||||
|
||||
if (format == ALPHA) { // save ALPHA images as 8bit grayscale
|
||||
header[2] = 0x0B;
|
||||
@@ -2179,7 +2181,7 @@ public class PImage implements PConstants, Cloneable {
|
||||
}
|
||||
if (saveImageFormats != null) {
|
||||
for (int i = 0; i < saveImageFormats.length; i++) {
|
||||
System.out.println(saveImageFormats[i]);
|
||||
//System.out.println(saveImageFormats[i]);
|
||||
if (filename.endsWith("." + saveImageFormats[i])) {
|
||||
saveImageIO(filename);
|
||||
return;
|
||||
|
||||
+9
-10
@@ -8,6 +8,15 @@ X add arraycopy(from, to, count);
|
||||
X fix fill/stroke issues in bugs db (bug 339)
|
||||
X saveTIFF, saveHeaderTIFF, saveTGA no longer public/static in PImage
|
||||
X this was a mistake to expose the api this way
|
||||
X more image file i/o in java 1.4
|
||||
X add dynamic loading of the jpeg, png, and gif(?) encoder classes
|
||||
X http://dev.processing.org/bugs/show_bug.cgi?id=165
|
||||
X http://java.sun.com/products/java-media/jai/index.jsp
|
||||
X http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1120174647
|
||||
X P5 cannot read files generated by saveFrame()
|
||||
X need to update docs re: tga
|
||||
X and add support for reading its own uncompressed TIFs
|
||||
X http://dev.processing.org/bugs/show_bug.cgi?id=260
|
||||
|
||||
_ an image marked RGB but with 0s for the alpha won't draw in JAVA2D
|
||||
|
||||
@@ -20,16 +29,6 @@ _ test support for reading and writing images that have alpha and imageio
|
||||
_ get tiff exporter for p5 to support argb and gray
|
||||
_ http://dev.processing.org/bugs/show_bug.cgi?id=343
|
||||
|
||||
X more image file i/o in java 1.4
|
||||
X add dynamic loading of the jpeg, png, and gif(?) encoder classes
|
||||
X http://dev.processing.org/bugs/show_bug.cgi?id=165
|
||||
X http://java.sun.com/products/java-media/jai/index.jsp
|
||||
X http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1120174647
|
||||
_ P5 cannot read files generated by saveFrame()
|
||||
_ need to update docs re: tga
|
||||
X and add support for reading its own uncompressed TIFs
|
||||
_ http://dev.processing.org/bugs/show_bug.cgi?id=260
|
||||
|
||||
_ this produces a dark blue background:
|
||||
colorMode(RGB, 100);
|
||||
background(128);
|
||||
|
||||
Reference in New Issue
Block a user