void *image_read_exr(const char *name, int *w, int *h, int *c, int *b) { ImfInputFile *file; ImfRgba *data; const ImfHeader *head; float *p = 0; if ((file = ImfOpenInputFile(name))) { if ((head = ImfInputHeader(file))) { int x0; int x1; int y0; int y1; int i; /* Read and extract header info. */ ImfHeaderDataWindow(head, &x0, &y0, &x1, &y1); *w = x1 - x0 + 1; *h = y1 - y0 + 1; *c = 4; *b = sizeof (float); /* Allocate temporary storage and read pixel data to it. */ if ((data = (ImfRgba *) malloc((*w) * (*h) * sizeof (ImfRgba)))) { ImfInputSetFrameBuffer(file, data - x0 - y0 * (*w), 1, (*w)); ImfInputReadPixels (file, y0, y1); /* Allocate final storage and copy pixel data to it. */ if ((p = (float *) malloc((*w) * (*h) * (*c) * (*b)))) { for (i = 0; i < (*w) * (*h); ++i) { p[i * 4 + 0] = ImfHalfToFloat(data[i].r); p[i * 4 + 1] = ImfHalfToFloat(data[i].g); p[i * 4 + 2] = ImfHalfToFloat(data[i].b); p[i * 4 + 3] = ImfHalfToFloat(data[i].a); } } free(data); } } ImfCloseInputFile(file); } return p; }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d E X R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadEXRImage reads an image in the high dynamic-range (HDR) file format % developed by Industrial Light & Magic. It allocates the memory necessary % for the new Image structure and returns a pointer to the new image. % % The format of the ReadEXRImage method is: % % Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadEXRImage(const ImageInfo *image_info,ExceptionInfo *exception) { const ImfHeader *hdr_info; Image *image; ImageInfo *read_info; ImfInputFile *file; ImfRgba *scanline; int max_x, max_y, min_x, min_y; MagickBooleanType status; register ssize_t x; register Quantum *q; ssize_t y; /* Open image. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); image=AcquireImage(image_info,exception); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) { image=DestroyImageList(image); return((Image *) NULL); } read_info=CloneImageInfo(image_info); if (IsPathAccessible(read_info->filename) == MagickFalse) { (void) AcquireUniqueFilename(read_info->filename); (void) ImageToFile(image,read_info->filename,exception); } file=ImfOpenInputFile(read_info->filename); if (file == (ImfInputFile *) NULL) { ThrowFileException(exception,BlobError,"UnableToOpenBlob", ImfErrorMessage()); read_info=DestroyImageInfo(read_info); return((Image *) NULL); } hdr_info=ImfInputHeader(file); ImfHeaderDataWindow(hdr_info,&min_x,&min_y,&max_x,&max_y); image->columns=max_x-min_x+1UL; image->rows=max_y-min_y+1UL; image->matte=MagickTrue; if (image_info->ping != MagickFalse) { (void) ImfCloseInputFile(file); if (LocaleCompare(image_info->filename,read_info->filename) != 0) (void) RelinquishUniqueFileResource(read_info->filename); read_info=DestroyImageInfo(read_info); (void) CloseBlob(image); return(GetFirstImageInList(image)); } scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline)); if (scanline == (ImfRgba *) NULL) { (void) ImfCloseInputFile(file); ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } for (y=0; y < (ssize_t) image->rows; y++) { q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (Quantum *) NULL) break; ImfInputSetFrameBuffer(file,scanline-min_x-image->columns*(min_y+y),1, image->columns); ImfInputReadPixels(file,min_y+y,min_y+y); for (x=0; x < (ssize_t) image->columns; x++) { SetPixelRed(image,ClampToQuantum((MagickRealType) QuantumRange* ImfHalfToFloat(scanline[x].r)),q); SetPixelGreen(image,ClampToQuantum((MagickRealType) QuantumRange* ImfHalfToFloat(scanline[x].g)),q); SetPixelBlue(image,ClampToQuantum((MagickRealType) QuantumRange* ImfHalfToFloat(scanline[x].b)),q); SetPixelAlpha(image,ClampToQuantum((MagickRealType) QuantumRange* ImfHalfToFloat(scanline[x].a)),q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) break; } scanline=(ImfRgba *) RelinquishMagickMemory(scanline); (void) ImfCloseInputFile(file); if (LocaleCompare(image_info->filename,read_info->filename) != 0) (void) RelinquishUniqueFileResource(read_info->filename); read_info=DestroyImageInfo(read_info); (void) CloseBlob(image); return(GetFirstImageInList(image)); }