Bug 24697 - DCR - Request for PNG write support
Summary: DCR - Request for PNG write support
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 2.0.1   Edit
Hardware: PC Windows 2000
: P3 enhancement with 10 votes (vote)
Target Milestone: 3.3   Edit
Assignee: Carolyn MacLeod CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
: 88258 116067 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-10-11 09:17 EDT by Ken Walker CLA
Modified: 2007-06-01 16:12 EDT (History)
15 users (show)

See Also:


Attachments
Patch to add PNG write support. (9.62 KB, patch)
2006-02-02 01:33 EST, Geoff Strom CLA
no flags Details | Diff
Patch to add PNG write support with CLDC compatible zlib support. (27.00 KB, patch)
2006-03-16 22:25 EST, Geoff Strom CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ken Walker CLA 2002-10-11 09:17:27 EDT
Currently, you can load PNG images, but not write them out. This is desirable 
for embedded development since the J2ME Profile MID-P specifies PNG. I'd like a 
way to use SWT's image loading and conversions to get my graphincs in to PNG 
format.
Comment 1 Steve Northover CLA 2002-10-11 10:54:58 EDT
We should do this but it's a lot of work to wrote a PNG writer, no?  Not for 
2.1?

Chrix to investigate and advise.
Comment 2 Christophe Cornu CLA 2002-10-11 11:07:26 EDT
Note to avoid any confusion: MIDP does not support PNG writing either.

Comment 3 Ken Walker CLA 2004-01-27 12:17:07 EST
I didn't say I wanted to write in MID-P, I use SWT as a tool to do conversions 
from my other formats on the desktop.
Comment 4 Remko Popma CLA 2004-02-15 10:10:12 EST
Currently images can only be saved as JPG files.
Support for PNG and GIF would be great!

See also Bug 38232 and Bug 38186.
Comment 5 Carolyn MacLeod CLA 2005-04-05 17:34:30 EDT
*** Bug 88258 has been marked as a duplicate of this bug. ***
Comment 6 Karel Kyovsky CLA 2005-08-05 09:13:05 EDT
I would like to realy have PNG saving funkcionality in eclipse.
Current state is horrible, reasons:
1. GIF doesn't support more than 256 colors.
2. JPG has very bad quality, there is no way to adjust it.
3. Conversion from SWT Image to AWT Buffered Image is damn slow.

Comment 7 Ken Walker CLA 2005-11-08 12:34:59 EST
Hmm, I entered this in 2002.  Any chance, since PNG is now a defacto-standard,
that this can be revisited?
Comment 8 Steve Northover CLA 2005-11-11 16:53:33 EST
*** Bug 116067 has been marked as a duplicate of this bug. ***
Comment 9 Geoff Strom CLA 2006-02-02 01:33:47 EST
Created attachment 34001 [details]
Patch to add PNG write support.

This patch adds support for saving SWT images as PNG files.

The patch supports the following PNG color types:

Type 2 (RGB triple) at 8 bits with or without SWT.TRANSPARENCY_PIXEL

Type 3 (palette) at 8 bits with or without SWT.TRANSPARENCY_ALPHA or SWT.TRANSPARENCY_PIXEL

Type 6 (RGB triple with alpha) at 8 bits
Comment 10 Carolyn MacLeod CLA 2006-02-02 14:40:11 EST
Nice clean-looking code, Geoff! I didn't test it yet - just read it quickly. Is it possible to write it using class org.eclipse.swt.internal.image.LZWCodec instead of java.util.zip.Deflater and java.util.zip.DeflaterOutputStream?
(possibly not, because LZWCodec uses LZW compression, and I believe PNG uses LZ77 compression, but maybe we could modify the class slightly and reuse it?)

Also, can we just do the CRC math, instead of using java.util.zip.CRC32?

The reason I ask is that we would prefer not to include the java.util.zip package if possible.
Comment 11 Carolyn MacLeod CLA 2006-02-02 15:59:25 EST
Note that class org.eclipse.swt.internal.image.PngChunk has a method called computeCRC which we can use to do the CRC calculation.
Comment 12 Carolyn MacLeod CLA 2006-02-02 17:38:53 EST
I started to implement this back in November, but I never wrote the compression code (i.e. the IDAT chunk) - I only wrote the "glue" chunks. Then other higher-priority stuff caused me to abandon what I had done. I've dragged it out to compare with your patch, and I think your stuff looks much cleaner. But we really would like to try to avoid the java.util.zip package. (We avoided it for the decoding - I believe it is not available in J2ME).

The stuff I did reused the swt internal PngChunk class & subclasses (which is why your code is cleaner... fewer classes <g>). I felt compelled to do it that way because that's what the decoder does (and I didn't write the decoder). The encoding logic that I added is all in PNGFileFormat.unloadIntoByteStream(). I am releasing the code that I wrote, temporarily, so that you can see it. It still throws SWT.ERROR_UNSUPPORTED_FORMAT so that nobody gets hurt <grin>. If you can figure out a way to use LZWCodec and updateAdler to do the compression, then we can use either your code or mine (or a mix) for the rest.
Comment 13 Ken Walker CLA 2006-02-02 17:46:36 EST
You are correct in that Zip support is not available in CLDC 1.0 or 1.1 and maintaining SWTs low level compatibility is important.
Comment 14 Geoff Strom CLA 2006-02-02 23:30:35 EST
Thanks for the fast feedback. I thought about using the subclasses too, but decided this way was cleaner (and easier). Using PngChunk for CRC should be an easy change, but losing java.util.zip is a bummer. I don't think the LZWCodec will be of much help as PNG uses LZ77, but I'll take a look at it.

Thanks for sharing your code; it must be hard to decide which wheel is squeakiest!
Comment 15 Geoff Strom CLA 2006-03-16 22:25:51 EST
Created attachment 36467 [details]
Patch to add PNG write support with CLDC compatible zlib support.

This is basically the same as the previous patch, except it uses PngChunk
for CRC support and it uses a new PngDeflater class for zlib/deflate
support.

PngDeflater is a simple, straight forward zlib implementation. It uses
fixed Huffman codes and a single matching strategy.

While portions of PngDeflater are based on Putty's sshzlib.c (which is
MIT licensed), any errors or deviations from RFC 1950/1951 are entirely
my own fault.

Below is a table that shows the difference between using PngDeflater and
java.util.zip.Deflater from Java 1.4.2_04 for compression. As you can see,
my code is both slower and less effective at compression, but it's not
horrible either. I would assume that the built-in zip compression uses
dynamic Huffman codes, which may explain some of the compressed size
difference.

At any rate, this code is a tinkerer's dream, and I've been playing with
it for too long, so here it is.

file            dimensions (pixels)    time (milliseconds)    size (bytes)

zip_small.png     100x89                 50                     6131
zip_medium.png   787x697                691                    53477
zip_large.png   1024x768               1071                   334834

test_small.png    100x89                110                     7441
test_medium.png  787x697                831                    69707
test_large.png  1024x768               1652                   396349
Comment 16 Carolyn MacLeod CLA 2006-03-20 11:41:06 EST
Fabulous! Sorry I am just getting back to you today - I was out of the office for March Break. I'll get back to you on this by the end of the day.
Carolyn
Comment 17 Carolyn MacLeod CLA 2006-03-20 15:31:34 EST
Very nice, Geoff! Thanks for going back and taking out the java zip dependence.
This will work nicely for us now, and I'll probably take the code pretty much verbatim. I might even delete a few of those old PNG loading classes that are not pulling their weight, to make loading and unloading more orthogonal.

Because this is more than just a bug fix it needs to be submitted through the "Contribution Questionnaire" process. First, I fill in the following form:
http://www.eclipse.org/legal/ContributionQuestionnairePart1-v1.0.php
and then someone from the Eclipse Management Organization will look the code over and get back to me before I can commit it. I suspect that this won't happen this week, because a lot of Eclipse folks are away at EclipseCon. I am indicating on the form that I want it in by M6 (March 31).

Thank-you very much for your contribution! I will make sure your name is on the SWT contributors page once it goes in.
Comment 18 Carolyn MacLeod CLA 2006-03-21 15:36:13 EST
Form submitted to EMO.
Comment 19 Carolyn MacLeod CLA 2006-04-26 15:26:20 EDT
The EMO stuff didn't go through in time to add this feature before the 
"no more features" milestone (M6), so this will not be able to go into 3.2. 
We will get it released into the 3.3. stream immediately after 3.2 ships.
Comment 20 Carolyn MacLeod CLA 2006-07-18 14:20:50 EDT
Fixed > 20060718.

Permission from the EMO was granted, and eclipse 3.2 has shipped. I've released the code to HEAD - it will be in next Tuesday's 3.3 integration build.

The ImageAnalyzer example now encodes PNG, and Snippet246.java was added to the SWT snippets page.

Thank-you very much for your contribution, Geoff - this is fabulous!