|
Blender V2.27
|
00001 /* 00002 * Dpx image file format library routines. 00003 * 00004 * Copyright 1999 - 2002 David Hodson <hodsond@acm.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU General Public License as published by the Free 00008 * Software Foundation; either version 2 of the License, or (at your option) 00009 * any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00013 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00014 * for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 00022 #include "dpxfile.h" 00023 #include "dpxlib.h" 00024 00025 #include <stdio.h> 00026 #include <math.h> 00027 #include <stdlib.h> 00028 #include <time.h> /* strftime() */ 00029 #include <sys/types.h> 00030 #ifdef WIN32 00031 #include <winsock.h> 00032 #else 00033 #include <netinet/in.h> /* htonl() */ 00034 #endif 00035 #include <string.h> /* memset */ 00036 #include "cin_debug_stuff.h" 00037 #include "logmemfile.h" 00038 00039 static void 00040 fillDpxChannelInfo(DpxFile* dpx, DpxChannelInformation* chan, int des) { 00041 00042 (void)dpx; /* unused */ 00043 00044 chan->signage = 0; 00045 chan->ref_low_data = htonl(0); 00046 chan->ref_low_quantity = htonf(0.0); 00047 chan->ref_high_data = htonl(1023); 00048 chan->ref_high_quantity = htonf(2.046); 00049 chan->designator1 = des; 00050 chan->transfer_characteristics = 0; 00051 chan->colourimetry = 0; 00052 chan->bits_per_pixel = 10; 00053 chan->packing = htons(1); 00054 chan->encoding = 0; 00055 chan->data_offset = 0; 00056 chan->line_padding = htonl(0); 00057 chan->channel_padding = htonl(0); 00058 chan->description[0] = 0; 00059 } 00060 00061 static void 00062 dumpDpxChannelInfo(DpxChannelInformation* chan) { 00063 d_printf(" Signage %ld", (intptr_t)ntohl(chan->signage)); 00064 d_printf(" Ref low data %ld\n", (intptr_t)ntohl(chan->ref_low_data)); 00065 d_printf(" Ref low quantity %f\n", ntohf(chan->ref_low_quantity)); 00066 d_printf(" Ref high data %ld\n", (intptr_t)ntohl(chan->ref_high_data)); 00067 d_printf(" Ref high quantity %f\n", ntohf(chan->ref_high_quantity)); 00068 d_printf(" Designator1: %d,", chan->designator1); 00069 d_printf(" Bits per pixel %d\n", chan->bits_per_pixel); 00070 d_printf(" Packing: %d,", ntohs(chan->packing)); 00071 d_printf(" Data Offset: %ld,", (intptr_t)ntohl(chan->data_offset)); 00072 } 00073 00074 static void 00075 fillDpxFileInfo( 00076 DpxFile* dpx, DpxFileInformation* fileInfo, const char* filename) { 00077 00078 time_t fileClock; 00079 struct tm* fileTime; 00080 00081 /* Note: always write files in network order */ 00082 /* By the spec, it shouldn't matter, but ... */ 00083 00084 fileInfo->magic_num = htonl(DPX_FILE_MAGIC); 00085 fileInfo->offset = htonl(dpx->imageOffset); 00086 strcpy(fileInfo->vers, "v1.0"); 00087 fileInfo->file_size = htonl(dpx->imageOffset + 00088 pixelsToLongs(dpx->height * dpx->width * dpx->depth) * 4); 00089 fileInfo->ditto_key = 0; 00090 fileInfo->gen_hdr_size = htonl( 00091 sizeof(DpxFileInformation) + 00092 sizeof(DpxImageInformation) + 00093 sizeof(DpxOriginationInformation)); 00094 fileInfo->ind_hdr_size = htonl(sizeof(DpxMPIInformation)); 00095 fileInfo->user_data_size = 0; 00096 strncpy(fileInfo->file_name, filename, 99); 00097 fileInfo->file_name[99] = 0; 00098 00099 fileClock = time(0); 00100 fileTime = localtime(&fileClock); 00101 strftime(fileInfo->create_date, 24, "%Y:%m:%d:%H:%M:%S%Z", fileTime); 00102 /* Question: is %Z in strftime guaranteed to return 3 chars? */ 00103 fileInfo->create_date[23] = 0; 00104 00105 strcpy(fileInfo->creator, "David's DPX writer"); 00106 fileInfo->project[0] = 0; 00107 fileInfo->copyright[0] = 0; 00108 fileInfo->key = 0xFFFFFFFF; /* same in any byte order */ 00109 } 00110 00111 static void 00112 dumpDpxFileInfo(DpxFileInformation* fileInfo) { 00113 d_printf("\n--File Information--\n"); 00114 d_printf("Magic: %8.8lX\n", (unsigned long)ntohl(fileInfo->magic_num)); 00115 d_printf("Image Offset %ld\n", (intptr_t)ntohl(fileInfo->offset)); 00116 d_printf("Version \"%s\"\n", fileInfo->vers); 00117 d_printf("File size %ld\n", (intptr_t)ntohl(fileInfo->file_size)); 00118 d_printf("Ditto key %ld\n", (intptr_t)ntohl(fileInfo->ditto_key)); 00119 d_printf("Generic Header size %ld\n", (intptr_t)ntohl(fileInfo->gen_hdr_size)); 00120 d_printf("Industry Header size %ld\n", (intptr_t)ntohl(fileInfo->ind_hdr_size)); 00121 d_printf("User Data size %ld\n", (intptr_t)ntohl(fileInfo->user_data_size)); 00122 d_printf("File name \"%s\"\n", fileInfo->file_name); 00123 d_printf("Creation date \"%s\"\n", fileInfo->create_date); 00124 d_printf("Creator \"%s\"\n", fileInfo->creator); 00125 d_printf("Project \"%s\"\n", fileInfo->project); 00126 d_printf("Copyright \"%s\"\n", fileInfo->copyright); 00127 d_printf("Key %ld\n", (intptr_t)ntohl(fileInfo->key)); 00128 } 00129 00130 static void 00131 fillDpxImageInfo( 00132 DpxFile* dpx, DpxImageInformation* imageInfo) { 00133 imageInfo->orientation = 0; 00134 imageInfo->channels_per_image = htons(1); 00135 imageInfo->pixels_per_line = htonl(dpx->width); 00136 imageInfo->lines_per_image = htonl(dpx->height); 00137 00138 if (dpx->depth == 1) { 00139 fillDpxChannelInfo(dpx, &imageInfo->channel[0], 0); 00140 00141 } else if (dpx->depth == 3) { 00142 fillDpxChannelInfo(dpx, &imageInfo->channel[0], 50); 00143 } 00144 } 00145 00146 static void 00147 dumpDpxImageInfo(DpxImageInformation* imageInfo) { 00148 00149 int n; 00150 int i; 00151 d_printf("\n--Image Information--\n"); 00152 d_printf("Image orientation %d,", ntohs(imageInfo->orientation)); 00153 n = ntohs(imageInfo->channels_per_image); 00154 d_printf("Channels %d\n", n); 00155 d_printf("Pixels per line %ld\n", (intptr_t)ntohl(imageInfo->pixels_per_line)); 00156 d_printf("Lines per image %ld\n", (intptr_t)ntohl(imageInfo->lines_per_image)); 00157 for (i = 0; i < n; ++i) { 00158 d_printf(" --Channel %d--\n", i); 00159 dumpDpxChannelInfo(&imageInfo->channel[i]); 00160 } 00161 } 00162 00163 static void 00164 fillDpxOriginationInfo( 00165 DpxFile* dpx, DpxOriginationInformation* originInfo, DpxFileInformation* fileInfo) 00166 { 00167 /* unused */ 00168 (void)dpx; 00169 (void)originInfo; 00170 (void)fileInfo; 00171 } 00172 00173 static void 00174 dumpDpxOriginationInfo(DpxOriginationInformation* originInfo) { 00175 d_printf("\n--Origination Information--\n"); 00176 d_printf("X offset %ld\n", (intptr_t)ntohl(originInfo->x_offset)); 00177 d_printf("Y offset %ld\n", (intptr_t)ntohl(originInfo->y_offset)); 00178 d_printf("X centre %f\n", ntohf(originInfo->x_centre)); 00179 d_printf("Y centre %f\n", ntohf(originInfo->y_centre)); 00180 d_printf("Original X %ld\n", (intptr_t)ntohl(originInfo->x_original_size)); 00181 d_printf("Original Y %ld\n", (intptr_t)ntohl(originInfo->y_original_size)); 00182 d_printf("File name \"%s\"\n", originInfo->file_name); 00183 d_printf("Creation time \"%s\"\n", originInfo->creation_time); 00184 d_printf("Input device \"%s\"\n", originInfo->input_device); 00185 d_printf("Serial number \"%s\"\n", originInfo->input_serial_number); 00186 } 00187 00188 static void 00189 initDpxMainHeader(DpxFile* dpx, DpxMainHeader* header, const char* shortFilename) { 00190 memset(header, 0, sizeof(DpxMainHeader)); 00191 fillDpxFileInfo(dpx, &header->fileInfo, shortFilename); 00192 fillDpxImageInfo(dpx, &header->imageInfo); 00193 fillDpxOriginationInfo(dpx, &header->originInfo, &header->fileInfo); 00194 #if 0 00195 fillDpxMPIInfo(dpx, &header->filmHeader); 00196 #endif 00197 } 00198 00199 static void 00200 dumpDpxMainHeader(DpxMainHeader* header) { 00201 dumpDpxFileInfo(&header->fileInfo); 00202 dumpDpxImageInfo(&header->imageInfo); 00203 dumpDpxOriginationInfo(&header->originInfo); 00204 #if 0 00205 dumpDpxMPIInformation(&header->filmHeader); 00206 #endif 00207 } 00208 00209 static int verbose = 0; 00210 void 00211 dpxSetVerbose(int verbosity) { 00212 verbose = verbosity; 00213 } 00214 00215 static void 00216 verboseMe(DpxFile* dpx) { 00217 00218 d_printf("size %d x %d x %d\n", dpx->width, dpx->height, dpx->depth); 00219 d_printf("ImageStart %d, lineBufferLength %d, implied length %d\n", 00220 dpx->imageOffset, dpx->lineBufferLength * 4, 00221 dpx->imageOffset + pixelsToLongs(dpx->width * dpx->depth * dpx->height) * 4); 00222 } 00223 00224 int 00225 dpxGetRowBytes(DpxFile* dpx, unsigned short* row, int y) { 00226 00227 /* Note: this code is bizarre because DPX files can wrap */ 00228 /* packed longwords across line boundaries!!!! */ 00229 00230 size_t readLongs; 00231 unsigned int longIndex; 00232 int numPixels = dpx->width * dpx->depth; 00233 int pixelIndex; 00234 00235 /* only seek if not reading consecutive lines */ 00236 /* this is not quite right yet, need to account for leftovers */ 00237 if (y != dpx->fileYPos) { 00238 int lineOffset = pixelsToLongs(y * dpx->width * dpx->depth) * 4; 00239 if (verbose) d_printf("Seek in getRowBytes\n"); 00240 if (logimage_fseek(dpx, dpx->imageOffset + lineOffset, SEEK_SET) != 0) { 00241 if (verbose) d_printf("Couldn't seek to line %d at %d\n", y, dpx->imageOffset + lineOffset); 00242 return 1; 00243 } 00244 dpx->fileYPos = y; 00245 } 00246 00247 /* read enough longwords */ 00248 readLongs = pixelsToLongs(numPixels - dpx->pixelBufferUsed); 00249 if (logimage_fread(dpx->lineBuffer, 4, readLongs, dpx) != readLongs) { 00250 if (verbose) d_printf("Couldn't read line %d length %d\n", y, (int)readLongs * 4); 00251 return 1; 00252 } 00253 ++dpx->fileYPos; 00254 00255 /* convert longwords to pixels */ 00256 pixelIndex = dpx->pixelBufferUsed; 00257 00258 /* this is just strange */ 00259 if (dpx->depth == 1) { 00260 for (longIndex = 0; longIndex < readLongs; ++longIndex) { 00261 unsigned int t = ntohl(dpx->lineBuffer[longIndex]); 00262 dpx->pixelBuffer[pixelIndex] = t & 0x3ff; 00263 t = t >> 10; 00264 dpx->pixelBuffer[pixelIndex+1] = t & 0x3ff; 00265 t = t >> 10; 00266 dpx->pixelBuffer[pixelIndex+2] = t & 0x3ff; 00267 pixelIndex += 3; 00268 } 00269 } else /* if (dpx->depth == 3) */ { 00270 for (longIndex = 0; longIndex < readLongs; ++longIndex) { 00271 unsigned int t = ntohl(dpx->lineBuffer[longIndex]); 00272 t = t >> 2; 00273 dpx->pixelBuffer[pixelIndex+2] = t & 0x3ff; 00274 t = t >> 10; 00275 dpx->pixelBuffer[pixelIndex+1] = t & 0x3ff; 00276 t = t >> 10; 00277 dpx->pixelBuffer[pixelIndex] = t & 0x3ff; 00278 pixelIndex += 3; 00279 } 00280 } 00281 dpx->pixelBufferUsed = pixelIndex; 00282 00283 /* extract required pixels */ 00284 for (pixelIndex = 0; pixelIndex < numPixels; ++pixelIndex) { 00285 if(dpx->params.doLogarithm) 00286 row[pixelIndex] = dpx->lut10_16[dpx->pixelBuffer[pixelIndex]]; 00287 else 00288 row[pixelIndex] = dpx->pixelBuffer[pixelIndex] << 6; 00289 } 00290 00291 /* save remaining pixels */ 00292 while (pixelIndex < dpx->pixelBufferUsed) { 00293 dpx->pixelBuffer[pixelIndex - numPixels] = dpx->pixelBuffer[pixelIndex]; 00294 ++pixelIndex; 00295 } 00296 dpx->pixelBufferUsed -= numPixels; 00297 00298 /* done! */ 00299 return 0; 00300 } 00301 00302 int 00303 dpxSetRowBytes(DpxFile* dpx, const unsigned short* row, int y) { 00304 00305 /* Note: this code is bizarre because DPX files can wrap */ 00306 /* packed longwords across line boundaries!!!! */ 00307 00308 size_t writeLongs; 00309 int longIndex; 00310 int numPixels = dpx->width * dpx->depth; 00311 int pixelIndex; 00312 int pixelIndex2; 00313 00314 /* only seek if not reading consecutive lines */ 00315 /* this is not quite right yet */ 00316 if (y != dpx->fileYPos) { 00317 int lineOffset = pixelsToLongs(y * dpx->width * dpx->depth) * 4; 00318 if (verbose) d_printf("Seek in getRowBytes\n"); 00319 if (logimage_fseek(dpx, dpx->imageOffset + lineOffset, SEEK_SET) != 0) { 00320 if (verbose) d_printf("Couldn't seek to line %d at %d\n", y, dpx->imageOffset + lineOffset); 00321 return 1; 00322 } 00323 dpx->fileYPos = y; 00324 } 00325 00326 /* put new pixels into pixelBuffer */ 00327 for (pixelIndex = 0; pixelIndex < numPixels; ++pixelIndex) { 00328 if(dpx->params.doLogarithm) 00329 dpx->pixelBuffer[dpx->pixelBufferUsed + pixelIndex] = dpx->lut16_16[row[pixelIndex]]; 00330 else 00331 dpx->pixelBuffer[dpx->pixelBufferUsed + pixelIndex] = row[pixelIndex] >> 6; 00332 } 00333 dpx->pixelBufferUsed += numPixels; 00334 00335 /* pack into longwords */ 00336 writeLongs = dpx->pixelBufferUsed / 3; 00337 /* process whole line at image end */ 00338 if (dpx->fileYPos == (dpx->height - 1)) { 00339 writeLongs = pixelsToLongs(dpx->pixelBufferUsed); 00340 } 00341 pixelIndex = 0; 00342 if (dpx->depth == 1) { 00343 for (longIndex = 0; longIndex < writeLongs; ++longIndex) { 00344 unsigned int t = dpx->pixelBuffer[pixelIndex] | 00345 (dpx->pixelBuffer[pixelIndex+1] << 10) | 00346 (dpx->pixelBuffer[pixelIndex+2] << 20); 00347 dpx->lineBuffer[longIndex] = htonl(t); 00348 pixelIndex += 3; 00349 } 00350 } else { 00351 for (longIndex = 0; longIndex < writeLongs; ++longIndex) { 00352 unsigned int t = dpx->pixelBuffer[pixelIndex+2] << 2 | 00353 (dpx->pixelBuffer[pixelIndex+1] << 12) | 00354 (dpx->pixelBuffer[pixelIndex] << 22); 00355 dpx->lineBuffer[longIndex] = htonl(t); 00356 pixelIndex += 3; 00357 } 00358 } 00359 00360 /* write them */ 00361 if (fwrite(dpx->lineBuffer, 4, writeLongs, dpx->file) != writeLongs) { 00362 if (verbose) d_printf("Couldn't write line %d length %d\n", y, (int)writeLongs * 4); 00363 return 1; 00364 } 00365 ++dpx->fileYPos; 00366 00367 /* save remaining pixels */ 00368 pixelIndex2 = 0; 00369 while (pixelIndex < dpx->pixelBufferUsed) { 00370 dpx->pixelBuffer[pixelIndex2] = dpx->pixelBuffer[pixelIndex]; 00371 ++pixelIndex; 00372 ++pixelIndex2; 00373 } 00374 dpx->pixelBufferUsed = pixelIndex2; 00375 00376 return 0; 00377 } 00378 00379 #define LFMEMFILE 0 00380 #define LFREALFILE 1 00381 00382 static DpxFile* 00383 intern_dpxOpen(int mode, const char* bytestuff, int bufsize) { 00384 00385 DpxMainHeader header; 00386 const char *filename = bytestuff; 00387 DpxFile* dpx = (DpxFile*)malloc(sizeof(DpxFile)); 00388 00389 if (dpx == 0) { 00390 if (verbose) d_printf("Failed to malloc dpx file structure.\n"); 00391 return 0; 00392 } 00393 00394 /* for close routine */ 00395 dpx->file = 0; 00396 dpx->lineBuffer = 0; 00397 dpx->pixelBuffer = 0; 00398 00399 if (mode == LFREALFILE) { 00400 filename = bytestuff; 00401 dpx->file = fopen(filename, "rb"); 00402 if (dpx->file == 0) { 00403 if (verbose) d_printf("Failed to open file \"%s\".\n", filename); 00404 dpxClose(dpx); 00405 return 0; 00406 } 00407 dpx->membuffer = 0; 00408 dpx->memcursor = 0; 00409 dpx->membuffersize = 0; 00410 } else if (mode == LFMEMFILE) { 00411 dpx->membuffer = (unsigned char *)bytestuff; 00412 dpx->memcursor = (unsigned char *)bytestuff; 00413 dpx->membuffersize = bufsize; 00414 } 00415 00416 dpx->reading = 1; 00417 00418 if (logimage_fread(&header, sizeof(header), 1, dpx) == 0) { 00419 if (verbose) d_printf("Not enough data for header in \"%s\".\n", filename); 00420 dpxClose(dpx); 00421 return 0; 00422 } 00423 00424 /* let's assume dpx files are always network order */ 00425 if (header.fileInfo.magic_num != ntohl(DPX_FILE_MAGIC)) { 00426 if (verbose) d_printf("Bad magic number %8.8lX in \"%s\".\n", 00427 (uintptr_t)ntohl(header.fileInfo.magic_num), filename); 00428 dpxClose(dpx); 00429 return 0; 00430 } 00431 00432 if (ntohs(header.imageInfo.channel[0].packing) != 1) { 00433 if (verbose) d_printf("Unknown packing %d\n", header.imageInfo.channel[0].packing); 00434 dpxClose(dpx); 00435 return 0; 00436 } 00437 00438 00439 dpx->width = ntohl(header.imageInfo.pixels_per_line); 00440 dpx->height = ntohl(header.imageInfo.lines_per_image); 00441 dpx->depth = ntohs(header.imageInfo.channels_per_image); 00442 /* Another DPX vs Cineon wierdness */ 00443 if (dpx->depth == 1) { 00444 switch (header.imageInfo.channel[0].designator1) { 00445 case 50: dpx->depth = 3; break; 00446 case 51: dpx->depth = 4; break; 00447 case 52: dpx->depth = 4; break; 00448 default: break; 00449 } 00450 } 00451 /* dpx->bitsPerPixel = 10; */ 00452 dpx->bitsPerPixel = header.imageInfo.channel[0].bits_per_pixel; 00453 if (dpx->bitsPerPixel != 10) { 00454 if (verbose) d_printf("Don't support depth: %d\n", dpx->bitsPerPixel); 00455 dpxClose(dpx); 00456 return 0; 00457 } 00458 00459 dpx->imageOffset = ntohl(header.fileInfo.offset); 00460 dpx->lineBufferLength = pixelsToLongs(dpx->width * dpx->depth); 00461 dpx->lineBuffer = malloc(dpx->lineBufferLength * 4); 00462 if (dpx->lineBuffer == 0) { 00463 if (verbose) d_printf("Couldn't malloc line buffer of size %d\n", dpx->lineBufferLength * 4); 00464 dpxClose(dpx); 00465 return 0; 00466 } 00467 00468 /* could have 2 pixels left over */ 00469 dpx->pixelBuffer = malloc((dpx->lineBufferLength * 3 + 2) * sizeof(unsigned short)); 00470 if (dpx->pixelBuffer == 0) { 00471 if (verbose) d_printf("Couldn't malloc pixel buffer of size %d\n", 00472 (dpx->width * dpx->depth + 2 + 2) * (int)sizeof(unsigned short)); 00473 dpxClose(dpx); 00474 return 0; 00475 } 00476 dpx->pixelBufferUsed = 0; 00477 00478 if (logimage_fseek(dpx, dpx->imageOffset, SEEK_SET) != 0) { 00479 if (verbose) d_printf("Couldn't seek to image data start at %d\n", dpx->imageOffset); 00480 dpxClose(dpx); 00481 return 0; 00482 } 00483 dpx->fileYPos = 0; 00484 00485 logImageGetByteConversionDefaults(&dpx->params); 00486 /* The SMPTE define this code: 00487 * 0 - User-defined 00488 * 1 - Printing density 00489 * 2 - Linear 00490 * 3 - Logarithmic 00491 * 4 - Unspecified video 00492 * 5 - SMPTE 240M 00493 * 6 - CCIR 709-1 00494 * 7 - CCIR 601-2 system B or G 00495 * 8 - CCIR 601-2 system M 00496 * 9 - NTSC composite video 00497 * 10 - PAL composite video 00498 * 11 - Z linear 00499 * 12 - homogeneous 00500 * 00501 * Note that transfer_characteristics is U8, don't need 00502 * check the byte order. 00503 */ 00504 00505 switch (header.imageInfo.channel[0].transfer_characteristics) { 00506 case 1: 00507 case 2: /* linear */ 00508 dpx->params.doLogarithm= 0; 00509 break; 00510 00511 case 3: 00512 dpx->params.doLogarithm= 1; 00513 break; 00514 00515 /* TODO - Unsupported, but for now just load them, 00516 * colors may look wrong, but can solve color conversion later 00517 */ 00518 case 4: 00519 case 5: 00520 case 6: 00521 case 7: 00522 case 8: 00523 case 9: 00524 case 10: 00525 case 11: 00526 case 12: 00527 if (verbose) d_printf("Un-supported Transfer Characteristics: %d using linear color conversion\n", header.imageInfo.channel[0].transfer_characteristics); 00528 dpx->params.doLogarithm= 0; 00529 break; 00530 default: 00531 if (verbose) d_printf("Un-supported Transfer Characteristics: %d\n", header.imageInfo.channel[0].transfer_characteristics); 00532 dpxClose(dpx); 00533 return 0; 00534 break; 00535 } 00536 setupLut(dpx); 00537 00538 dpx->getRow = &dpxGetRowBytes; 00539 dpx->setRow = 0; 00540 dpx->close = &dpxClose; 00541 00542 if (verbose) { 00543 verboseMe(dpx); 00544 } 00545 00546 return dpx; 00547 } 00548 00549 DpxFile* 00550 dpxOpen(const char *filename) { 00551 return intern_dpxOpen(LFREALFILE, filename, 0); 00552 } 00553 00554 DpxFile* 00555 dpxOpenFromMem(unsigned char *buffer, unsigned int size) { 00556 return intern_dpxOpen(LFMEMFILE, (const char *) buffer, size); 00557 } 00558 00559 int 00560 dpxIsMemFileCineon(void *buffer) { 00561 int magicnum = 0; 00562 magicnum = *((int*)buffer); 00563 if (magicnum == ntohl(DPX_FILE_MAGIC)) return 1; 00564 else return 0; 00565 } 00566 00567 DpxFile* 00568 dpxCreate(const char* filename, int width, int height, int depth) { 00569 00570 /* Note: always write files in network order */ 00571 /* By the spec, it shouldn't matter, but ... */ 00572 00573 DpxMainHeader header; 00574 const char* shortFilename = 0; 00575 00576 DpxFile* dpx = (DpxFile*)malloc(sizeof(DpxFile)); 00577 if (dpx == 0) { 00578 if (verbose) d_printf("Failed to malloc dpx file structure.\n"); 00579 return 0; 00580 } 00581 00582 memset(&header, 0, sizeof(header)); 00583 00584 /* for close routine */ 00585 dpx->file = 0; 00586 dpx->lineBuffer = 0; 00587 dpx->pixelBuffer = 0; 00588 00589 dpx->file = fopen(filename, "wb"); 00590 if (dpx->file == 0) { 00591 if (verbose) d_printf("Couldn't open file %s\n", filename); 00592 dpxClose(dpx); 00593 return 0; 00594 } 00595 dpx->reading = 0; 00596 00597 dpx->width = width; 00598 dpx->height = height; 00599 dpx->depth = depth; 00600 dpx->bitsPerPixel = 10; 00601 dpx->imageOffset = sizeof(DpxMainHeader); 00602 00603 dpx->lineBufferLength = pixelsToLongs(dpx->width * dpx->depth); 00604 dpx->lineBuffer = malloc(dpx->lineBufferLength * 4); 00605 if (dpx->lineBuffer == 0) { 00606 if (verbose) d_printf("Couldn't malloc line buffer of size %d\n", dpx->lineBufferLength * 4); 00607 dpxClose(dpx); 00608 return 0; 00609 } 00610 00611 dpx->pixelBuffer = malloc((dpx->lineBufferLength * 3 + 2) * sizeof(unsigned short)); 00612 if (dpx->pixelBuffer == 0) { 00613 if (verbose) d_printf("Couldn't malloc pixel buffer of size %d\n", 00614 (dpx->width * dpx->depth + 2 + 2) * (int)sizeof(unsigned short)); 00615 dpxClose(dpx); 00616 return 0; 00617 } 00618 dpx->pixelBufferUsed = 0; 00619 00620 /* find trailing part of filename */ 00621 shortFilename = strrchr(filename, '/'); 00622 if (shortFilename == 0) { 00623 shortFilename = filename; 00624 } else { 00625 ++shortFilename; 00626 } 00627 initDpxMainHeader(dpx, &header, shortFilename); 00628 logImageGetByteConversionDefaults(&dpx->params); 00629 /* Need set the file type before write the header! 00630 * 2 - Linear 00631 * 3 - Logarithmic 00632 * 00633 * Note that transfer characteristics is U8, don't need 00634 * check the byte order. 00635 */ 00636 if (dpx->params.doLogarithm == 0) 00637 header.imageInfo.channel[0].transfer_characteristics= 2; 00638 else 00639 header.imageInfo.channel[0].transfer_characteristics= 3; 00640 00641 if (fwrite(&header, sizeof(header), 1, dpx->file) == 0) { 00642 if (verbose) d_printf("Couldn't write image header\n"); 00643 dpxClose(dpx); 00644 return 0; 00645 } 00646 dpx->fileYPos = 0; 00647 setupLut(dpx); 00648 00649 dpx->getRow = 0; 00650 dpx->setRow = &dpxSetRowBytes; 00651 dpx->close = &dpxClose; 00652 00653 return dpx; 00654 } 00655 00656 void 00657 dpxClose(DpxFile* dpx) { 00658 00659 if (dpx == 0) { 00660 return; 00661 } 00662 00663 if (dpx->file) { 00664 fclose(dpx->file); 00665 dpx->file = 0; 00666 } 00667 00668 if (dpx->lineBuffer) { 00669 free(dpx->lineBuffer); 00670 dpx->lineBuffer = 0; 00671 } 00672 00673 if (dpx->pixelBuffer) { 00674 free(dpx->pixelBuffer); 00675 dpx->pixelBuffer = 0; 00676 } 00677 00678 free(dpx); 00679 } 00680 00681 void 00682 dpxDump(const char* filename) { 00683 00684 DpxMainHeader header; 00685 FILE* file; 00686 00687 file = fopen(filename, "rb"); 00688 if (file == 0) { 00689 d_printf("Failed to open file \"%s\".\n", filename); 00690 return; 00691 } 00692 00693 if (fread(&header, sizeof(header), 1, file) == 0) { 00694 d_printf("Not enough data for header in \"%s\".\n", filename); 00695 fclose(file); 00696 return; 00697 } 00698 00699 fclose(file); 00700 dumpDpxMainHeader(&header); 00701 }
1.7.3