int openexr_dd_open(const char *name, int width, int height, int bits, RtToken component, const char *format) { #if HAVE_OPENEXR int i; ImfHeader *header; (void)bits; if (strcmp(format, "float") != 0 ) { ri_log(LOG_ERROR, "currently openexr output supports float format"); return 0; } if (strcmp(component, RI_RGB) != 0) { ri_log(LOG_WARN, "currently only supports rgb component"); } header = ImfNewHeader(); ImfHeaderSetDisplayWindow(header, 0, 0, width - 1 , height - 1); ImfHeaderSetDataWindow(header, 0, 0, width - 1, height - 1); ImfHeaderSetScreenWindowWidth(header, width); ImfHeaderSetStringAttribute(header, "owner", "lucille"); gfp = ImfOpenOutputFile(name, header, IMF_WRITE_RGBA); if (!gfp) { printf("[error] Can't open file [ %s ] for save.\n", name); return 0; } ImfDeleteHeader(header); gbuf = (ImfRgba *)ri_mem_alloc(width * height * sizeof(ImfRgba)); for (i = 0; i < width * height; i++) { ImfFloatToHalf(0.0f, &gbuf[i].r); ImfFloatToHalf(0.0f, &gbuf[i].g); ImfFloatToHalf(0.0f, &gbuf[i].b); ImfFloatToHalf(1.0f, &gbuf[i].a); } ImfOutputSetFrameBuffer(gfp, gbuf, /* pointer to pixels */ 1, /* xstride */ width); /* ystride */ gwidth = width; gheight = height; #endif return 1; }
void image_write_exr(const char *name, int w, int h, int c, int b, void *p) { ImfOutputFile *file; ImfRgba *data; ImfHeader *head; const float *q = (const float *) p; /* Allocation and intialize a new header. */ if ((head = ImfNewHeader())) { ImfHeaderSetDataWindow (head, 0, 0, w - 1, h - 1); ImfHeaderSetDisplayWindow(head, 0, 0, w - 1, h - 1); ImfHeaderSetCompression (head, IMF_ZIP_COMPRESSION); ImfHeaderSetLineOrder (head, IMF_INCREASING_Y); if ((file = ImfOpenOutputFile(name, head, IMF_WRITE_RGBA))) { /* Allocate temporary storage and copy pixel data to it. */ if ((data = (ImfRgba *) calloc(w * h, sizeof (ImfRgba)))) { int i; for (i = 0; i < w * h; ++i) { float R = (c > 0) ? q[i * c + 0] : 0.0f; float G = (c > 1) ? q[i * c + 1] : 0.0f; float B = (c > 2) ? q[i * c + 2] : 0.0f; float A = (c > 3) ? q[i * c + 3] : 1.0f; ImfFloatToHalf(R, &data[i].r); ImfFloatToHalf(G, &data[i].g); ImfFloatToHalf(B, &data[i].b); ImfFloatToHalf(A, &data[i].a); } /* Write the file. */ ImfOutputSetFrameBuffer(file, data, 1, w); ImfOutputWritePixels (file, h); free(data); } ImfCloseOutputFile(file); } ImfDeleteHeader(head); } }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e E X R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteEXRImage() writes an image to a file the in the high dynamic-range % (HDR) file format developed by Industrial Light & Magic. % % The format of the WriteEXRImage method is: % % MagickBooleanType WriteEXRImage(const ImageInfo *image_info, % Image *image,ExceptionInfo *exception) % % A description of each parameter follows. % % o image_info: the image info. % % o image: The image. % % o exception: return any errors or warnings in this structure. % */ static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image, ExceptionInfo *exception) { ImageInfo *write_info; ImfHalf half_quantum; ImfHeader *hdr_info; ImfOutputFile *file; ImfRgba *scanline; int compression; MagickBooleanType status; register const Quantum *p; register ssize_t x; ssize_t y; /* Open output image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); if (image->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); if (status == MagickFalse) return(status); write_info=CloneImageInfo(image_info); (void) AcquireUniqueFilename(write_info->filename); hdr_info=ImfNewHeader(); ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int) image->rows-1); ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int) image->rows-1); compression=IMF_NO_COMPRESSION; if (write_info->compression == ZipSCompression) compression=IMF_ZIPS_COMPRESSION; if (write_info->compression == ZipCompression) compression=IMF_ZIP_COMPRESSION; if (write_info->compression == PizCompression) compression=IMF_PIZ_COMPRESSION; if (write_info->compression == Pxr24Compression) compression=IMF_PXR24_COMPRESSION; #if defined(B44Compression) if (write_info->compression == B44Compression) compression=IMF_B44_COMPRESSION; #endif #if defined(B44ACompression) if (write_info->compression == B44ACompression) compression=IMF_B44A_COMPRESSION; #endif ImfHeaderSetCompression(hdr_info,compression); ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y); file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA); ImfDeleteHeader(hdr_info); if (file == (ImfOutputFile *) NULL) { ThrowFileException(exception,BlobError,"UnableToOpenBlob", ImfErrorMessage()); write_info=DestroyImageInfo(write_info); return(MagickFalse); } scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline)); if (scanline == (ImfRgba *) NULL) { (void) ImfCloseOutputFile(file); ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); } for (y=0; y < (ssize_t) image->rows; y++) { p=GetVirtualPixels(image,0,y,image->columns,1,exception); if (p == (const Quantum *) NULL) break; for (x=0; x < (ssize_t) image->columns; x++) { ImfFloatToHalf(QuantumScale*GetPixelRed(image,p),&half_quantum); scanline[x].r=half_quantum; ImfFloatToHalf(QuantumScale*GetPixelGreen(image,p),&half_quantum); scanline[x].g=half_quantum; ImfFloatToHalf(QuantumScale*GetPixelBlue(image,p),&half_quantum); scanline[x].b=half_quantum; if (image->matte == MagickFalse) ImfFloatToHalf(1.0,&half_quantum); else ImfFloatToHalf(QuantumScale*GetPixelAlpha(image,p),&half_quantum); scanline[x].a=half_quantum; p+=GetPixelChannels(image); } ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns); ImfOutputWritePixels(file,1); } (void) ImfCloseOutputFile(file); scanline=(ImfRgba *) RelinquishMagickMemory(scanline); (void) FileToImage(image,write_info->filename,exception); (void) RelinquishUniqueFileResource(write_info->filename); write_info=DestroyImageInfo(write_info); (void) CloseBlob(image); return(MagickTrue); }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % W r i t e E X R I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % WriteEXRImage() writes an image to a file the in the high dynamic-range % (HDR) file format developed by Industrial Light & Magic. % % The format of the WriteEXRImage method is: % % MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image) % % A description of each parameter follows. % % o image_info: the image info. % % o image: The image. % */ static MagickBooleanType WriteEXRImage(const ImageInfo *image_info,Image *image) { ImageInfo *write_info; ImfHalf half_quantum; ImfHeader *hdr_info; ImfOutputFile *file; ImfRgba *scanline; long y; MagickBooleanType status; register const PixelPacket *p; register long x; /* Open output image file. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); if (image->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == MagickFalse) return(status); write_info=CloneImageInfo(image_info); (void) AcquireUniqueFilename(write_info->filename); hdr_info=ImfNewHeader(); ImfHeaderSetDataWindow(hdr_info,0,0,(int) image->columns-1,(int) image->rows-1); ImfHeaderSetDisplayWindow(hdr_info,0,0,(int) image->columns-1,(int) image->rows-1); ImfHeaderSetCompression(hdr_info,write_info->compression == NoCompression ? IMF_NO_COMPRESSION : IMF_PIZ_COMPRESSION); ImfHeaderSetLineOrder(hdr_info,IMF_INCREASING_Y); file=ImfOpenOutputFile(write_info->filename,hdr_info,IMF_WRITE_RGBA); ImfDeleteHeader(hdr_info); if (file == (ImfOutputFile *) NULL) { ThrowFileException(&image->exception,BlobError,"UnableToOpenBlob", ImfErrorMessage()); write_info=DestroyImageInfo(write_info); return(MagickFalse); } scanline=(ImfRgba *) AcquireQuantumMemory(image->columns,sizeof(*scanline)); if (scanline == (ImfRgba *) NULL) { (void) ImfCloseOutputFile(file); ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); } for (y=0; y < (long) image->rows; y++) { p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); if (p == (const PixelPacket *) NULL) break; for (x=0; x < (long) image->columns; x++) { ImfFloatToHalf(QuantumScale*p->red,&half_quantum); scanline[x].r=half_quantum; ImfFloatToHalf(QuantumScale*p->green,&half_quantum); scanline[x].g=half_quantum; ImfFloatToHalf(QuantumScale*p->blue,&half_quantum); scanline[x].b=half_quantum; if (image->matte == MagickFalse) ImfFloatToHalf(1.0,&half_quantum); else ImfFloatToHalf(1.0-QuantumScale*p->opacity,&half_quantum); scanline[x].a=half_quantum; p++; } ImfOutputSetFrameBuffer(file,scanline-(y*image->columns),1,image->columns); ImfOutputWritePixels(file,1); } (void) ImfCloseOutputFile(file); scanline=(ImfRgba *) RelinquishMagickMemory(scanline); (void) FileToImage(image,write_info->filename); (void) RelinquishUniqueFileResource(write_info->filename); write_info=DestroyImageInfo(write_info); (void) CloseBlob(image); return(MagickTrue); }
int main() { const char *software = "microexr"; const char *comment = "a simple test case for the microexr library"; ImfHeader *header = ImfNewHeader(); ImfHeaderSetType(header, IMF_DEEPSCANLINE); ImfHeaderSetCompression(header, IMF_ZIPS_COMPRESSION ); ImfHeaderSetStringAttribute( header, "software", software ); ImfHeaderSetStringAttribute( header, "comment", comment ); ImfHeaderSetDataWindow( header, 0, 0, XRES-1, YRES-1 ); ImfHeaderSetDisplayWindow( header, 0, 0, XRES-1, YRES-1 ); ImfHeaderSetPixelAspectRatio( header, 1.0f ); ImfHeaderSetScreenWindowCenter(header, 0.0, 0.0); ImfHeaderSetScreenWindowWidth(header, 1.0); ImfHeaderSetLineOrder( header, IMF_INCREASING_Y ); ImfOutputFile *exr = ImfOpenOutputFile( "deepgradient.exr", header, IMF_WRITE_RGBAZZB ); if( !exr ) { perror( "gradient: unable to open 'gradient.exr' for writing" ); exit( 1 ); } /* Write a simple gradient in a framebuffer. */ void **fb = new void*[XRES*6]; /* Fast access to half and float samples of the framebuffer */ ImfHalf **half_fb = (ImfHalf **)fb; float **float_fb = (float **)fb; unsigned int *samples = new unsigned int[XRES]; for(unsigned i=0; i<XRES; i++) { samples[i]=1; fb[i*6 + 0] = new ImfHalf[samples[i]]; fb[i*6 + 1] = new ImfHalf[samples[i]]; fb[i*6 + 2] = new ImfHalf[samples[i]]; fb[i*6 + 3] = new ImfHalf[samples[i]]; fb[i*6 + 4] = new float[samples[i]]; fb[i*6 + 5] = new float[samples[i]]; } for( unsigned j=0; j<YRES; j++ ) { float g = float(j) / float(YRES-1); assert( g<=1.0f ); for( unsigned i=0; i<XRES; i++ ) { float r = float(i) / float(XRES-1); for(unsigned s=0; s<samples[i]; s++) { ImfFloatToHalf( r, half_fb[i*6 + 0]+s ); ImfFloatToHalf( g, half_fb[i*6 + 1]+s ); ImfFloatToHalf(0.5f, half_fb[i*6 + 2]+s ); ImfFloatToHalf(0.9f, half_fb[i*6 + 3]+s ); float z = (g-0.5)*(g-0.5) + (r-0.5)*(r-0.5); z = 10*sqrt(z); z = 10-z; float_fb[i*6 + 4][s] = z; float_fb[i*6 + 5][s] = z+0.01; } } ImfOutputSetDeepFrameBuffer( exr, samples, (const char **)fb, 6*sizeof(fb), 0, sizeof(fb) ); ImfOutputWritePixels( exr, 1 ); } ImfCloseOutputFile( exr ); ImfDeleteHeader( header ); delete [] samples; for(unsigned i=0; i<XRES; i++) { delete half_fb[i*6 + 0]; delete half_fb[i*6 + 1]; delete half_fb[i*6 + 2]; delete half_fb[i*6 + 3]; delete float_fb[i*6 + 4]; delete float_fb[i*6 + 5]; } delete [] fb; exit( 0 ); }