Victor Image Processing Library How-to Tips

Decompress a JPEG Image from Memory




Load a JPEG from an image buffer - the Source Code

This example demonstrates both the reading of the file and proper allocation of a memory buffer to hold it, and the use of the Victor library loadjpgfrombuffer function to decompress the JPEG data and create the image. Requires Victor Image Processing Library for Windows v 4.1x or higher.
//Replace existing image with a new JPEG image, loaded through a buffer
int loadjpgfilefrombuffer(LPTSTR fname, imgdes *image)
   {
   #define BAD_READ -87
   
   JpegData jdat;      // Reserve space for struct
   DWORD bytesRead, 
   int rcode = NO_ERROR;
   OFSTRUCT of_st;
   UCHAR huge *jpegbuff;
   HANDLE fhandle;
   DWORD filesize;
   
        // Load JPEG file into buffer
   fhandle = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ NORMAL, NULL);
   if(fhandle == INVALID_HANDLE_VALUE)
      return(BAD_OPN);
        // Get filesize
   filesize = GetFileSize(fhandle, NULL);
   CloseHandle(fhandle);  // Close the file
   
        // Allocate memory to hold file
   jpegbuff = (UCHAR huge *)GlobalAllocPtr(GMEM_MOVEABLE, filesize);
   if(jpegbuff == 0)
      return(BAD_MEM);   // Allocation failed!
      
        // Load JPEG file into memory
   fhandle = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ NORMAL, NULL);
   if(fhandle == INVALID_HANDLE_VALUE)
      return(BAD_OPN);
   ReadFile(fhandle, jpegbuff, filesize, &bytesRead, NULL);
   if(bytesRead == 0)
      rcode = BAD_READ;
   CloseHandle(fhandle);  // Close the file
   
        // Read the JPEG data from the buffer
   // Make sure JPEG data exists and get image dimensions
   rcode = jpeginfofrombuffer(jpegbuff, &jdat);
   if(rcode == NO_ERROR) {
        // Release the current image buffer
      freeimage(image);
        // Allocate the new image buffer to the file dimensions
      // This allocates a DIB to hold the image
      rcode = allocimage(image, jdat.width, jdat.length, jdat.vbitcount);
      if(rcode == NO_ERROR) {
        // Decompress the JPEG data
         rcode = loadjpgfrombuffer(jpegbuff, image);
         }   
      }
   GlobalFreePtr(jpegbuff); // Free allocated memory
   // Now the image is in the DIB and ready to process, display, or print
   return(rcode);
}
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




Copyright © 1995 - 1998 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.