|
Blender V2.27
|
00001 00029 #include <stdlib.h> 00030 #include <stdio.h> 00031 #include <stddef.h> 00032 #include <string> 00033 00034 00035 #include <openexr_api.h> 00036 00037 extern "C" 00038 { 00039 00040 // The following prevents a linking error in debug mode for MSVC using the libs in CVS 00041 #if defined(WITH_OPENEXR) && defined(_WIN32) && defined(_DEBUG) && !defined(__MINGW32__) && !defined(__CYGWIN__) 00042 _CRTIMP void __cdecl _invalid_parameter_noinfo(void) 00043 { 00044 } 00045 #endif 00046 00047 #include "MEM_guardedalloc.h" 00048 00049 #include "BLI_blenlib.h" 00050 #include "BLI_math_color.h" 00051 00052 #include "IMB_imbuf_types.h" 00053 #include "IMB_imbuf.h" 00054 #include "IMB_allocimbuf.h" 00055 #include "IMB_metadata.h" 00056 00057 #include "openexr_multi.h" 00058 } 00059 00060 #include <iostream> 00061 00062 #if defined (_WIN32) && !defined(FREE_WINDOWS) 00063 #include <half.h> 00064 #include <IlmImf/ImfVersion.h> 00065 #include <IlmImf/ImfArray.h> 00066 #include <IlmImf/ImfIO.h> 00067 #include <IlmImf/ImfChannelList.h> 00068 #include <IlmImf/ImfPixelType.h> 00069 #include <IlmImf/ImfInputFile.h> 00070 #include <IlmImf/ImfOutputFile.h> 00071 #include <IlmImf/ImfCompression.h> 00072 #include <IlmImf/ImfCompressionAttribute.h> 00073 #include <IlmImf/ImfStringAttribute.h> 00074 #include <Imath/ImathBox.h> 00075 #else 00076 #include <OpenEXR/half.h> 00077 #include <OpenEXR/ImfVersion.h> 00078 #include <OpenEXR/ImathBox.h> 00079 #include <OpenEXR/ImfArray.h> 00080 #include <OpenEXR/ImfIO.h> 00081 #include <OpenEXR/ImfChannelList.h> 00082 #include <OpenEXR/ImfPixelType.h> 00083 #include <OpenEXR/ImfInputFile.h> 00084 #include <OpenEXR/ImfOutputFile.h> 00085 #include <OpenEXR/ImfCompression.h> 00086 #include <OpenEXR/ImfCompressionAttribute.h> 00087 #include <OpenEXR/ImfStringAttribute.h> 00088 #endif 00089 00090 using namespace Imf; 00091 using namespace Imath; 00092 00093 class Mem_IStream: public IStream 00094 { 00095 public: 00096 00097 Mem_IStream (unsigned char *exrbuf, size_t exrsize): 00098 IStream("dummy"), _exrpos (0), _exrsize(exrsize) { _exrbuf = exrbuf; } 00099 00100 virtual bool read (char c[], int n); 00101 virtual Int64 tellg (); 00102 virtual void seekg (Int64 pos); 00103 virtual void clear (); 00104 //virtual ~Mem_IStream() {}; // unused 00105 00106 private: 00107 00108 Int64 _exrpos; 00109 Int64 _exrsize; 00110 unsigned char *_exrbuf; 00111 }; 00112 00113 bool Mem_IStream::read (char c[], int n) 00114 { 00115 if (n + _exrpos <= _exrsize) 00116 { 00117 memcpy(c, (void *)(&_exrbuf[_exrpos]), n); 00118 _exrpos += n; 00119 return true; 00120 } 00121 else 00122 return false; 00123 } 00124 00125 Int64 Mem_IStream::tellg () 00126 { 00127 return _exrpos; 00128 } 00129 00130 void Mem_IStream::seekg (Int64 pos) 00131 { 00132 _exrpos = pos; 00133 } 00134 00135 void Mem_IStream::clear () 00136 { 00137 } 00138 00139 struct _RGBAZ 00140 { 00141 half r; 00142 half g; 00143 half b; 00144 half a; 00145 half z; 00146 }; 00147 00148 typedef struct _RGBAZ RGBAZ; 00149 00150 extern "C" 00151 { 00152 00153 int imb_is_a_openexr(unsigned char *mem) 00154 { 00155 return Imf::isImfMagic ((const char *)mem); 00156 } 00157 00158 static void openexr_header_compression(Header *header, int compression) 00159 { 00160 switch(compression) 00161 { 00162 case 0: 00163 header->compression() = NO_COMPRESSION; 00164 break; 00165 case 1: 00166 header->compression() = PXR24_COMPRESSION; 00167 break; 00168 case 2: 00169 header->compression() = ZIP_COMPRESSION; 00170 break; 00171 case 3: 00172 header->compression() = PIZ_COMPRESSION; 00173 break; 00174 case 4: 00175 header->compression() = RLE_COMPRESSION; 00176 break; 00177 default: 00178 header->compression() = ZIP_COMPRESSION; 00179 break; 00180 } 00181 } 00182 00183 static void openexr_header_metadata(Header *header, struct ImBuf *ibuf) 00184 { 00185 ImMetaData* info; 00186 00187 for(info= ibuf->metadata; info; info= info->next) 00188 header->insert(info->key, StringAttribute(info->value)); 00189 } 00190 00191 static int imb_save_openexr_half(struct ImBuf *ibuf, const char *name, int flags) 00192 { 00193 int channels = ibuf->channels; 00194 int width = ibuf->x; 00195 int height = ibuf->y; 00196 int write_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != NULL; // summarize 00197 00198 try 00199 { 00200 Header header (width, height); 00201 00202 openexr_header_compression(&header, ibuf->ftype & OPENEXR_COMPRESS); 00203 openexr_header_metadata(&header, ibuf); 00204 00205 header.channels().insert ("R", Channel (HALF)); 00206 header.channels().insert ("G", Channel (HALF)); 00207 header.channels().insert ("B", Channel (HALF)); 00208 if (ibuf->depth==32 && channels >= 4) 00209 header.channels().insert ("A", Channel (HALF)); 00210 if (write_zbuf) // z we do as float always 00211 header.channels().insert ("Z", Channel (FLOAT)); 00212 00213 FrameBuffer frameBuffer; 00214 OutputFile *file = new OutputFile(name, header); 00215 00216 /* we store first everything in half array */ 00217 RGBAZ *pixels = new RGBAZ[height * width]; 00218 RGBAZ *to = pixels; 00219 int xstride= sizeof (RGBAZ); 00220 int ystride= xstride*width; 00221 00222 /* indicate used buffers */ 00223 frameBuffer.insert ("R", Slice (HALF, (char *) &pixels[0].r, xstride, ystride)); 00224 frameBuffer.insert ("G", Slice (HALF, (char *) &pixels[0].g, xstride, ystride)); 00225 frameBuffer.insert ("B", Slice (HALF, (char *) &pixels[0].b, xstride, ystride)); 00226 if (ibuf->depth==32 && channels >= 4) 00227 frameBuffer.insert ("A", Slice (HALF, (char *) &pixels[0].a, xstride, ystride)); 00228 if (write_zbuf) 00229 frameBuffer.insert ("Z", Slice (FLOAT, (char *)(ibuf->zbuf_float + (height-1)*width), 00230 sizeof(float), sizeof(float) * -width)); 00231 if(ibuf->rect_float) { 00232 float *from; 00233 00234 if(ibuf->profile == IB_PROFILE_LINEAR_RGB) { 00235 for (int i = ibuf->y-1; i >= 0; i--) 00236 { 00237 from= ibuf->rect_float + channels*i*width; 00238 00239 for (int j = ibuf->x; j > 0; j--) 00240 { 00241 to->r = from[0]; 00242 to->g = from[1]; 00243 to->b = from[2]; 00244 to->a = (channels >= 4)? from[3]: 1.0f; 00245 to++; from += 4; 00246 } 00247 } 00248 } 00249 else { 00250 for (int i = ibuf->y-1; i >= 0; i--) 00251 { 00252 from= ibuf->rect_float + channels*i*width; 00253 00254 for (int j = ibuf->x; j > 0; j--) 00255 { 00256 to->r = srgb_to_linearrgb(from[0]); 00257 to->g = srgb_to_linearrgb(from[1]); 00258 to->b = srgb_to_linearrgb(from[2]); 00259 to->a = (channels >= 4)? from[3]: 1.0f; 00260 to++; from += 4; 00261 } 00262 } 00263 } 00264 } 00265 else { 00266 unsigned char *from; 00267 00268 if(ibuf->profile == IB_PROFILE_LINEAR_RGB) { 00269 for (int i = ibuf->y-1; i >= 0; i--) 00270 { 00271 from= (unsigned char *)ibuf->rect + channels*i*width; 00272 00273 for (int j = ibuf->x; j > 0; j--) 00274 { 00275 to->r = (float)(from[0])/255.0; 00276 to->g = (float)(from[1])/255.0; 00277 to->b = (float)(from[2])/255.0; 00278 to->a = (float)(channels >= 4) ? from[3]/255.0 : 1.0f; 00279 to++; from += 4; 00280 } 00281 } 00282 } 00283 else { 00284 for (int i = ibuf->y-1; i >= 0; i--) 00285 { 00286 from= (unsigned char *)ibuf->rect + channels*i*width; 00287 00288 for (int j = ibuf->x; j > 0; j--) 00289 { 00290 to->r = srgb_to_linearrgb((float)from[0] / 255.0); 00291 to->g = srgb_to_linearrgb((float)from[1] / 255.0); 00292 to->b = srgb_to_linearrgb((float)from[2] / 255.0); 00293 to->a = channels >= 4 ? (float)from[3]/255.0 : 1.0f; 00294 to++; from += 4; 00295 } 00296 } 00297 } 00298 } 00299 00300 // printf("OpenEXR-save: Writing OpenEXR file of height %d.\n", height); 00301 00302 file->setFrameBuffer (frameBuffer); 00303 file->writePixels (height); 00304 delete file; 00305 delete [] pixels; 00306 } 00307 catch (const std::exception &exc) 00308 { 00309 printf("OpenEXR-save: ERROR: %s\n", exc.what()); 00310 if (ibuf) IMB_freeImBuf(ibuf); 00311 00312 return (0); 00313 } 00314 00315 return (1); 00316 } 00317 00318 static int imb_save_openexr_float(struct ImBuf *ibuf, const char *name, int flags) 00319 { 00320 int channels = ibuf->channels; 00321 int width = ibuf->x; 00322 int height = ibuf->y; 00323 int write_zbuf = (flags & IB_zbuffloat) && ibuf->zbuf_float != NULL; // summarize 00324 00325 try 00326 { 00327 Header header (width, height); 00328 00329 openexr_header_compression(&header, ibuf->ftype & OPENEXR_COMPRESS); 00330 openexr_header_metadata(&header, ibuf); 00331 00332 header.channels().insert ("R", Channel (FLOAT)); 00333 header.channels().insert ("G", Channel (FLOAT)); 00334 header.channels().insert ("B", Channel (FLOAT)); 00335 if (ibuf->depth==32 && channels >= 4) 00336 header.channels().insert ("A", Channel (FLOAT)); 00337 if (write_zbuf) 00338 header.channels().insert ("Z", Channel (FLOAT)); 00339 00340 FrameBuffer frameBuffer; 00341 OutputFile *file = new OutputFile(name, header); 00342 int xstride = sizeof(float) * channels; 00343 int ystride = - xstride*width; 00344 float *rect[4] = {NULL, NULL, NULL, NULL}; 00345 00346 /* last scanline, stride negative */ 00347 rect[0]= ibuf->rect_float + channels*(height-1)*width; 00348 rect[1]= rect[0]+1; 00349 rect[2]= rect[0]+2; 00350 rect[3]= (channels >= 4)? rect[0]+3:rect[0]; /* red as alpha, is this needed since alpha isnt written? */ 00351 00352 frameBuffer.insert ("R", Slice (FLOAT, (char *)rect[0], xstride, ystride)); 00353 frameBuffer.insert ("G", Slice (FLOAT, (char *)rect[1], xstride, ystride)); 00354 frameBuffer.insert ("B", Slice (FLOAT, (char *)rect[2], xstride, ystride)); 00355 if (ibuf->depth==32 && channels >= 4) 00356 frameBuffer.insert ("A", Slice (FLOAT, (char *)rect[3], xstride, ystride)); 00357 if (write_zbuf) 00358 frameBuffer.insert ("Z", Slice (FLOAT, (char *) (ibuf->zbuf_float + (height-1)*width), 00359 sizeof(float), sizeof(float) * -width)); 00360 file->setFrameBuffer (frameBuffer); 00361 file->writePixels (height); 00362 delete file; 00363 } 00364 catch (const std::exception &exc) 00365 { 00366 printf("OpenEXR-save: ERROR: %s\n", exc.what()); 00367 if (ibuf) IMB_freeImBuf(ibuf); 00368 00369 return (0); 00370 } 00371 00372 return (1); 00373 // printf("OpenEXR-save: Done.\n"); 00374 } 00375 00376 00377 int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) 00378 { 00379 if (flags & IB_mem) 00380 { 00381 printf("OpenEXR-save: Create EXR in memory CURRENTLY NOT SUPPORTED !\n"); 00382 imb_addencodedbufferImBuf(ibuf); 00383 ibuf->encodedsize = 0; 00384 return(0); 00385 } 00386 00387 if (ibuf->ftype & OPENEXR_HALF) 00388 return imb_save_openexr_half(ibuf, name, flags); 00389 else { 00390 /* when no float rect, we save as half (16 bits is sufficient) */ 00391 if (ibuf->rect_float==NULL) 00392 return imb_save_openexr_half(ibuf, name, flags); 00393 else 00394 return imb_save_openexr_float(ibuf, name, flags); 00395 } 00396 } 00397 00398 /* ********************* Nicer API, MultiLayer and with Tile file support ************************************ */ 00399 00400 /* naming rules: 00401 - parse name from right to left 00402 - last character is channel ID, 1 char like 'A' 'R' 'G' 'B' 'X' 'Y' 'Z' 'W' 'U' 'V' 00403 - separated with a dot; the Pass name (like "Depth", "Color", "Diffuse" or "Combined") 00404 - separated with a dot: the Layer name (like "Lamp1" or "Walls" or "Characters") 00405 */ 00406 00407 static ListBase exrhandles= {NULL, NULL}; 00408 00409 typedef struct ExrHandle { 00410 struct ExrHandle *next, *prev; 00411 00412 InputFile *ifile; 00413 TiledOutputFile *tofile; 00414 OutputFile *ofile; 00415 int tilex, tiley; 00416 int width, height; 00417 int mipmap; 00418 00419 ListBase channels; /* flattened out, ExrChannel */ 00420 ListBase layers; /* hierarchical, pointing in end to ExrChannel */ 00421 } ExrHandle; 00422 00423 /* flattened out channel */ 00424 typedef struct ExrChannel { 00425 struct ExrChannel *next, *prev; 00426 00427 char name[EXR_TOT_MAXNAME+1]; /* full name of layer+pass */ 00428 int xstride, ystride; /* step to next pixel, to next scanline */ 00429 float *rect; /* first pointer to write in */ 00430 char chan_id; /* quick lookup of channel char */ 00431 } ExrChannel; 00432 00433 00434 /* hierarchical; layers -> passes -> channels[] */ 00435 typedef struct ExrPass { 00436 struct ExrPass *next, *prev; 00437 char name[EXR_PASS_MAXNAME]; 00438 int totchan; 00439 float *rect; 00440 struct ExrChannel *chan[EXR_PASS_MAXCHAN]; 00441 char chan_id[EXR_PASS_MAXCHAN]; 00442 } ExrPass; 00443 00444 typedef struct ExrLayer { 00445 struct ExrLayer *next, *prev; 00446 char name[EXR_LAY_MAXNAME+1]; 00447 ListBase passes; 00448 } ExrLayer; 00449 00450 /* ********************** */ 00451 00452 void *IMB_exr_get_handle(void) 00453 { 00454 ExrHandle *data= (ExrHandle *)MEM_callocN(sizeof(ExrHandle), "exr handle"); 00455 BLI_addtail(&exrhandles, data); 00456 return data; 00457 } 00458 00459 /* adds flattened ExrChannels */ 00460 /* xstride, ystride and rect can be done in set_channel too, for tile writing */ 00461 void IMB_exr_add_channel(void *handle, const char *layname, const char *passname, int xstride, int ystride, float *rect) 00462 { 00463 ExrHandle *data= (ExrHandle *)handle; 00464 ExrChannel *echan; 00465 00466 echan= (ExrChannel *)MEM_callocN(sizeof(ExrChannel), "exr tile channel"); 00467 00468 if(layname) { 00469 char lay[EXR_LAY_MAXNAME+1], pass[EXR_PASS_MAXNAME+1]; 00470 BLI_strncpy(lay, layname, EXR_LAY_MAXNAME); 00471 BLI_strncpy(pass, passname, EXR_PASS_MAXNAME); 00472 00473 sprintf(echan->name, "%s.%s", lay, pass); 00474 } 00475 else 00476 BLI_strncpy(echan->name, passname, EXR_TOT_MAXNAME-1); 00477 00478 echan->xstride= xstride; 00479 echan->ystride= ystride; 00480 echan->rect= rect; 00481 00482 // printf("added channel %s\n", echan->name); 00483 BLI_addtail(&data->channels, echan); 00484 } 00485 00486 /* only used for writing temp. render results (not image files) */ 00487 void IMB_exr_begin_write(void *handle, const char *filename, int width, int height, int compress) 00488 { 00489 ExrHandle *data= (ExrHandle *)handle; 00490 Header header (width, height); 00491 ExrChannel *echan; 00492 00493 data->width= width; 00494 data->height= height; 00495 00496 for(echan= (ExrChannel *)data->channels.first; echan; echan= echan->next) 00497 header.channels().insert (echan->name, Channel (FLOAT)); 00498 00499 openexr_header_compression(&header, compress); 00500 // openexr_header_metadata(&header, ibuf); // no imbuf. cant write 00501 /* header.lineOrder() = DECREASING_Y; this crashes in windows for file read! */ 00502 00503 header.insert ("BlenderMultiChannel", StringAttribute ("Blender V2.55.1 and newer")); 00504 00505 data->ofile = new OutputFile(filename, header); 00506 } 00507 00508 void IMB_exrtile_begin_write(void *handle, const char *filename, int mipmap, int width, int height, int tilex, int tiley) 00509 { 00510 ExrHandle *data= (ExrHandle *)handle; 00511 Header header (width, height); 00512 ExrChannel *echan; 00513 00514 data->tilex= tilex; 00515 data->tiley= tiley; 00516 data->width= width; 00517 data->height= height; 00518 data->mipmap= mipmap; 00519 00520 for(echan= (ExrChannel *)data->channels.first; echan; echan= echan->next) 00521 header.channels().insert (echan->name, Channel (FLOAT)); 00522 00523 header.setTileDescription (TileDescription (tilex, tiley, (mipmap)? MIPMAP_LEVELS: ONE_LEVEL)); 00524 header.lineOrder() = RANDOM_Y; 00525 header.compression() = RLE_COMPRESSION; 00526 00527 header.insert ("BlenderMultiChannel", StringAttribute ("Blender V2.43")); 00528 00529 data->tofile = new TiledOutputFile(filename, header); 00530 } 00531 00532 /* read from file */ 00533 int IMB_exr_begin_read(void *handle, const char *filename, int *width, int *height) 00534 { 00535 ExrHandle *data= (ExrHandle *)handle; 00536 00537 if(BLI_exists(filename) && BLI_filepathsize(filename)>32) { /* 32 is arbitrary, but zero length files crashes exr */ 00538 data->ifile = new InputFile(filename); 00539 if(data->ifile) { 00540 Box2i dw = data->ifile->header().dataWindow(); 00541 data->width= *width = dw.max.x - dw.min.x + 1; 00542 data->height= *height = dw.max.y - dw.min.y + 1; 00543 00544 const ChannelList &channels = data->ifile->header().channels(); 00545 00546 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) 00547 IMB_exr_add_channel(data, NULL, i.name(), 0, 0, NULL); 00548 00549 return 1; 00550 } 00551 } 00552 return 0; 00553 } 00554 00555 /* still clumsy name handling, layers/channels can be ordered as list in list later */ 00556 void IMB_exr_set_channel(void *handle, const char *layname, const char *passname, int xstride, int ystride, float *rect) 00557 { 00558 ExrHandle *data= (ExrHandle *)handle; 00559 ExrChannel *echan; 00560 char name[EXR_TOT_MAXNAME + 1]; 00561 00562 if(layname) { 00563 char lay[EXR_LAY_MAXNAME+1], pass[EXR_PASS_MAXNAME+1]; 00564 BLI_strncpy(lay, layname, EXR_LAY_MAXNAME); 00565 BLI_strncpy(pass, passname, EXR_PASS_MAXNAME); 00566 00567 sprintf(name, "%s.%s", lay, pass); 00568 } 00569 else 00570 BLI_strncpy(name, passname, EXR_TOT_MAXNAME-1); 00571 00572 echan= (ExrChannel *)BLI_findstring(&data->channels, name, offsetof(ExrChannel, name)); 00573 00574 if(echan) { 00575 echan->xstride= xstride; 00576 echan->ystride= ystride; 00577 echan->rect= rect; 00578 } 00579 else 00580 printf("IMB_exrtile_set_channel error %s\n", name); 00581 } 00582 00583 void IMB_exrtile_clear_channels(void *handle) 00584 { 00585 ExrHandle *data= (ExrHandle *)handle; 00586 BLI_freelistN(&data->channels); 00587 } 00588 00589 void IMB_exrtile_write_channels(void *handle, int partx, int party, int level) 00590 { 00591 ExrHandle *data= (ExrHandle *)handle; 00592 FrameBuffer frameBuffer; 00593 ExrChannel *echan; 00594 00595 for(echan= (ExrChannel *)data->channels.first; echan; echan= echan->next) { 00596 float *rect= echan->rect - echan->xstride*partx - echan->ystride*party; 00597 00598 frameBuffer.insert (echan->name, Slice (FLOAT, (char *)rect, 00599 echan->xstride*sizeof(float), echan->ystride*sizeof(float))); 00600 } 00601 00602 data->tofile->setFrameBuffer (frameBuffer); 00603 00604 try { 00605 // printf("write tile %d %d\n", partx/data->tilex, party/data->tiley); 00606 data->tofile->writeTile (partx/data->tilex, party/data->tiley, level); 00607 } 00608 catch (const std::exception &exc) { 00609 std::cerr << "OpenEXR-writeTile: ERROR: " << exc.what() << std::endl; 00610 } 00611 } 00612 00613 void IMB_exr_write_channels(void *handle) 00614 { 00615 ExrHandle *data= (ExrHandle *)handle; 00616 FrameBuffer frameBuffer; 00617 ExrChannel *echan; 00618 00619 if(data->channels.first) { 00620 for(echan= (ExrChannel *)data->channels.first; echan; echan= echan->next) { 00621 /* last scanline, stride negative */ 00622 float *rect = echan->rect + echan->xstride*(data->height-1)*data->width; 00623 00624 frameBuffer.insert (echan->name, Slice (FLOAT, (char *)rect, 00625 echan->xstride*sizeof(float), -echan->ystride*sizeof(float))); 00626 } 00627 00628 data->ofile->setFrameBuffer (frameBuffer); 00629 try { 00630 data->ofile->writePixels (data->height); 00631 } 00632 catch (const std::exception &exc) { 00633 std::cerr << "OpenEXR-writePixels: ERROR: " << exc.what() << std::endl; 00634 } 00635 } 00636 else { 00637 printf("Error: attempt to save MultiLayer without layers.\n"); 00638 } 00639 } 00640 00641 void IMB_exr_read_channels(void *handle) 00642 { 00643 ExrHandle *data= (ExrHandle *)handle; 00644 FrameBuffer frameBuffer; 00645 ExrChannel *echan; 00646 00647 /* check if exr was saved with previous versions of blender which flipped images */ 00648 const StringAttribute *ta = data->ifile->header().findTypedAttribute <StringAttribute> ("BlenderMultiChannel"); 00649 short flip = (ta && strncmp(ta->value().c_str(), "Blender V2.43", 13)==0); /* 'previous multilayer attribute, flipped */ 00650 00651 for(echan= (ExrChannel *)data->channels.first; echan; echan= echan->next) { 00652 00653 if(echan->rect) { 00654 if(flip) 00655 frameBuffer.insert (echan->name, Slice (FLOAT, (char *)echan->rect, 00656 echan->xstride*sizeof(float), echan->ystride*sizeof(float))); 00657 else 00658 frameBuffer.insert (echan->name, Slice (FLOAT, (char *)(echan->rect + echan->xstride*(data->height-1)*data->width), 00659 echan->xstride*sizeof(float), -echan->ystride*sizeof(float))); 00660 } 00661 else 00662 printf("warning, channel with no rect set %s\n", echan->name); 00663 } 00664 00665 data->ifile->setFrameBuffer (frameBuffer); 00666 00667 try { 00668 data->ifile->readPixels (0, data->height-1); 00669 } 00670 catch (const std::exception &exc) { 00671 std::cerr << "OpenEXR-readPixels: ERROR: " << exc.what() << std::endl; 00672 } 00673 } 00674 00675 void IMB_exr_multilayer_convert(void *handle, void *base, 00676 void * (*addlayer)(void *base, char *str), 00677 void (*addpass)(void *base, void *lay, char *str, 00678 float *rect, int totchan, char *chan_id)) 00679 { 00680 ExrHandle *data= (ExrHandle *)handle; 00681 ExrLayer *lay; 00682 ExrPass *pass; 00683 00684 if(data->layers.first==NULL) { 00685 printf("cannot convert multilayer, no layers in handle\n"); 00686 return; 00687 } 00688 00689 for(lay= (ExrLayer *)data->layers.first; lay; lay= lay->next) { 00690 void *laybase= addlayer(base, lay->name); 00691 if(laybase) { 00692 for(pass= (ExrPass *)lay->passes.first; pass; pass= pass->next) { 00693 addpass(base, laybase, pass->name, pass->rect, pass->totchan, pass->chan_id); 00694 pass->rect= NULL; 00695 } 00696 } 00697 } 00698 } 00699 00700 00701 void IMB_exr_close(void *handle) 00702 { 00703 ExrHandle *data= (ExrHandle *)handle; 00704 ExrLayer *lay; 00705 ExrPass *pass; 00706 00707 if(data->ifile) 00708 delete data->ifile; 00709 else if(data->ofile) 00710 delete data->ofile; 00711 else if(data->tofile) 00712 delete data->tofile; 00713 00714 data->ifile= NULL; 00715 data->ofile= NULL; 00716 data->tofile= NULL; 00717 00718 BLI_freelistN(&data->channels); 00719 00720 for(lay= (ExrLayer *)data->layers.first; lay; lay= lay->next) { 00721 for(pass= (ExrPass *)lay->passes.first; pass; pass= pass->next) 00722 if(pass->rect) 00723 MEM_freeN(pass->rect); 00724 BLI_freelistN(&lay->passes); 00725 } 00726 BLI_freelistN(&data->layers); 00727 00728 BLI_remlink(&exrhandles, data); 00729 MEM_freeN(data); 00730 } 00731 00732 /* ********* */ 00733 00734 static int imb_exr_split_channel_name(ExrChannel *echan, char *layname, char *passname) 00735 { 00736 int plen, len= strlen(echan->name); 00737 00738 if(len < 4) { 00739 printf("multilayer read: name too short: %s\n", echan->name); 00740 return 0; 00741 } 00742 if(echan->name[len-2]!='.') { 00743 printf("multilayer read: name has no Channel: %s\n", echan->name); 00744 return 0; 00745 } 00746 echan->chan_id= echan->name[len-1]; 00747 00748 len-= 3; 00749 while(len>=0) { 00750 if(echan->name[len]=='.') 00751 break; 00752 len--; 00753 } 00754 BLI_strncpy(passname, echan->name+len+1, EXR_PASS_MAXNAME); 00755 plen= strlen(passname); 00756 if(plen < 3) { 00757 printf("multilayer read: should not happen: %s\n", echan->name); 00758 return 0; 00759 } 00760 passname[plen-2]= 0; 00761 00762 if(len<1) 00763 layname[0]= 0; 00764 else { 00765 BLI_strncpy(layname, echan->name, EXR_LAY_MAXNAME); 00766 layname[len]= 0; 00767 } 00768 // printf("found lay %s pass %s chan %c\n", layname, passname, echan->chan_id); 00769 return 1; 00770 } 00771 00772 static ExrLayer *imb_exr_get_layer(ListBase *lb, char *layname) 00773 { 00774 ExrLayer *lay= (ExrLayer *)BLI_findstring(lb, layname, offsetof(ExrLayer, name)); 00775 00776 if(lay==NULL) { 00777 lay= (ExrLayer *)MEM_callocN(sizeof(ExrLayer), "exr layer"); 00778 BLI_addtail(lb, lay); 00779 BLI_strncpy(lay->name, layname, EXR_LAY_MAXNAME); 00780 } 00781 00782 return lay; 00783 } 00784 00785 static ExrPass *imb_exr_get_pass(ListBase *lb, char *passname) 00786 { 00787 ExrPass *pass= (ExrPass *)BLI_findstring(lb, passname, offsetof(ExrPass, name)); 00788 00789 if(pass==NULL) { 00790 pass= (ExrPass *)MEM_callocN(sizeof(ExrPass), "exr pass"); 00791 00792 if(strcmp(passname, "Combined")==0) 00793 BLI_addhead(lb, pass); 00794 else 00795 BLI_addtail(lb, pass); 00796 } 00797 00798 BLI_strncpy(pass->name, passname, EXR_LAY_MAXNAME); 00799 00800 return pass; 00801 } 00802 00803 /* creates channels, makes a hierarchy and assigns memory to channels */ 00804 static ExrHandle *imb_exr_begin_read_mem(InputFile *file, int width, int height) 00805 { 00806 ExrLayer *lay; 00807 ExrPass *pass; 00808 ExrChannel *echan; 00809 ExrHandle *data= (ExrHandle *)IMB_exr_get_handle(); 00810 int a; 00811 char layname[EXR_TOT_MAXNAME], passname[EXR_TOT_MAXNAME]; 00812 00813 data->ifile= file; 00814 data->width= width; 00815 data->height= height; 00816 00817 const ChannelList &channels = data->ifile->header().channels(); 00818 00819 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) 00820 IMB_exr_add_channel(data, NULL, i.name(), 0, 0, NULL); 00821 00822 /* now try to sort out how to assign memory to the channels */ 00823 /* first build hierarchical layer list */ 00824 for(echan= (ExrChannel *)data->channels.first; echan; echan= echan->next) { 00825 if( imb_exr_split_channel_name(echan, layname, passname) ) { 00826 ExrLayer *lay= imb_exr_get_layer(&data->layers, layname); 00827 ExrPass *pass= imb_exr_get_pass(&lay->passes, passname); 00828 00829 pass->chan[pass->totchan]= echan; 00830 pass->totchan++; 00831 if(pass->totchan>=EXR_PASS_MAXCHAN) 00832 break; 00833 } 00834 } 00835 if(echan) { 00836 printf("error, too many channels in one pass: %s\n", echan->name); 00837 IMB_exr_close(data); 00838 return NULL; 00839 } 00840 00841 /* with some heuristics, try to merge the channels in buffers */ 00842 for(lay= (ExrLayer *)data->layers.first; lay; lay= lay->next) { 00843 for(pass= (ExrPass *)lay->passes.first; pass; pass= pass->next) { 00844 if(pass->totchan) { 00845 pass->rect= (float *)MEM_mapallocN(width*height*pass->totchan*sizeof(float), "pass rect"); 00846 if(pass->totchan==1) { 00847 echan= pass->chan[0]; 00848 echan->rect= pass->rect; 00849 echan->xstride= 1; 00850 echan->ystride= width; 00851 pass->chan_id[0]= echan->chan_id; 00852 } 00853 else { 00854 char lookup[256]; 00855 00856 memset(lookup, 0, sizeof(lookup)); 00857 00858 /* we can have RGB(A), XYZ(W), UVA */ 00859 if(pass->totchan==3 || pass->totchan==4) { 00860 if(pass->chan[0]->chan_id=='B' || pass->chan[1]->chan_id=='B' || pass->chan[2]->chan_id=='B') { 00861 lookup[(unsigned int)'R']= 0; 00862 lookup[(unsigned int)'G']= 1; 00863 lookup[(unsigned int)'B']= 2; 00864 lookup[(unsigned int)'A']= 3; 00865 } 00866 else if(pass->chan[0]->chan_id=='Y' || pass->chan[1]->chan_id=='Y' || pass->chan[2]->chan_id=='Y') { 00867 lookup[(unsigned int)'X']= 0; 00868 lookup[(unsigned int)'Y']= 1; 00869 lookup[(unsigned int)'Z']= 2; 00870 lookup[(unsigned int)'W']= 3; 00871 } 00872 else { 00873 lookup[(unsigned int)'U']= 0; 00874 lookup[(unsigned int)'V']= 1; 00875 lookup[(unsigned int)'A']= 2; 00876 } 00877 for(a=0; a<pass->totchan; a++) { 00878 echan= pass->chan[a]; 00879 echan->rect= pass->rect + lookup[(unsigned int)echan->chan_id]; 00880 echan->xstride= pass->totchan; 00881 echan->ystride= width*pass->totchan; 00882 pass->chan_id[ (unsigned int)lookup[(unsigned int)echan->chan_id] ]= echan->chan_id; 00883 } 00884 } 00885 else { /* unknown */ 00886 for(a=0; a<pass->totchan; a++) { 00887 echan= pass->chan[a]; 00888 echan->rect= pass->rect + a; 00889 echan->xstride= pass->totchan; 00890 echan->ystride= width*pass->totchan; 00891 pass->chan_id[a]= echan->chan_id; 00892 } 00893 } 00894 } 00895 } 00896 } 00897 } 00898 00899 return data; 00900 } 00901 00902 00903 /* ********************************************************* */ 00904 00905 typedef struct RGBA 00906 { 00907 float r; 00908 float g; 00909 float b; 00910 float a; 00911 } RGBA; 00912 00913 00914 /* debug only */ 00915 static void exr_print_filecontents(InputFile *file) 00916 { 00917 const ChannelList &channels = file->header().channels(); 00918 00919 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) 00920 { 00921 const Channel &channel = i.channel(); 00922 printf("OpenEXR-load: Found channel %s of type %d\n", i.name(), channel.type); 00923 } 00924 } 00925 00926 /* for non-multilayer, map R G B A channel names to something that's in this file */ 00927 static const char *exr_rgba_channelname(InputFile *file, const char *chan) 00928 { 00929 const ChannelList &channels = file->header().channels(); 00930 00931 for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) 00932 { 00933 /* const Channel &channel = i.channel(); */ /* Not used yet */ 00934 const char *str= i.name(); 00935 int len= strlen(str); 00936 if(len) { 00937 if(BLI_strcasecmp(chan, str+len-1)==0) { 00938 return str; 00939 } 00940 } 00941 } 00942 return chan; 00943 } 00944 00945 00946 00947 static int exr_has_zbuffer(InputFile *file) 00948 { 00949 return !(file->header().channels().findChannel("Z") == NULL); 00950 } 00951 00952 static int exr_is_renderresult(InputFile *file) 00953 { 00954 const StringAttribute *comments= file->header().findTypedAttribute<StringAttribute>("BlenderMultiChannel"); 00955 if(comments) 00956 // if(comments->value() == "Blender MultiChannel") 00957 return 1; 00958 return 0; 00959 } 00960 00961 struct ImBuf *imb_load_openexr(unsigned char *mem, size_t size, int flags) 00962 { 00963 struct ImBuf *ibuf = NULL; 00964 InputFile *file = NULL; 00965 00966 if (imb_is_a_openexr(mem) == 0) return(NULL); 00967 00968 try 00969 { 00970 Mem_IStream *membuf = new Mem_IStream(mem, size); 00971 int is_multi; 00972 file = new InputFile(*membuf); 00973 00974 Box2i dw = file->header().dataWindow(); 00975 int width = dw.max.x - dw.min.x + 1; 00976 int height = dw.max.y - dw.min.y + 1; 00977 00978 //printf("OpenEXR-load: image data window %d %d %d %d\n", 00979 // dw.min.x, dw.min.y, dw.max.x, dw.max.y); 00980 00981 if(0) // debug 00982 exr_print_filecontents(file); 00983 00984 is_multi= exr_is_renderresult(file); 00985 00986 /* do not make an ibuf when */ 00987 if(is_multi && !(flags & IB_test) && !(flags & IB_multilayer)) 00988 { 00989 printf("Error: can't process EXR multilayer file\n"); 00990 } 00991 else { 00992 00993 ibuf = IMB_allocImBuf(width, height, 32, 0); 00994 ibuf->ftype = OPENEXR; 00995 00996 /* openEXR is linear as per EXR spec */ 00997 ibuf->profile = IB_PROFILE_LINEAR_RGB; 00998 00999 if (!(flags & IB_test)) 01000 { 01001 if(is_multi) /* only enters with IB_multilayer flag set */ 01002 { 01003 /* constructs channels for reading, allocates memory in channels */ 01004 ExrHandle *handle= imb_exr_begin_read_mem(file, width, height); 01005 if(handle) { 01006 IMB_exr_read_channels(handle); 01007 ibuf->userdata= handle; /* potential danger, the caller has to check for this! */ 01008 return ibuf; 01009 } 01010 } 01011 else { 01012 FrameBuffer frameBuffer; 01013 float *first; 01014 int xstride = sizeof(float) * 4; 01015 int ystride = - xstride*width; 01016 01017 imb_addrectfloatImBuf(ibuf); 01018 01019 /* inverse correct first pixel for datawindow coordinates (- dw.min.y because of y flip) */ 01020 first= ibuf->rect_float - 4*(dw.min.x - dw.min.y*width); 01021 /* but, since we read y-flipped (negative y stride) we move to last scanline */ 01022 first+= 4*(height-1)*width; 01023 01024 frameBuffer.insert ( exr_rgba_channelname(file, "R"), 01025 Slice (FLOAT, (char *) first, xstride, ystride)); 01026 frameBuffer.insert ( exr_rgba_channelname(file, "G"), 01027 Slice (FLOAT, (char *) (first+1), xstride, ystride)); 01028 frameBuffer.insert ( exr_rgba_channelname(file, "B"), 01029 Slice (FLOAT, (char *) (first+2), xstride, ystride)); 01030 01031 frameBuffer.insert ( exr_rgba_channelname(file, "A"), 01032 Slice (FLOAT, (char *) (first+3), xstride, ystride, 1, 1, 1.0f)); /* 1.0 is fill value */ 01033 01034 if(exr_has_zbuffer(file)) 01035 { 01036 float *firstz; 01037 01038 addzbuffloatImBuf(ibuf); 01039 firstz= ibuf->zbuf_float - (dw.min.x - dw.min.y*width); 01040 firstz+= (height-1)*width; 01041 frameBuffer.insert ("Z", Slice (FLOAT, (char *)firstz , sizeof(float), -width*sizeof(float))); 01042 } 01043 01044 file->setFrameBuffer (frameBuffer); 01045 file->readPixels (dw.min.y, dw.max.y); 01046 01047 // XXX, ImBuf has no nice way to deal with this. 01048 // ideally IM_rect would be used when the caller wants a rect BUT 01049 // at the moment all functions use IM_rect. 01050 // Disabling this is ok because all functions should check if a rect exists and create one on demand. 01051 // 01052 // Disabling this because the sequencer frees immediate. 01053 // 01054 // if(flag & IM_rect) 01055 // IMB_rect_from_float(ibuf); 01056 } 01057 } 01058 01059 } 01060 delete file; 01061 return(ibuf); 01062 } 01063 catch (const std::exception &exc) 01064 { 01065 std::cerr << exc.what() << std::endl; 01066 if (ibuf) IMB_freeImBuf(ibuf); 01067 delete file; 01068 01069 return (0); 01070 } 01071 01072 } 01073 01074 01075 } // export "C"
1.7.3