int ImageCodecJPEG::decode( const Buffer & input, Buffer & output, const SizeInt & size ) { dword rgbWidth, rgbHeight, rgbChannels; byte * rgbBuffer = NULL; // decompress the jpeg data into the buffer if (! DecodeFromJPEGBuffer( (byte *)input.buffer(), input.bufferSize(), &rgbBuffer, &rgbWidth, &rgbHeight, &rgbChannels ) ) return -1; // an error has occured if ( size.width != rgbWidth || size.height != rgbHeight ) { delete [] rgbBuffer; return -1; // image is not the correct size } int nBytes = rgbWidth * rgbHeight * rgbChannels; output.set( rgbBuffer, nBytes ); return nBytes; }
BOOL sdlSurfaceCreateFromJpg(BYTE *pJpgBuf, DWORD nJpgSize, SDL_BMP_Surface *bitmapSurface) { SDL_Surface *bitmap; Uint32 rmask, gmask, bmask, amask; //corresponding to IJL_BGR; //IJL_BGR: B -> Byte 0, G -> Byte 1, R -> Byte 2 rmask = 0x00ff0000; gmask = 0x0000ff00; bmask = 0x000000ff; amask = 0x00000000; DWORD width, height, nchannels, pitch; BYTE *buffer; //raw data buffer BOOL bres; bres = DecodeFromJPEGBuffer(pJpgBuf, nJpgSize, &width, &height, &nchannels, &pitch, &buffer); if(!bres) return FALSE; bitmap = SDL_CreateRGBSurfaceFrom(buffer, width, height, 24, pitch, rmask, gmask, bmask, amask); //free(buffer); CAN'T free, since bitmap surface point to this buffer. ASSERT(bitmapSurface != NULL); bitmapSurface->bitmap = bitmap; bitmapSurface->buffer = buffer; bitmapSurface->nWidth = width; bitmapSurface->nHeight = height; return TRUE; }
//_tmain0 is only for test JPEG decode & SDL blit. It doesn't handle PW_avi file int _tmain0(int argc, _TCHAR* argv[]) { SDL_Surface *screen; Uint8 video_bpp; Uint32 videoflags; BOOL bres; //LPCSTR lpszPathName, DWORD width; DWORD height; DWORD nchannels; BYTE* buffer; DWORD pitch; char fn[128]; char *jpg; if(argc<1) { APP_ERROR("Please type %s jpgName\n", argv[0]); return -1; } /* Initialize SDL */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { APP_ERROR("Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); /* Clean up on exit */ video_bpp = 24; /* double buffer & full screen */ //videoflags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN; //screen=SDL_SetVideoMode(0, 0, video_bpp,videoflags); videoflags = SDL_HWSURFACE | SDL_DOUBLEBUF; screen=SDL_SetVideoMode(1280, 800, video_bpp,videoflags); if ( screen == NULL ) { APP_ERROR("Couldn't set screen x %d video mode: %s\n", video_bpp, SDL_GetError() ); exit(2); } APP_INFO("screen size %d x %d\n", screen->w, screen->h); buffer = NULL; for (int i=0; i<10; i++) { DWORD nJpgSize; BYTE *pJpgBuf; sprintf_s(fn, "wxga_%1d.JPG",i%2); //sprintf_s(fn, "%s",argv[1], i%2); jpg = fn; //bres = DecodeJPGFileToGeneralBuffer( jpg, &width, &height, &nchannels, &pitch, &buffer); //bres = DecodeJPGFileToGeneralBuffer( argv[1], &width, &height, &nchannels, &pitch, &buffer); pJpgBuf = utilFileLoad(jpg, &nJpgSize); if (NULL == pJpgBuf) continue; bres = DecodeFromJPEGBuffer(pJpgBuf, nJpgSize, &width, &height, &nchannels, &pitch, &buffer); free(pJpgBuf); SDL_Surface *bitmap; Uint32 rmask, gmask, bmask, amask; //corresponding to IJL_BGR; //IJL_BGR: B -> Byte 0, G -> Byte 1, R -> Byte 2 rmask = 0x00ff0000; gmask = 0x0000ff00; bmask = 0x000000ff; amask = 0x00000000; SDL_Rect dst={0}; //make dst in center of screen. if( (screen->w >= width) && (screen->h >= height) ) { dst.x = (Sint16)(screen->w - width) /2; dst.y = (Sint16)(screen->h - height) /2; } bitmap = SDL_CreateRGBSurfaceFrom(buffer, width, height, 24, pitch, rmask, gmask, bmask, amask); SDL_BlitSurface(bitmap, NULL, screen, &dst); //free alloc buffers; SDL_FreeSurface(bitmap); free(buffer); Uint32 ticks; // Do Delay ticks = SDL_GetTicks(); while(SDL_GetTicks() - ticks < 2000) { SDL_Delay(1000); } SDL_Flip(screen); } /* Shutdown all subsystems */ SDL_Quit(); printf("Quiting....\n"); return 0; }