UIC Jpeg encoding color distortion

UIC Jpeg encoding color distortion

I am trying to encode raw bitmap pixel data to a jpeg encoded equivalent. I am able to compile the uic example given (uic_transcoder_con) and everything is working fine. When I try to implement the sample into my code however I am getting a color distorted version of my original image. I have the same encoding parameters as the example and have tried using other combinations but to no success. I have attached a sample image I am trying to encode and the resulting jpeg I get after the encoding. Here is a snippet of the code I am trying out.

.....

		//BEGIN ENCODING PROCESS
		ExcStatus excStatus = ExcStatusOk;
		JPEGEncoder encoder;
		Image imageCn;
		ImageDataPtr dataPtr;
		ImageDataOrder dataOrder;
		Rect refgrid;
		RectSize size;
		Point origin;
		ImageSamplingGeometry geometry;
		CMemBuffOutput* out = new CMemBuffOutput(); 
		BaseStream::TSize mosize;
		Ipp8u* buf;

		int nOfComponents = numSamples;

		//int du = ((8&255)+7)/8 ;
		int step = dstWidth * nOfComponents; 

		mosize = step*dstHeight*2; 

		buf = (Ipp8u*)ippMalloc((int)mosize);
		if(0 == buf)
			return 1;

		//open the output stream
		out->Open(buf, (int)mosize);
	
		excStatus = encoder.SetNOfThreads(1);
		if(UIC::ExcStatusOk != status)
			return 1;

		//initialize encoder
		excStatus = encoder.Init();
		if(UIC::ExcStatusOk != status)
			return 1;

		//attach output stream to encoder
		excStatus = encoder.AttachStream(*out);
		if(ExcStatusOk != excStatus)
			return 1;

		dataOrder.SetDataType(T8u);

		origin.SetX(0);
		origin.SetY(0);

		size.SetWidth(dstWidth);
		size.SetHeight(dstHeight);
		
		refgrid.SetOrigin(origin);
		refgrid.SetSize(size);
		
		geometry.SetRefGridRect(refgrid);
		geometry.ReAlloc(nOfComponents);
		geometry.SetEnumSampling(S444);

		dataOrder.ReAlloc(Interleaved, nOfComponents);
		dataOrder.PixelStep()[0] = nOfComponents;
		dataOrder.LineStep() [0] = step;

		imageCn.ColorSpec().ReAlloc(nOfComponents);
		imageCn.ColorSpec().SetColorSpecMethod(Enumerated);
		imageCn.ColorSpec().SetComponentToColorMap(Direct);

		for(int i = 0; i < nOfComponents; i++)
		{
			imageCn.ColorSpec().DataRange()[i].SetAsRange8u(255);
		}

		imageCn.ColorSpec().SetEnumColorSpace(ImageEnumColorSpace::RGBA);
		
		//set pointer equal to resized pixel data (NOTE: imgResizeBuf is an unsigned char* )
		dataPtr.p8u = imgResizeBuf;

		//set the buffer of the image
		imageCn.Buffer().Attach(&dataPtr,dataOrder,geometry);
	
		if(ExcStatusOk != encoder.AttachImage(imageCn))
		return 1;
		
		//initialize encoding params
		JPEGEncoderParamsBAS je_params;
		
		je_params.SetColor((JCOLOR)ImageEnumColorSpace::YCbCr);
		je_params.SetSampling(JSS::JS_444);
		je_params.SetRestartInterval(1);
		je_params.SetHuffmanOption(0);
		je_params.SetQuality(100);
		je_params.SetThreading((JTMODE)1);
		
		if(ExcStatusOk != encoder.SetParams(je_params))
			return 1;


		excStatus = encoder.WriteHeader();
		if(ExcStatusOk != excStatus)
			return 1;

		//encode the data
		excStatus = encoder.WriteData();
		if(ExcStatusOk != excStatus)
			return 1;
		

 

AttachmentSize
Downloadimage/jpeg testImageJpeg.jpg556.27 KB
Downloadimage/x-ms-bmp testImageOrig.bmp1.17 MB
5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Matthew,

As far as uci_transcoder_con works OK with this image, could your check the difference in JPEG encoder initialization between your source code and uic_transcoder. Just in debugger, line by line. Especially, when it concerns pixel type, channel number and color space. May be you need to specify BGRA instead of RGBA ?

Regards,
Sergey

Hi Sergey,

I have ran both uic_transcoder_con and my source code at the same time and compared the jpeg encoder properties. They are identical to one another, (noChannels = 4, RGBA color space, etc.). The one line in my source code I am a bit concerned about is how I am allocating the dataPtr.p8u to an unsigned char* (imgResizeBuf). Could this be a possible problem? The reason I mention this is that I saw my memory output stream length after writing was not the same size as the one in the uic_transcoder_con. 

Thanks 

Best Reply

How did you read BMP file? In uic_transcoder I see the channel swapping for 32-bit BMPs:

    case 4:
      status = ippiSwapChannels_8u_C4IR((Ipp8u*)m_imageData,m_step, m_roi, order);
      break;

with order=            int order_c4[4] = { 2, 1, 0, 3};

Did you do that?

 

Hi Sergey,

That was exactly my problem! Thank you so much for the quick reply.

Cheers

Leave a Comment

Please sign in to add a comment. Not a member? Join today