Solved

Reducing Color depth of an image in java

Posted on 2004-09-27
41
1,704 Views
Last Modified: 2010-03-31
I have a necissity where I need to transmit images from one machine to other. These images are typically Screen Shots. In java using robot I am getting a 32 bit color depth images. I need to reduce their size and color depth. currently I am converting screen shots to jpg and png, but in order to reduce the size further I need to reduce the color depth.

Can any body tell me a way where I can reduce the color depth of an image in java ?
0
Comment
Question by:ThummalaRaghuveer
  • 13
  • 13
  • 13
  • +1
41 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12163437
It's possible, i think, to do this without advanced libraries, but would be probably much easier with them:

http://jiu.sourceforge.net/
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 167 total points
ID: 12164590
0
 
LVL 92

Expert Comment

by:objects
ID: 12165022
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12166136
http://www.gurge.com/amd/java/quantize/

I think quantize code in this link is not actually reducing the image, but is just playing around with the same image and trying to change the pixel colors in an appropriate way. I checked the sizes of various variables in the code and they never seem to change with what ever the level of compression. They are remaing the same as the original image.
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12166649
With the code available at
http://jiu.sourceforge.net/
is actually increasing my image sizes. I tried their example code on an image of size 72 kb and its size after conversion came to 339 kb in grey scale. Also it does not have anything to do with the jpg formats. I want to reduce the color depth because I wanted to reduce the size of the image... can anybody send a piece of code that does it or direct to a link which does it. Currently my images are of size 128 x 128 pixels and their size is from 250 bytes to 5kB. I want to reduce these sizes by reducing the color depth of the image.
0
 
LVL 92

Expert Comment

by:objects
ID: 12166659
theres example code in the links i posted above.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167122
Color reduction is not trivial, which is probably why you are having trouble finding something. See http://community.borland.com/article/0,1410,15972,00.html
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12167212
> I checked the sizes of various variables in the code and they never seem to change with what ever the level of compression. They are remaing the same as the original image.

But if you quantise the image down to say 8 colors, then jpeg compress the image with a high compression, the image size *should* be smaller

Don't forget that if you are sending the image as a jpg, it *has* to be a 32bit image...  That is what JPG is...
0
 
LVL 92

Expert Comment

by:objects
ID: 12167226
Thats right, check exactly what your encoder is doing with the colour depth.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167256
Don't forget that the png's compression capabilities are probably not being used. Can you post a link to one?
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175018
This is what I am doing currently..
//Here bi is the buffered image of the image I am trying to send.
ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayOutputStream out1 = new ByteArrayOutputStream();
            try {
                  ImageIO.write(bi, "jpg", out);
                  ImageIO.write(bi, "png", out1);
                  byte[] jpg = out.toByteArray();
                  byte[] png = out.toByteArray();
                  if (jpg.length < png.length) {
                        return jpg;
                  } else {
                        return png;
                  }

The bi here is of 32 bit color depth

At the receiving end I do the following once I store this byte array of jpg or png.

//Here the bytes in jpg or png are stored in imageBytes
BufferedImage newImage = ImageIO.read(new ByteArrayInputStream(imageBytes));

Here in quantize they are creating instance of Image from MemoryImageSource

How to get a jpg or png using an instance of Image or MemoryImageSource??

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 12175031
you want to be reducing the depth before you send it over the wire don'ty you?
How you decode it seems irrelevant.
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175200
Yaa I wish to do that but currently I am not doing it. I just generating a jpg or png and then seding it over the wire...

At the other end all I am doing is recreating the BufferedImage from the jpg or png image information stored in an array....

As of now I am not reducing the color depth.....
0
 
LVL 92

Expert Comment

by:objects
ID: 12175224
So what is it exactly you want to do?
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175280
Either process jpg/png that is generated and reduce the color depth of the image

or

process the bufferedImage and alter it in a way so that the jpg or png that I would get finally has a reduced color depth
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175304
You'd probably find it easier to get a library that supports png compression
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175346
does the png compression libraries reduce the color depth and also the size of the resulting image.. If so where can I find one and also a way to use it...?
0
 
LVL 92

Expert Comment

by:objects
ID: 12175408
No it doesn't.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175474
>>does the png compression libraries reduce the color depth

No

>>and also the size of the resulting image

If you mean file size, then yes

0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175487
I am already compressing the images into png using

ImageIO.write(bi, "png", out1);

as I mentioned above.....

I want to reduce file size further by reducing the quality of the image
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175497
>>I am already compressing the images into png using ...

That won't compress them
0
 
LVL 92

Expert Comment

by:objects
ID: 12175499
> I want to reduce file size further by reducing the quality of the image

Yes I realise that, will see if I can get something for you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175503
As i mentioned before, please post a link to one of these pngs
0
 
LVL 92

Expert Comment

by:objects
ID: 12175550
(s)he's already using compression, its the colour depth that needs to be reduced.

Did you have any particular mapping you wanted to use to reduce the color depth?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175564
>>(s)he's already using compression

How? Java doesn't support png compression (last i looked anyway)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175601
Personally, i think the likelihood of your finding a Java library to reduce color depth that doesn't trash the image somewhat slim
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175748
Nope I do not have any mappings to reduce the color depth..

The application does not store anything in files it just pushes them over the wire.... I`ll modify it to write into files and then post link to those images
0
 
LVL 92

Expert Comment

by:objects
ID: 12175761
copies of the images aren't really necessary as you've explained how they are produced.
the colour reduction needs to be done before the encoding so how they are encoded is not really relevant.
Will see if I can find some time to put some code together for you.
0
 
LVL 92

Accepted Solution

by:
objects earned 167 total points
ID: 12175768
If you wanted to have a play with a different png encoder in the meantime, then you could try this:
http://catcode.com/pngencoder/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12175786
>>... so how they are encoded is not really relevant.

>>If you wanted to have a play with a different png encoder in the meantime, then you could try this

LOL
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12175888
Anyway if you want to have a copy of the image created I have created You can access them at
http://www.cs.odu.edu/~rthummal/image/

I`ll keep looking into the encoders you have mentioned

Thanks...
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12183066
Finally Things started working...

but the way its beeing done is funny and is not efficient... Its some thing like creating a jpg or png as described in previous code and then alter it and create a png again.... If I could do this directly it would be great.... Here is how I did it......

Once I create a jpg or png as described in above code.... I am sending it to quantize program which plays around with the pixels and decreases the number of colors shown... Then I altered setImage(int,int,int[]) method in ImageFrame of quantize to use pngEncoder and compress it with a factor of 9 and make it return the image...

I am sending this image over the wire.......

Can we do the what quantize does directly using a BufferredImage instead of a jpg or png...?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 166 total points
ID: 12183297
>>Can we do the what quantize does directly using a BufferredImage instead of a jpg or png...?

call the code in TestQuantize.main at http://www.gurge.com/amd/java/quantize/src/TestQuantize.java
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12184488
I think I cannot call PixelGrabber on a bufferedImage
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12184496
Yes, you can
0
 
LVL 92

Expert Comment

by:objects
ID: 12184768
I should have a standalone routine done for you in the next couple of days, just a bit busy with other work at the moment. Will let you know.
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12185228
Can U give an Idea How......

In pixelGrabber I did not see any methods that operate on BufferedImage
0
 
LVL 92

Expert Comment

by:objects
ID: 12185247
BufferedImage is a subclass of Image so it can be used.
0
 
LVL 2

Author Comment

by:ThummalaRaghuveer
ID: 12189278
Thankyou very much for Ur help in this
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12193289
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12194498
no worries, glad I could help (:
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 107
Apps blocked by Java 9 81
hibernate example for saving data 19 43
Java string replace 11 48
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question