FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   color resolution of images saved by P5
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: color resolution of images saved by P5  (Read 1232 times)
Mythmon


color resolution of images saved by P5
« on: Mar 5th, 2004, 5:10am »

im working on a project (nothing big, just to learn, like everything else i do) and i need to know 2 things:
 
1. when P5 saves images, is there any color loss, or rounding of values in the color info?
2. can you save images that include the alpha value of a particular pixel, so that it can be read again when the image is loaded by P5?
 
If i cant do these things nativly, but someone knows a way to do it, id be grateful if you would share it with me.
« Last Edit: Mar 5th, 2004, 4:36pm by Mythmon »  
toxi_
Guest
Email
Re: color resolution of images saved by P5
« Reply #1 on: Mar 5th, 2004, 12:17pm »

if you save TIFF or TGA images, there's no loss of quality. AFAIK the alpha channel is only saved for TGA's, but you can't load TGAs with loadimage(). i will post an alternative function in a bit. there're also some issues with the alpha channel you should be aware of and can read about here
 
toxi_
Guest
Email
Re: color resolution of images saved by P5
« Reply #2 on: Mar 5th, 2004, 3:48pm »

okay, here it goes... that function loads 24bit or 32bit TGA images...
 
Code:
void setup() {
  BImage img=loadTarga("test.tga");
  // resize window to image size
  size(img.width,img.height);
  image(img,0,0);
}
 
// [toxi040304] Targa bitmap loader for 24/32bit RGB(A)
BImage loadTarga(String file) {
  // load image file as byte array
  byte[] buffer=loadBytes(file);
 
  // check if it's a TGA and has 8bits/colour channel
  if (buffer[2] == 2 && buffer[17] == 8) {
    // get image dimensions
    int w=(b2i(buffer[13])<<8) + b2i(buffer[12]);
    int h=(b2i(buffer[15])<<8) + b2i(buffer[14]);
    // check if image has alpha
    boolean hasAlpha=(buffer[16] == 32);
 
    // setup new image object
    BImage img=new BImage(w,h);
    img.format=(hasAlpha ? RGBA : RGB);
 
    // 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
    int offset=18;
 
    // read out line by line
    for (int y = h-1; y >= 0; y--) {
 for (int x = 0; x < w; x++) {
   img.pixels[index + x] = b2i(buffer[offset++]) | b2i(buffer[offset++])<<8 | b2i(buffer[offset++])<<16;
   // set alpha based on alpha data or revert to 100% (if there's only a 24bit image)
   if (hasAlpha) img.pixels[index + x]|=b2i(buffer[offset++])<<24;
   else img.pixels[index + x]|=0xff000000;
 }
 index -= w;
    }
    return img;
  }
  println("loadTarga(): wrong image format");
  return null;
}
 
// byte to integer conversion
int b2i(byte b) {
  return (int)(b<0 ? 256+b : b);
}

 
hope that helps!
 
Mythmon


Re: color resolution of images saved by P5
« Reply #3 on: Mar 5th, 2004, 4:36pm »

thanks toxi!
 
in that post you linked you said that alpha isn't set right for certain function. would set() be one of these?
« Last Edit: Mar 5th, 2004, 4:37pm by Mythmon »  
toxi_
Guest
Email
Re: color resolution of images saved by P5
« Reply #4 on: Mar 5th, 2004, 4:53pm »

set() should be fine as long as you make sure to use proper alpha values:
 
eg. to set a bright red pixel at 50% alpha you would need to write:
 
set(x,y,0x80ff0000);
 
the "80" component equals 128 in decimal which is 1/2 of 255 (ignoring rounding errors you have to live with). on screen this pixel will be at maximum red, regardsless of the current background. however once exported and used for compositing, its colour will blend with the new background colour used there and not stay bright red as on screen.
 
if you ignore the alpha value totally and just do:
 
set(x,y,0xff0000)
 
..it will still be a bright red dot on screen, but it will turn invisible in the exported image as now its alpha value = 0.
 
in general, working with alpha is tricky as the sketch display always has some specified background colour whereas the exported image could be composited with any other background. Processing is not photoshop and does not work with layers. everything you draw directly ends up in the same pixel buffer. It's pretty much like working only with the background layer in Photoshop and then exporting it. there will always be inconsistencies .
 
fry


WWW
Re: color resolution of images saved by P5
« Reply #5 on: Mar 5th, 2004, 6:20pm »

cool.. toxi, i've preliminarily added this code to loadImage().
 
Mythmon


Re: color resolution of images saved by P5
« Reply #6 on: Mar 5th, 2004, 6:23pm »

since ive begun trying to implement the image saving part of my sketch, ive been having some problems.
the completed skecth will allow you to make a 7x7 grid of 8x8 squares, then take every 8x8 square and turn it into 2 pixels and put those in a 14*7 image, save it and load it and decode it. so far i have this
http://mythmon.freeservers.com/
compile it and youl get a aray out of bounds error when you click the encode button. would someone like to try and fix and/or point out any other flaws in the progam, please?
 
thanks in advance
 
PS has anyone done anything similar to this?
« Last Edit: Mar 5th, 2004, 6:25pm by Mythmon »  
TomC

WWW
Re: color resolution of images saved by P5
« Reply #7 on: Mar 10th, 2004, 11:15am »

Code:

for (int l=0; l<8; k++)

 
I think you mean l++?
 
Meanwhile, I can't really tell what it's supposed to be doing because I don't have times.vlw
 
Mythmon


Re: color resolution of images saved by P5
« Reply #8 on: Mar 11th, 2004, 12:01am »

well actually times.vlw is just the times new roman font, and P5 made it into a .vlw file.
 
anyways, i found that error a while ago, and have already proceeded into the next part of the program. To explain what the program does: it will allow you to make a 7x7 grid of 8x8 squares, then take every 8x8 square and turn it into 2 pixels and put those in a 14*7 image, save it and load it and decode it into it's sets of squares.
 
anyways, it so far will code 1 square, and the rest should be fairly easy, then i just need to save the image and allow you to load them and decode the pictures.
 
Mythmon


Re: color resolution of images saved by P5
« Reply #9 on: Mar 14th, 2004, 12:00am »

hey toxi, does processing have a local method to save .tga? because ive tried myBImage.save("test.tga"); but the file it saves is unopenable by paint, photoshop and your loader loads something that doesn't have any color.
 
Pages: 1 

« Previous topic | Next topic »