//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 homepage | Victor Product Summary | more source code