Code Comments
Programming Forum and web based access to our favorite programming groups.Hi,
I've tried decompressing JPEG-compressed raster data using both
javax.imageio.plugins.jpeg.* (together with the general javax.imageio.* infr
astructure) and the IJG jpeglib C code, but both of these keep swapping RGB
band ordering of my images to BGR ordering.
The only way I've found to work around this is to specify
JPEGImageReadParam.setSourceBands([b,g,r]), where b, g, and r correspond to
the band offsets for blue, green, and red bands. Just to give a bit of conte
xt, I've also implemented JPEG compression, which is how I get the raster da
ta to be compressed in the first place. During the JPEG compression process
I specify a ComponentSampleModel with the band offsets [r,g,b]. I know that
the JPEG compression works correctly, because the compressed data can be vie
wed in a standard browser with the correct colors.
My questions: Is the color swapping in JPEG decompression a known issue? Why
does JPEG decompression keep reversing the bands internally? Is it somethin
g with my usage of the API? Is my workaround valid, or does it just "cure th
e symptoms" without fixing the underlying problem?
If anyone can please help me out in getting to the bottom of this, I would r
eally appreciate it.
Thanks,
C.
(My decompression code is below. Just before I call this function, I write t
he data in bis to a file and successfully display it in a standard browser w
ith correct colors, so I am assuming that my compress function is correct an
d I therefore have not included the compression code. )
public static void decompress(BinaryInputStream bis, BinaryOutputStream bos)
{
ImageReadParam ep = null;
ImageReader imread = null;
ImageInputStream iis = null;
JPEGEncodeParam ep1 = null;
JPEGQTable[] qtbl_new = null;
JPEGHuffmanTable[] dchuff = null;
JPEGHuffmanTable[] achuff = null;
byte[] outBuffer = null;
int numBands = 3; //hard coded for clarity
int method = JPEG_FULL; //hard coded for clarity, but actually the swappin
g occurs for both full and abbreviated formats.
try
{
Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
imread = (ImageReader)readers.next();
ep = imread.getDefaultReadParam();
iis = ImageIO.createImageInputStream(bis);
imread.setInput(iis, true);
qtbl_new = new JPEGQTable[numBands];
dchuff = new JPEGHuffmanTable[numBands];
achuff = new JPEGHuffmanTable[numBands];
double scquality = (double)quality/100.0;
scquality = jpegQualityScaling(scquality);
if ((method == JPEG_ABBREV))
{
for (int z = 0; z < numBands; z++)
{
qtbl_new[z] = JPEGQTable.K2Chrominance.getScaledInstance((float) scquality
, true);
dchuff[z] = JPEGHuffmanTable.StdDCChrominance;
achuff[z] = JPEGHuffmanTable.StdACChrominance;
}
((JPEGImageReadParam)ep).setDecodeTables(qtbl_new, dchuff, achuff);
}
int[] bandOffsets = null;
bandOffsets = new int[numBands];
int ii = numBands-1;
for(int i=0; i < numBands; i++)
{
if (numBands == 3)
{
bandOffsets = ii;
ii = ii - 1;
}
else
{
bandOffsets = i;
}
}
((JPEGImageReadParam)ep).setSourceBands(bandOffsets);
BufferedImage decompIm = imread.read(0, ep);
Raster decompRaster = decompIm.getData();
//... snipped irrelevant processing of Raster.
}
catch (Exception ex)
{
try
{
iis.close();
}
catch (Exception ex1)
{
}
qtbl_new = null;
achuff = null;
dchuff = null;
outBuffer = null;
}
}
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.