Intel® Integrated Performance Primitives
Community support and discussions relating to developing high-performance vision, signal, security, and storage applications.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

UIC Jpeg encoding color distortion

Matthew_C_1
Beginner
148 Views

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().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;
		

 

0 Kudos
1 Solution
Sergey_K_Intel
Employee
148 Views

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?

 

View solution in original post

4 Replies
Sergey_K_Intel
Employee
148 Views

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

Matthew_C_1
Beginner
148 Views

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 

Sergey_K_Intel
Employee
149 Views

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?

 

View solution in original post

Matthew_C_1
Beginner
148 Views

Hi Sergey,

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

Cheers

Reply