Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Image Processing - Jpeg

  • 09-03-2014 10:46pm
    #1
    Registered Users, Registered Users 2 Posts: 59 ✭✭


    Ok guys, working on my final year project here, and need some help, need to be able to take a BMP image, transform those into YCbCr values, perform a DCT on them, quantize them and then write this as a JPEG. writing the Jpeg is the problem, can get as far as the quantization stage, and now i need help, does anyone know if there is a Java library out there to do huffman coding that is Jpeg compliant, or does anyone know, do i even really need to perform the encoding in order for it to be a jpeg usable by any graphics software? Please help!! thanks!!


Comments

  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    writing the Jpeg is the problem

    There's standard Java libraries which perform this.
    You don't need to worry about how it's implemented.

    You can control the compression versus image quality with a parameter from 0 - 1 ( floating point number ).
    0 = best compression ( lossy compression = worst image quality )
    1 = lossless compression ( best quality, larger file sizes )
    0.8 = compromise between losing some quality for smaller file sizes


    Here's some code from a utility class of mine to store JPEGs and PNGs.
    I've thrown in some of the imports, might have missed some though..
    // Imports
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.awt.image.renderable.ParameterBlock;
    import javax.imageio.IIOImage;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriteParam;
    import javax.imageio.ImageWriter;
    import javax.imageio.stream.FileImageOutputStream;
    
    
         public static boolean savePNG(final String filename, final Image photo, final float quality)
        {
            return (saveImage(filename, "png", photo, quality));
        }
    
        
        public static boolean saveJPEG(final String filename, final Image photo, final float quality)
        {
            return (saveImage(filename, "jpg", photo, quality));
        }
        
        public static boolean saveImage(final String filename, final String imageType, final Image photo, final float quality)
        {
            boolean saved = false;
            BufferedImage bi = new BufferedImage(photo.getWidth(null), photo.getHeight(null), BufferedImage.TYPE_INT_RGB);
    
            try
            {
                Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(imageType);
                ImageWriter writer = (ImageWriter)iter.next();
                // instantiate an ImageWriteParam object with default compression options
                
                ImageWriteParam iwp = writer.getDefaultWriteParam();
                
                if (iwp.canWriteCompressed())
                {            
                    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                    iwp.setCompressionQuality(quality);   // a float between 0 and 1.  0 = best compression & worse quality, 1 = best quality & worst compression
                    // 1 specifies minimum compression and maximum quality
                }
                
                File file = new File(filename);
                FileImageOutputStream output = new FileImageOutputStream(file);
                writer.setOutput(output);
                IIOImage image = new IIOImage(bi, null, null);
                writer.write(null, image, iwp);
                writer.dispose();
                
                saved = true;
            }
            catch (Exception ex)
            {
                System.out.println("Error saving JPEG : " + ex.getMessage());
            }
    
            return (saved);
        }    
    

    regards,
    CD


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    Note that the above implementation is independent of the Sun/Oracles JRE.
    There is another way of implementing the above, but it relies on you using the Sun/Oracle JRE, so it may not work using other JRE's, eg. OpenJDK or IBM's JDK. But I'll add it here for completeness to show why I didn't use it in case someone posts it as a shorter/easier version.
     public static boolean saveJPEG(final String filename, final Image photo)
        {
            boolean saved = false;
            BufferedImage bi = new BufferedImage(photo.getWidth(null), photo.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();
            g2.drawImage(photo, null, null);
            FileOutputStream out = null;
    
            try
            {
                out = new FileOutputStream(filename);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
                param.setQuality(1.0f, false); // 100% high quality setting, no compression
                encoder.setJPEGEncodeParam(param);
                encoder.encode(bi);
                out.close();
                saved = true;
            }
            catch (Exception ex)
            {
                System.out.println("Error saving JPEG : " + ex.getMessage());
            }
    
            return (saved);
        }    
    


  • Registered Users, Registered Users 2 Posts: 59 ✭✭Nidge_Weasel


    Hi Creeping death. I am aware of the image libraries, but the problem is I cannot use them, as I need to perform operations on the quantized DCT, and write them as a jpeg unchanged.


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    Hi Creeping death. I am aware of the image libraries, but the problem is I cannot use them, as I need to perform operations on the quantized DCT, and write them as a jpeg unchanged.

    Okay, I haven't done that, but this looks like it's in the right area.


    http://alvinalexander.com/java/jwarehouse/mvnforum-1.0.0-rc4_04-src/myvietnam/src/net/myvietnam/mvncore/thirdparty/JpegEncoder.java.shtml


  • Registered Users, Registered Users 2 Posts: 59 ✭✭Nidge_Weasel


    Can you believe, I found this before, but without the Huffman step working. This is perfect I think, thank you!


  • Advertisement
Advertisement