The code below should be extracting the pixel data from the mapProvinceFile bitmap specificed and placing it into a byte array, modifying some of that color data depending on the contents of a custom ArrayList, vertically flipping the image, and then displaying the resulting modified image in a new pane.

Unfortunately, the image which is recreated appears to be identical to the original (apart from being flipped). This occurs despite the modified byte array being inside a databuffer which is used as the input for a raster which the resulting image then has applied to it by way of 'setData(raster)'.

The input is a very large unindexed 24bit bitmap. Here is an example file: http://www.mediafire.com/?rttpk4o33b3oj74

Here is the code: http://pastebin.com/TK0UbrPj

Anybody know what could be wrong?

If anybody knows a faster way of swapping particular colors in a very large unindexed bitmap that would be useful as well. I was thinking of somehow converting it to an indexed bitmap and then swapping the color indexes, but I couldn't figure out any way to successful assign it/recreate it with a color index with Java.

You use getData().getDataElements(... which can return all kinds of different formats, depending on the image source. Your apparent assumption that it's 3 bytes/pixel may be wrong? (Most commonly you see one 32-bit int per pixel containing ARGB values that you can unpack with shift/mask operation).
Have a look at the PixelGrabber class for a predictable consistent way to get pixel data from an image.

Testing the byte array using lines 44-46 of my code leads to output which strongly suggests it is using 3 bytes per pixel. It certainly is when I return it as int array.

You use getData().getDataElements(... which can return all kinds of different formats, depending on the image source. Your apparent assumption that it's 3 bytes/pixel may be wrong? (Most commonly you see one 32-bit int per pixel containing ARGB values that you can unpack with shift/mask operation).

OK, just thought that was worth checking.
Your code uses some complex-looking data structures, whose purpose isn't immediately obvious. Maybe it's time to back off a bit and do this is easy stages? Personally I would create small test file with a few blocks of solid RGBCMY colour, then try hard-coding swapping two of the colours before moving on to the full logic and the full file.

Thank you for the suggestion about testing it with a smaller image. After doing so, I noticed that some small changes were being made to the image, so the code recreating the image was not at fault.

It turned out I had forgotten to cast the left hand side values on lines 19, 21 and 23 from int to byte. I then needed to add an if statement to check that provinceDataTable[i].ownerColor was not null.

That solves the major issues. However, for some reason the changed parts of the image produced at the end are all using the greyscale equivalent of the color they should be.

All the issues are now solved. Thank you.

Great! Well done! Any particular learning points that you could share with us all here?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.