leonbloy / pngj

PNGJ : pure Java library for high performance PNG encoding
http://hjg.com.ar/pngj/
273 stars 52 forks source link

ChunksList cannot be cast to ChunksListForWrite #34

Closed Potass closed 5 years ago

Potass commented 9 years ago

When I simply try to write something to metadata, ClassCastException is thrown.

My code: PngReader pngReader = new PngReader(new File("path to png image")); pngReader.getMetadata().setText("my key", "my data"); pngReader.end();

Exception is thrown when setText() is called from getChunkListW() method in PngMetadata class by this cast: return (ChunksListForWrite) chunkList;

Could you, please, fix it?

leonbloy commented 5 years ago

That's an invalid cast, and for a good reason. The ChunksList of a reader is not writable. The PngWriter instead has a ChunksListForWrite , and you can write to them.

I cannot imagine why you would want to write to the chunks of a just read PNG image, if you are trying to add a chunk to write a new PNG, you should use pngw.copyChunksFrom(pngr.getChunksList()); as in the Flip horizontally an image snippet https://github.com/leonbloy/pngj/wiki/Snippets