JpegReader-ipp.cpp
Go to the documentation of this file.
00001 #include <assert.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <sys/stat.h>
00006 #include <unistd.h>
00007 
00008 #include "../libIppJpeg/decoder.h"
00009 
00010 #include "JpegReader-ipp.h"
00011 
00012 JpegReaderIPP::JpegReaderIPP() : JpegReader()
00013 {
00014         decoder = new CJPEGDecoder;
00015 }
00016 
00017 JpegReaderIPP::~JpegReaderIPP()
00018 {
00019         //printf("~JpegReaderIPP\n");
00020         delete decoder;
00021         decoder = NULL;
00022 }
00023 
00024 
00025 bool JpegReaderIPP::load(char * filename)
00026 {
00027         struct stat s;
00028         if (!stat(filename,&s)) {
00029                 char tmp[1024];
00030                 sprintf(tmp,"Could not load '%s'",filename);
00031                 perror(tmp);
00032                 return false;
00033         }
00034 
00035         FILE * infile;
00036         if ((infile = fopen(filename, "rb")) == NULL) {
00037                 fprintf(stderr, "can't open %s\n", filename);
00038                 return false;
00039         }
00040 
00041         unsigned char * fbuffer;
00042         unsigned int size;
00043 
00044         size = s.st_size;
00045         fbuffer = (unsigned char*)malloc(size);
00046         assert(fbuffer != NULL);
00047         fread(fbuffer,size,1,infile);
00048 
00049         bool res = this->load(fbuffer,size);
00050         free(fbuffer);
00051 
00052         /* And we're done! */
00053         return res;
00054 }
00055 
00056 
00057 bool JpegReaderIPP::load(const unsigned char * src, unsigned int srcsize)
00058 {
00059         JERRCODE     jerr;
00060         JCOLOR       jpeg_color;
00061         JSS          jpeg_sampling;
00062         int          jpeg_nChannels;
00063         int          jpeg_precision;
00064         JCOLOR       outcolor;
00065         int              outchan;
00066         unsigned int step, size;
00067 
00068         //printf("SRC  : ");for (unsigned int i=0;i<20;i++) printf("%02X ",src[i]); printf("\n");
00069         jerr = decoder->SetSource((Ipp8u*)src,srcsize);
00070         if(JPEG_OK != jerr) {
00071                 fprintf(stderr,"decoder.SetSource() failed\n");
00072                 return false;
00073         }
00074 
00075         jerr = decoder->ReadHeader((int*)&width,(int*)&height,
00076                         &jpeg_nChannels, &jpeg_color, &jpeg_sampling, &jpeg_precision); 
00077         if(JPEG_OK != jerr) {
00078                 fprintf(stderr,"decoder.ReadHeader() failed\n");
00079                 return false;
00080         }
00081         //printf("Header: %d chan cs %d\n",jpeg_nChannels,jpeg_color);
00082 
00083         switch (reqColorSpace) {
00084                 case cmAuto :
00085                         //printf("Req Auto: ");
00086                         switch (jpeg_color) {
00087                                 case JC_GRAY:
00088                                         //printf("Out Gray\n");
00089                                         outputColorSpace = cmGray;
00090                                         step = width * sizeof(Ipp8u);
00091                                         size = height * step;
00092                                         outcolor = JC_GRAY;
00093                                         outchan  = 1;
00094                                         break;
00095                                 case JC_YCBCR:
00096                                         //printf("Out YUV\n");
00097                                         outputColorSpace = cmYUV;
00098                                         step = width*sizeof(Ipp8u)*3;
00099                                         size = height * step;
00100                                         outcolor = JC_YCBCR;
00101                                         outchan  = 3;
00102                                         break;
00103                                 default :
00104                                         //printf("Out RGB\n");
00105                                         outputColorSpace = cmRGB;
00106                                         step = width*sizeof(Ipp8u)*3;
00107                                         size = height * step;
00108                                         outcolor = JC_RGB;
00109                                         outchan  = 3;
00110                                         break;
00111                         }
00112                         break;
00113                 case cmRGB:
00114                         //printf("Out RGB\n");
00115                         outputColorSpace = cmRGB;
00116                         step = width*sizeof(Ipp8u)*3;
00117                         size = height * step;
00118                         outcolor = JC_RGB;
00119                         break;
00120                 case cmYUV:
00121                         //printf("Out YUV\n");
00122                         outputColorSpace = cmYUV;
00123                         step = width*sizeof(Ipp8u)*3;
00124                         size = height * step;
00125                         outcolor = JC_YCBCR;
00126                         outchan  = 3;
00127                         break;
00128                 case cmGray:
00129                 default :
00130                         //printf("Out Gray\n");
00131                         outputColorSpace = cmGray;
00132                         step = width * sizeof(Ipp8u);
00133                         size = height * step;
00134                         outcolor = JC_GRAY;
00135                         outchan  = 1;
00136                         break;
00137 
00138         }
00139 
00140         if (!externaloutput) {
00141                 buffer = (unsigned char*)(realloc(buffer,size));
00142                 assert(buffer != NULL);
00143         }
00144 
00145         IppiSize dim = {width,height};
00146         jerr = decoder->SetDestination((Ipp8u*)buffer,
00147                         step, dim, outchan, outcolor);
00148         if(JPEG_OK != jerr) {
00149                 fprintf(stderr,"decoder.SetDestination() failed\n");
00150                 return false;
00151         }
00152 
00153         jerr = decoder->ReadData();
00154         if(JPEG_OK != jerr) {
00155                 fprintf(stderr,"decoder.ReadData() failed\n");
00156                 return false;
00157         }
00158         //printf("OUT  : ");for (unsigned int i=0;i<20;i++) printf("%02X ",buffer[i]); printf("\n");
00159 
00160         return true;
00161 }
00162 
00163 void JpegReaderIPP::setOutputColorSpace(ColorSpace cspace)
00164 {
00165         reqColorSpace = cspace;
00166 }
00167 


canon_vbc50i
Author(s): Cedric Pradalier
autogenerated on Mon Jan 6 2014 11:18:27