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_
   Topics & Contributions
   Tools
(Moderator: REAS)
   Save sketch to JPEG/PNG
« Previous topic | Next topic »

Pages: 1 2 3 
   Author  Topic: Save sketch to JPEG/PNG  (Read 9172 times)
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Save sketch to JPEG/PNG
« on: Oct 21st, 2003, 3:29pm »

Should save the sketch into a JPEG file can also save into PNG. It's partially working but I can't seem to grab the sketch image ... Koenie is working on this one. I wonder if anyone can beat him to it. hehe. (Excluding the P5 Dev Team)
 
Code:

// SaveJPEG
 
import java.applet.*;  
import java.awt.*;  
import java.awt.image.*;  
import java.awt.event.*;  
import java.io.*;  
import java.net.*;  
import java.text.*;  
import java.util.*;  
import java.util.zip.*;  
import javax.imageio.*;
 
public class SaveJPEG extends BApplet
{
  RenderedImage rendImage;
 
  void setup()
  {
    size(200,200);
    background(255);
    stroke(255);
    line( 0, 0, 80, 80 );
  }
   
  void draw()
  {
    rendImage = creator();
    try
    {
 File file = new File("newimage.png");
 ImageIO.write(rendImage, "png", file);
 
 file = new File("newimage.jpg");
 ImageIO.write(rendImage, "jpg", file);
    }
    catch (IOException e)
    {
    }  
  }    
   
  RenderedImage creator()
  {
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    // Graphics mg = bufferedImage.getGraphics();
    // Graphics m = super.getGraphics();
    // m = bufferedImage.getGraphics();
    Graphics m = bufferedImage.getGraphics();
    // m. drawstuff ...
    m.dispose();
    return bufferedImage;
  }
}

 
Note: This is a VERY rough way to do things hehe.
Hint: pixels[] is nice.
« Last Edit: Oct 21st, 2003, 3:31pm by Martin »  
Koenie

170825270170825270koeniedesign WWW Email
Re: Save sketch to JPEG/PNG
« Reply #1 on: Oct 21st, 2003, 3:50pm »


 
Code:
import java.applet.*;  
import java.awt.*;  
import java.awt.image.*;  
import java.awt.event.*;  
import java.io.*;  
import java.net.*;  
import java.text.*;  
import java.util.*;  
import java.util.zip.*;  
import javax.imageio.*;
 
public class SaveJPEG extends BApplet {
  void setup() {
    size(200,200);
    background(255);
     
    // draws some funky stuff on the screen
    stroke(0); line(0, 0, 80, 80);
    noStroke(); fill(0, 255, 0); ellipse(10, 10, 40, 40);
  }
 
  RenderedImage rendImage; // image to render to
 
  void draw() {
    rendImage = creator();
 
    try {
 File file = new File("newimage.png");
 ImageIO.write(rendImage, "png", file); // saves to PNG
 
 file = new File("newimage.jpg");
 ImageIO.write(rendImage, "jpg", file); // saves to JPG
    }
    catch (IOException e) {}
  }
   
  RenderedImage creator() {
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics m = bufferedImage.getGraphics();
     
    // copies all pixels to the new Graphics object
    for (int i = 0; i < width; i++) {
 for (int j = 0; j < height; j++) {
   m.setColor(new Color(pixels[j*width+i]));
   m.drawLine(i, j, i, j);
 }
    }
     
    m.dispose(); // don't know what this is for...
    return bufferedImage;
  }
}

 
Koenie
 

http://koeniedesign.com
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #2 on: Oct 21st, 2003, 4:00pm »

good. m.dispose() is to 'throw away' the Graphics object that we created so that some system resources used by it may be freed up.
 
now, since javax.imageio isn't imported by processing, we'd want to use another way to deal with exporting to jpeg/png files.
 
(coding right now ... will edit this post in a while)
 
koenie: to do ... make it more flexible.
« Last Edit: Oct 21st, 2003, 4:03pm by Martin »  
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #3 on: Oct 21st, 2003, 4:18pm »

here... no more java mode.
 
Code:

void setup()
{
  size(200,200);
  background(255);
 
  // draws some funky stuff on the screen
  stroke(0); line(0, 0, 80, 80);
  noStroke(); fill(0, 255, 0); ellipse(10, 10, 40, 40);
}
 
RenderedImage rendImage; // image to render to
BufferedImage backBuffer;
 
void draw()
{
  rendImage = creator();
  write("koenie.jpg");
}
 
RenderedImage creator()
{
  BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics m = bufferedImage.getGraphics();
 
  // copies all pixels to the new Graphics object
  for (int i = 0; i < width; i++)
  {
    for (int j = 0; j < height; j++)
    {
 m.setColor(new Color(pixels[j*width+i]));
 m.drawLine(i, j, i, j);
    }
  }
 
  m.dispose(); // don't know what this is for...
  return bufferedImage;
}
 
void write( String fileName )
{
  backBuffer = (BufferedImage) rendImage;
   
  try
  {
    BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( fileName ));
    com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder( bos );
    com.sun.image.codec.jpeg.JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam( backBuffer );
    jep.setQuality( 1.0f, false );
    encoder.setJPEGEncodeParam( jep );
    encoder.encode( backBuffer );
    bos.close();
  }
  catch ( Exception e )
  {
    e.printStackTrace();
  }
}
 
Koenie

170825270170825270koeniedesign WWW Email
Re: Save sketch to JPEG/PNG
« Reply #4 on: Oct 21st, 2003, 4:31pm »

and the programs goes shorter and shorter.
this code uses the function saveToJPG("filename.jpg")
 
Code:
void setup() {  
  size(200,200);
  background(255);
 
  // draws some funky stuff on the screen
  stroke(0); line(0, 0, 80, 80);
  noStroke(); fill(0, 255, 0); ellipse(10, 10, 40, 40);
}
 
void draw() {
  saveToJPG("koenie.jpg");
}
 
void saveToJPG(String fileName) { // oh, how we love compact coding
  BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics m = img.getGraphics();
  for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {m.setColor(new Color(pixels[j*width+i])); m.drawLine(i, j, i, j);}}
  m.dispose(); try {BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
    com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(bos);
    com.sun.image.codec.jpeg.JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(img);  
    jep.setQuality(1.0f, false); encoder.setJPEGEncodeParam(jep); encoder.encode(img); bos.close();
  } catch ( Exception e ) {}
}

 
Koenie
 

http://koeniedesign.com
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #5 on: Oct 21st, 2003, 4:39pm »

good good. now we should move on and do the direct translation (w/o having to drawline) by using an offscreen graphics object.
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #6 on: Oct 22nd, 2003, 2:47am »

hehe. btw koenie, it's better to have longer human-readable code rather than have short/compact obfuscated code.
 
benelek

35160983516098 WWW Email
Re: Save sketch to JPEG/PNG
« Reply #7 on: Oct 22nd, 2003, 2:56am »

hey, obfuscation rocks! the problem is that the bboard tends to wrap the long lines of code, making it harder to tell when new lines start
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #8 on: Oct 22nd, 2003, 3:27am »

benelek: Haha! In that case, link your code!
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #9 on: Oct 22nd, 2003, 3:48am »

http://decode.ateneo.edu/martin/SaveJPEGPNG.pde
 
now uses the save(filename) syntax to save to either jpeg, png, tiff, or targa formats.
 
shorter code (really?)
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #10 on: Oct 22nd, 2003, 4:21am »

javax.imageio is of course available only in 1.4 and not in 1.3. while, com.sun.image is already available in 1.3.  we revolve around this.
 
http://decode.ateneo.edu/martin/SaveJPEGPNG2.pde
 
er... how about 1.5 and up? will implement this later as we don't know what's in store for us come 1.5 (bloat bloat bloat!)
 
arielm

WWW
Re: Save sketch to JPEG/PNG
« Reply #11 on: Oct 22nd, 2003, 11:32am »

martin, after looking into the code: why using a loop with "drawLine(i,j,i,j)" when BufferedImage gives you the 2 following methods?:
 
 
setRGB(int x, int y, int rgb)
 
Sets a pixel in this BufferedImage to the specified RGB value.
 
 
setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
 
Sets an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image data.
 
 
the first method will surely work, but still require a loop iterating over all the pixels,
 
while the second should take care of it in a blast, providing the correct parameters (+ creating your BufferedImage with TYPE_INT_ARGB which is the format of Bagel's pixels)
 
hth
 

Ariel Malka | www.chronotext.org
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Save sketch to JPEG/PNG
« Reply #12 on: Oct 22nd, 2003, 1:14pm »

hi ariel,
 
'twas koenie's creative idea! (but hey, it works! ) hehe the first method goes kazooks when doing jpeg. as for the second method, which one's rgbArray[]? could do this by getRGB but which?
 
thanks,
martin
 
arielm

WWW
Re: Save sketch to JPEG/PNG
« Reply #13 on: Oct 22nd, 2003, 3:36pm »

i think the requested array is simply bagel's pixels[]...
 
concerning the other parameters, i guess:
 
"startX", "startY" and "offset" should be 0,
 
"w" and "scansize" should be equal to bagel's width
 
and finally, "h" should be equal to bagel's height
 
 
well, all this is not tested, and probably won't work so easily
 

Ariel Malka | www.chronotext.org
Koenie

170825270170825270koeniedesign WWW Email
Re: Save sketch to JPEG/PNG
« Reply #14 on: Oct 22nd, 2003, 4:33pm »

But I want it shorter!
 
http://koeniedesign.com/saveitall.pde
 

 
Koenie
 

http://koeniedesign.com
Pages: 1 2 3 

« Previous topic | Next topic »