Community
cancel
Showing results for 
Search instead for 
Did you mean: 
fanguts
Beginner
26 Views

UIC decoder: some problem from Readdata()?

Hello: I implemented UIC decoder to my application. A number of decoder's interface has been implemented(such as jpegdec.Init(), jpegdec.AttachStream(), jpegdec.ReadHeader(), jpegdec.SetParams(), jpegdec.ReadData() ).
I can see the program got correct image size, sampling rate from ReadHeader().
In my program final part, I created a data buffer with size (image Height*Width*Sampling Rate). I copy the depressed image data--Buffer().DataPtr() from Readdata() to my buffer, the program is crush. It seems problem from jpegdec.ReadData(). I post my function below, please give some help, thank you all

FunctToReadJPEG()

{

int i;

int du = 0;

int dd_factor;

int comment_size;

Ipp8u* comment;

Image imageCn;

ExcStatus status;

JPEGDecoder jpegdec;

ImageDataPtr dataPtr;

ImageDataOrder dataOrder;

ImageColorSpec colorSpec;

ImageSamplingGeometry geometry;

PARAMS_JPEG param;

param.nthreads = 1;

param.use_qdct = 1;

param.dct_scale = JD_1_1;

param.color = JC_UNKNOWN;

param.huffman_opt = 0;

param.mode = JPEG_BASELINE;

param.point_transform = 0;

param.predictor = 1;

param.quality = 75;

param.restart_interval = 0;

param.sampling = JS_444;

param.dct_scale = JD_1_1;

param.use_qdct = 1;

param.comment_size = sizeof(param.comment);

jpegdec.SetNOfThreads(param.nthreads);

status = jpegdec.Init();

if(ExcStatusOk != status)

return JPEG_ERR_INTERNAL;

CStdFileInput in;

BaseStream::TStatus statusStream;

statusStream = in.Open("c:\\test.jpg");

if(BaseStream::StatusOk != statusStream)

{

return 0;

}

status = jpegdec.AttachStream(in);

if(ExcStatusOk != status)

return JPEG_ERR_INTERNAL;//UIC end

status = jpegdec.ReadHeader(colorSpec,geometry);//got image size 400X100, sampling rate 3 correctly

if(ExcStatusOk != status)

return JPEG_ERR_INTERNAL;

m_nPlateWidth = geometry.RefGridRect().Width();

m_nPlateHeight = geometry.RefGridRect().Height();

param.mode = jpegdec.Mode();

param.color = GetColor(colorSpec.EnumColorSpace());

param.sampling = GetSampling(geometry.EnumSampling());

jpegdec.Comment(&comment, &comment_size);

if(comment_size != 0)

{

param.comment_size = IPP_MIN(comment_size,MAX_JPEG_COMMENT_BUF);

ippsCopy_8u(comment,param.comment,param.comment_size);

}

else

{

param.comment[0] = '\0';

param.comment_size = 0;

}

int nOfComponents = geometry.NOfComponents();

if(JPEG_BASELINE == param.mode || JPEG_PROGRESSIVE == param.mode)

{

switch(param.dct_scale)

{

case JD_1_1: dd_factor = 1; break;

case JD_1_2: dd_factor = 2; break;

case JD_1_4: dd_factor = 4; break;

case JD_1_8: dd_factor = 8; break;

default:

{

param.dct_scale = JD_1_1;

dd_factor = 1;

}

}

}

else

{

param.dct_scale = JD_1_1;

dd_factor = 1;

}

JPEGDecoderParams jparam;

jparam.SetDCTScaleFactor(param.dct_scale);

jparam.SetDCTType(param.use_qdct);

jpegdec.SetParams(jparam);

if(param.dct_scale != JD_1_1)

{

Rect refgrid;

Point origin;

RectSize size;

origin.SetX(0);

origin.SetY(0);

switch (param.dct_scale)

{

case JD_1_2:

{

size.SetWidth ((geometry.RefGridRect().Width() + 1) >> 1);

size.SetHeight((geometry.RefGridRect().Height() + 1) >> 1);

}

break;

case JD_1_4:

{

size.SetWidth ((geometry.RefGridRect().Width() + 3) >> 2);

size.SetHeight((geometry.RefGridRect().Height() + 3) >> 2);

}

break;

case JD_1_8:

{

size.SetWidth ((geometry.RefGridRect().Width() + 7) >> 3);

size.SetHeight((geometry.RefGridRect().Height() + 7) >> 3);

}

break;

}

refgrid.SetOrigin(origin);

refgrid.SetSize(size);

geometry.SetRefGridRect(refgrid);

geometry.ReAlloc(nOfComponents);

}

if(colorSpec.DataRange()->BitDepth() + 1 <= 8)

{

dataOrder.SetDataType(T8u);

du = sizeof(Ipp8u);

}

else

{

dataOrder.SetDataType(T16u);

du = sizeof(Ipp16u);

}

dataOrder.ReAlloc(Interleaved, nOfComponents);

dataOrder.PixelStep()[0] = nOfComponents;

dataOrder.LineStep() [0] = ((geometry.RefGridRect().Width() * nOfComponents *du+ 3) & ~3);

imageCn.ColorSpec().ReAlloc(nOfComponents);

imageCn.ColorSpec().SetColorSpecMethod(Enumerated);

imageCn.ColorSpec().SetComponentToColorMap(Direct);

for(i = 0; i < nOfComponents; i++)

{

if(colorSpec.DataRange()->BitDepth() + 1 <= 8)

{

imageCn.ColorSpec().DataRange().SetAsRange8u(255);

}

else

{

imageCn.ColorSpec().DataRange().SetAsRange16u(1 << colorSpec.DataRange()->BitDepth());

}

}

ImageEnumColorSpace in_color;

ImageEnumColorSpace out_color;

in_color = colorSpec.EnumColorSpace();

switch(nOfComponents)

{

case 1: out_color = (in_color == Unknown) ? Unknown : Grayscale; break;

case 3: out_color = (in_color == Unknown) ? Unknown : RGB; break;

case 4: out_color = (in_color == Unknown) ? Unknown : CMYK; break;

default: out_color = Unknown; break;

}

imageCn.ColorSpec().SetEnumColorSpace(out_color);

int r;

IppiSize size;

size.width = geometry.RefGridRect().Width();

size.height = geometry.RefGridRect().Height();

CIppImage image;

r = image.Alloc(size, nOfComponents, colorSpec.DataRange()->BitDepth() + 1, 1);

if(0 != r)

return JPEG_ERR_ALLOC;

image.Color(GetColor(out_color));

if(image.Precision() <= 8)

{

dataPtr.p8u = image;

}

else

{

dataPtr.p16s = image;

}

imageCn.Buffer().Attach(&dataPtr,dataOrder,geometry);

status = jpegdec.SetParams(imageCn.ColorSpec(),imageCn.Buffer().BufferFormat().SamplingGeometry());

if(ExcStatusOk != status)

return JPEG_ERR_INTERNAL;

status = jpegdec.ReadData(imageCn.Buffer().DataPtr(),dataOrder);

if(ExcStatusOk != status)

return JPEG_ERR_INTERNAL;

/* ********************************************************
after readheader() and readData(), I created TempBuf

and copy the depressed image data imageCn.Buffer().DataPtr() to TempBuf, but program crash
**************************************************************/

BYTE *TempBuf;

TempBuf = new BYTE [size.height*size.width *3 ];//3 is sampling rate

for (int nY = 0; nY < size.height ; nY ++)

for (int nX = 0; nX < size.width ; nX ++)

{

memcpy(TempBuf + nY * size.width * 3 + nX * 3, imageCn.Buffer().DataPtr() + nY * size.width * 3 + nX * 3, 3);

}

}



0 Kudos
1 Reply
Vladimir_Dudnik
Employee
26 Views

memory allocated for UIC Image object may get padded at each row to get 32-bit alignment. The right way to copy pixel data from UIC Image object would be using ippiCopy function for appropriate bit-depth and number of channels. This will require you to get image step (aka pitch) info from UIC Image

Regards,
Vladimir