exrinput.cpp
Go to the documentation of this file.00001 /** 00002 * Copyright (C) 2003 Billy Biggs <vektor@dumbterm.net>. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining 00005 * a copy of this software and associated documentation files (the 00006 * "Software"), to deal in the Software without restriction, including 00007 * without limitation the rights to use, copy, modify, merge, publish, 00008 * distribute, sublicense, and/or sell copies of the Software, and to 00009 * permit persons to whom the Software is furnished to do so, subject to 00010 * the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be 00013 * included in all copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00016 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00018 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 00019 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 00020 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00021 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00022 * SOFTWARE. 00023 */ 00024 00025 #ifdef HAVE_OPENEXR 00026 00027 #include <stdio.h> 00028 #include <OpenEXR/ImfCRgbaFile.h> 00029 #include "exrinput.h" 00030 00031 using namespace Imf; 00032 00033 struct exrinput_s { 00034 ImfInputFile *file; 00035 ImfRgba *base; 00036 int width; 00037 int height; 00038 double *scanline; 00039 }; 00040 00041 exrinput_t *exrinput_new( const char *filename ) 00042 { 00043 exrinput_t *exrinput = malloc( sizeof( exrinput_t ) ); 00044 int xmin, xmax, ymin, ymax; 00045 const ImfHeader *header; 00046 00047 if( !exrinput ) { 00048 fprintf( stderr, "exrinput: Cannot allocate memory.\n" ); 00049 return 0; 00050 } 00051 00052 exrinput->file = ImfOpenInputFile( filename ); 00053 if( !exrinput->file ) { 00054 fprintf( stderr, "exrinput: %s\n", ImfErrorMessage() ); 00055 free( exrinput ); 00056 return 0; 00057 } 00058 00059 header = ImfInputHeader( exrinput->file ); 00060 ImfHeaderDataWindow( header, &xmin, &ymin, &xmax, &ymax ); 00061 exrinput->width = xmax - xmin + 1; 00062 exrinput->height = ymax - ymin + 1; 00063 00064 exrinput->base = malloc( exrinput->width * sizeof( ImfRgba ) ); 00065 exrinput->scanline = malloc( exrinput->width * sizeof( double ) * 4 ); 00066 if( !exrinput->base || !exrinput->scanline ) { 00067 fprintf( stderr, "exrinput: Cannot allocate memory.\n" ); 00068 if( exrinput->scanline ) free( exrinput->scanline ); 00069 if( exrinput->base ) free( exrinput->base ); 00070 ImfCloseInputFile( exrinput->file ); 00071 free( exrinput ); 00072 return 0; 00073 } 00074 00075 return exrinput; 00076 } 00077 00078 void exrinput_delete( exrinput_t *exrinput ) 00079 { 00080 ImfCloseInputFile( exrinput->file ); 00081 free( exrinput->scanline ); 00082 free( exrinput->base ); 00083 free( exrinput ); 00084 } 00085 00086 unsigned int exrinput_get_width( exrinput_t *exrinput ) 00087 { 00088 return exrinput->width; 00089 } 00090 00091 unsigned int exrinput_get_height( exrinput_t *exrinput ) 00092 { 00093 return exrinput->height; 00094 } 00095 00096 double *exrinput_get_scanline( exrinput_t *exrinput, int num ) 00097 { 00098 ImfRgba *inscanline = exrinput->base; 00099 double *outscanline = exrinput->scanline; 00100 int i; 00101 00102 ImfInputSetFrameBuffer( exrinput->file, exrinput->base - (num * exrinput->width), 1, exrinput->width ); 00103 ImfInputReadPixels( exrinput->file, num, num ); 00104 00105 for( i = 0; i < exrinput->width; i++ ) { 00106 float temp; 00107 temp = ImfHalfToFloat( inscanline[ i ].r ); 00108 *outscanline++ = temp; 00109 temp = ImfHalfToFloat( inscanline[ i ].g ); 00110 *outscanline++ = temp; 00111 temp = ImfHalfToFloat( inscanline[ i ].b ); 00112 *outscanline++ = temp; 00113 temp = ImfHalfToFloat( inscanline[ i ].a ); 00114 *outscanline++ = temp; 00115 } 00116 00117 return exrinput->scanline; 00118 } 00119 00120 #endif /* HAVE_OPENEXR */ 00121
Generated on 28 Feb 2009 for Milton by
1.5.6