We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Seltar's UnZipIt - Transparent Images
Page Index Toggle Pages: 1
Seltar's UnZipIt - Transparent Images (Read 2458 times)
Seltar's UnZipIt - Transparent Images
Nov 4th, 2007, 4:00am
 
I have been using Seltar's very handy UnZipIt library < http://libraries.seltar.org/unzipit/ > and have just noticed there is no support for transparency when retreiving images from a zip file.

Seltar, is there any quick and easy way to cook this up?

Also, to check if a file exists before attempting to loadImage() I have altered the getZipEntry function to return a boolean value like so:

Quote:

/**
* Check file exists
* @return boolean
*/
public boolean fileExists(String fname){
 // returns ZipEntry of a requested file
 try {
   // Open the ZIP file
   ZipInputStream in = new ZipInputStream(new FileInputStream(zip.getAbsolutePath()));

   // Get the entry that matches
   ZipEntry entry = in.getNextEntry();
   while(entry != null && !entry.getName().toLowerCase().equals(fname.toLowerCase())){
     in.closeEntry();
     entry = in.getNextEntry();
   }

   if(entry == null){
     return false;
   }
   else {
     return true;
   }
 }
 catch (IOException e) {
   System.out.println(e);
 }
 return false;
}


I am not sure if this is the best way to do this, so maybe you can suggest a better way.

Thanks!
Re: Seltar's UnZipIt - Transparent Images
Reply #1 - Nov 4th, 2007, 5:18am
 
Hello!

Glad you're finding the library useful Smiley

Code:

/**
* Load Image from a requested file
* @param fname String Filename Inside Zip
* @return PImage Image
*/
public PImage loadImage(String fname){
// returns a PImage object of the requested files Byte array
byte[] ret = loadBytes(fname);
if(ret != null){
Image img = Toolkit.getDefaultToolkit().createImage(ret);
MediaTracker t = new MediaTracker(parent);
t.addImage(img, 0);
try {
t.waitForAll();
} catch (Exception e) {
System.out.println(e);
}
return new PImage(img);
}
return null;
}


That's the code for making a PImage.. I took a quick look around, but can't see any quick way of doing it.. but then again, it is 5 am.. and I'm going to bed.. I'll try again tomorrow Wink

seltar
Re: Seltar's UnZipIt - Transparent Images
Reply #2 - Nov 4th, 2007, 11:02am
 
Thanks for the quick reply Seltar!

Just looking through the PApplet.class from the core, when loadImage is usually called it loads the bytes, creates a image, then loads it into a PImage and then calls checkAlpha() on that.

Quote:

byte abyte1[];
if(!s1.equals("jpg") && !s1.equals("jpeg") && !s1.equals("gif") && !s1.equals("png") && !s1.equals("unknown"))
break MISSING_BLOCK_LABEL_182;
abyte1 = loadBytes(s);
if(abyte1 == null)
return null;
PImage pimage;
Image image1 = Toolkit.getDefaultToolkit().createImage(abyte1);
pimage = loadImageSync(image1);
if(s1.equals("gif") || s1.equals("png"))
pimage.checkAlpha();
return pimage;


Here is the checkAlpha function.

Quote:

protected void checkAlpha() {
 if(pixels == null)
   return;
 for(int i = 0; i < pixels.length; i++) {
   if((pixels[i] & 0xff000000) == 0xff000000)
     continue;
   format = 2;
   break;
 }
}


If  loadImage() accepted bytes then that would be super easy, but if looks like a matter of working in the checkAlpha() function in there somehow.

What do you think?
Re: Seltar's UnZipIt - Transparent Images
Reply #3 - Nov 4th, 2007, 11:25am
 
Ok, I managed to get transparent png files working with the following.
It looks like checkAlpha() is not public so I have copied the function itself into UnZipIt. Would be nice to just be able to call it however...

You might want to have a look over this, but it seems to be working fine.

Quote:

/**
    * Load Image from a requested file
    * @param fname String Filename Inside Zip
    * @return PImage Image
    */
   public PImage loadImage(String fname){
       // returns a PImage object of the requested files Byte array
       byte[] ret = loadBytes(fname);
       if(ret != null){
           PImage pimage;
           Image img = Toolkit.getDefaultToolkit().createImage(ret);
           pimage = parent.loadImageSync(img);
           
           // get the file extension
           int i = fname.lastIndexOf('.');
           String s1 = fname.substring(i + 1);
           
           if(s1.equals("gif") || s1.equals("png")) {
               // load the alpha values
               if(pimage.pixels != null){
                   for(int j = 0; j < pimage.pixels.length; j++) {
                       if((pimage.pixels[i] & 0xff000000) == 0xff000000)
                           continue;
                       pimage.format = 2;
                       break;
                   }
               }
           }
           return pimage;
       }
       return null;
   }
Re: Seltar's UnZipIt - Transparent Images
Reply #4 - Feb 19th, 2010, 4:55am
 
I download this library but how must i use it to unzip a file ?
Re: Seltar's UnZipIt - Transparent Images
Reply #5 - Feb 19th, 2010, 5:30am
 
Something like this

import seltar.unzipit.UnZipIt;
UnZipIt zip = new UnZipIt("zipfile.zip",this);
zip.unpackFile("image.jpg",sketchPath("mynewimage.jpg"));

API here:
http://libraries.seltar.org/unzipit/javadoc/index.html
Re: Seltar's UnZipIt - Transparent Images
Reply #6 - Feb 19th, 2010, 5:37am
 
thank you for your quick answer.

i found the API but i never really understand how it works...i think it miss examples...

concerning the small code you wrote here:
you unpack an image file, must i understand i have to unzip all files contained in the zip folder?
or is there a way to unzip all the folder ?
Re: Seltar's UnZipIt - Transparent Images
Reply #7 - Feb 19th, 2010, 6:06am
 
I tried :

Code:

import seltar.unzipit.UnZipIt;
String[] nom;


void setup()
{ println(this);
UnZipIt zip = new UnZipIt("D://jml//Documents//Société JML//podopressioscope_data//exemples//unzip//podopressioscope.zip",this);
zip.unpackFile("icon.gif",sketchPath("mynewimage.gif"));
nom=zip.getFilenames();

}


but it returns IllegalArgument Exeption !
Re: Seltar's UnZipIt - Transparent Images
Reply #8 - Feb 19th, 2010, 6:34am
 
I don't know if that's the cause of error, but you don't need (and should avoid) to double the forward slashes in the path.
Also, if possible, avoid accents in the path...
Re: Seltar's UnZipIt - Transparent Images
Reply #9 - Feb 19th, 2010, 7:54am
 
OK it seems to be better but

i succed to unzip gif file but not exe file !
i don't succed to obtain filesname by

String[] nom;
nom=zip.getFilenames();

may be because there is an exe file in the zip ?

can't we unzip directly the full the zip file ?
Re: Seltar's UnZipIt - Transparent Images
Reply #10 - Feb 19th, 2010, 8:01am
 
It seems to work with a zipfile without .exe, i succed to obtain filenames...
Re: Seltar's UnZipIt - Transparent Images
Reply #11 - Feb 19th, 2010, 8:43am
 
OK this code unzip the full zip file

BUT it doesn't work for any zip file !!!


Code:


import seltar.unzipit.UnZipIt;
String[] nom;


void setup()
{  
 UnZipIt zip = new UnZipIt(sketchPath("dataqueueloaderdemo.zip"),this);  
 nom=zip.getFilenames();
 
 for (int f=0;f<nom.length;f++)
       {String m=nom[f].substring(nom[f].length()-1,nom[f].length());println(m);
         if (m.equals("/"))
             {
             createOutput(nom[f]+"// ");println(nom[f]);}
             else zip.unpackFile(nom[f],sketchPath(nom[f]));
       }
 println(nom);

}




I thought it was due to exe file but it isn't i don't known why sometimes i have an IllegalArgumentExeption on the instruction :
 nom=zip.getFilenames();
Re: Seltar's UnZipIt - Transparent Images
Reply #12 - Feb 23rd, 2010, 12:42am
 
I get it !!

The probleme was that i had a directory name with some accents "paramètres" and this prevent "getFilenames()" to work !!

In French we use lots of accents so it will be fine to accept it in your unzip program...

Thanks for hepls
Page Index Toggle Pages: 1