|
Blender V2.27
|
00001 /* 00002 * Cineon image file format library routines. 00003 * 00004 * Copyright 1999,2000,2001 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 "cineonlib.h" 00023 #include "cineonfile.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 fillCineonFileInfo(CineonFile* cineon, CineonFileInformation* fileInfo, const char* filename) { 00041 00042 time_t fileClock; 00043 struct tm* fileTime; 00044 00045 fileInfo->magic_num = htonl(CINEON_FILE_MAGIC); 00046 fileInfo->image_offset = htonl(cineon->imageOffset); 00047 fileInfo->gen_hdr_size = htonl( 00048 sizeof(CineonFileInformation) + 00049 sizeof(CineonImageInformation) + 00050 sizeof(CineonFormatInformation) + 00051 sizeof(CineonOriginationInformation)); 00052 fileInfo->ind_hdr_size = 0; 00053 fileInfo->user_data_size = 0; 00054 fileInfo->file_size = htonl(cineon->imageOffset + cineon->height * cineon->lineBufferLength); 00055 strcpy(fileInfo->vers, "V4.5"); 00056 strncpy(fileInfo->file_name, filename, 99); 00057 fileInfo->file_name[99] = 0; 00058 00059 fileClock = time(0); 00060 fileTime = localtime(&fileClock); 00061 strftime(fileInfo->create_date, 12, "%Y:%m:%d", fileTime); 00062 /* Question: is %Z in strftime guaranteed to return 3 chars? */ 00063 strftime(fileInfo->create_time, 12, "%H:%M:%S%Z", fileTime); 00064 fileInfo->create_time[11] = 0; 00065 } 00066 00067 static void 00068 dumpCineonFileInfo(CineonFileInformation* fileInfo) { 00069 d_printf("\n--File Information--\n"); 00070 d_printf("Magic: %8.8lX\n", (uintptr_t)ntohl(fileInfo->magic_num)); 00071 d_printf("Image Offset %ld\n", (intptr_t)ntohl(fileInfo->image_offset)); 00072 d_printf("Generic Header size %ld\n", (intptr_t)ntohl(fileInfo->gen_hdr_size)); 00073 d_printf("Industry Header size %ld\n", (intptr_t)ntohl(fileInfo->ind_hdr_size)); 00074 d_printf("User Data size %ld\n", (intptr_t)ntohl(fileInfo->user_data_size)); 00075 d_printf("File size %ld\n", (intptr_t)ntohl(fileInfo->file_size)); 00076 d_printf("Version \"%s\"\n", fileInfo->vers); 00077 d_printf("File name \"%s\"\n", fileInfo->file_name); 00078 d_printf("Creation date \"%s\"\n", fileInfo->create_date); 00079 d_printf("Creation time \"%s\"\n", fileInfo->create_time); 00080 } 00081 00082 static void 00083 fillCineonChannelInfo(CineonFile* cineon, CineonChannelInformation* chan, int des) { 00084 00085 chan->designator1 = 0; 00086 chan->designator2 = des; 00087 chan->bits_per_pixel = 10; 00088 chan->pixels_per_line = htonl(cineon->width); 00089 chan->lines_per_image = htonl(cineon->height); 00090 chan->ref_low_data = htonl(0); 00091 chan->ref_low_quantity = htonf(0.0); 00092 chan->ref_high_data = htonl(1023); 00093 chan->ref_high_quantity = htonf(2.046); 00094 } 00095 00096 static void 00097 dumpCineonChannelInfo(CineonChannelInformation* chan) { 00098 d_printf(" Metric selector: %d", chan->designator1); 00099 switch (chan->designator1) { 00100 case 0: d_printf(" (Universal)\n"); break; 00101 default: d_printf(" (Vendor specific)\n"); break; 00102 } 00103 d_printf(" Metric: %d,", chan->designator2); 00104 switch (chan->designator2) { 00105 case 0: d_printf(" B&W (printing density?)\n"); break; 00106 case 1: d_printf(" Red printing density\n"); break; 00107 case 2: d_printf(" Green printing density\n"); break; 00108 case 3: d_printf(" Blue printing density\n"); break; 00109 case 4: d_printf(" Red CCIR XA/11\n"); break; 00110 case 5: d_printf(" Green CCIR XA/11\n"); break; 00111 case 6: d_printf(" Blue CCIR XA/11\n"); break; 00112 default: d_printf(" (unknown)\n"); break; 00113 } 00114 d_printf(" Bits per pixel %d\n", chan->bits_per_pixel); 00115 d_printf(" Pixels per line %ld\n", (intptr_t)ntohl(chan->pixels_per_line)); 00116 d_printf(" Lines per image %ld\n", (intptr_t)ntohl(chan->lines_per_image)); 00117 d_printf(" Ref low data %ld\n", (intptr_t)ntohl(chan->ref_low_data)); 00118 d_printf(" Ref low quantity %f\n", ntohf(chan->ref_low_quantity)); 00119 d_printf(" Ref high data %ld\n", (intptr_t)ntohl(chan->ref_high_data)); 00120 d_printf(" Ref high quantity %f\n", ntohf(chan->ref_high_quantity)); 00121 } 00122 00123 static void 00124 fillCineonImageInfo(CineonFile* cineon, CineonImageInformation* imageInfo) { 00125 00126 imageInfo->orientation = 0; 00127 imageInfo->channels_per_image = cineon->depth; 00128 00129 if (cineon->depth == 1) { 00130 fillCineonChannelInfo(cineon, &imageInfo->channel[0], 0); 00131 00132 } else if (cineon->depth == 3) { 00133 fillCineonChannelInfo(cineon, &imageInfo->channel[0], 1); 00134 fillCineonChannelInfo(cineon, &imageInfo->channel[1], 2); 00135 fillCineonChannelInfo(cineon, &imageInfo->channel[2], 3); 00136 } 00137 00138 imageInfo->white_point_x = htonf(undefined()); 00139 imageInfo->white_point_y = htonf(undefined()); 00140 imageInfo->red_primary_x = htonf(undefined()); 00141 imageInfo->red_primary_y = htonf(undefined()); 00142 imageInfo->green_primary_x = htonf(undefined()); 00143 imageInfo->green_primary_y = htonf(undefined()); 00144 imageInfo->blue_primary_x = htonf(undefined()); 00145 imageInfo->blue_primary_y = htonf(undefined()); 00146 00147 strcpy(imageInfo->label, "David's Cineon writer."); 00148 00149 } 00150 00151 static void 00152 dumpCineonImageInfo(CineonImageInformation* imageInfo) { 00153 00154 int i; 00155 d_printf("\n--Image Information--\n"); 00156 d_printf("Image orientation %d,", imageInfo->orientation); 00157 switch (imageInfo->orientation) { 00158 case 0: d_printf(" LRTB\n"); break; 00159 case 1: d_printf(" LRBT\n"); break; 00160 case 2: d_printf(" RLTB\n"); break; 00161 case 3: d_printf(" RLBT\n"); break; 00162 case 4: d_printf(" TBLR\n"); break; 00163 case 5: d_printf(" TBRL\n"); break; 00164 case 6: d_printf(" BTLR\n"); break; 00165 case 7: d_printf(" BTRL\n"); break; 00166 default: d_printf(" (unknown)\n"); break; 00167 } 00168 d_printf("Channels %d\n", imageInfo->channels_per_image); 00169 for (i = 0; i < imageInfo->channels_per_image; ++i) { 00170 d_printf(" --Channel %d--\n", i); 00171 dumpCineonChannelInfo(&imageInfo->channel[i]); 00172 } 00173 00174 d_printf("White point x %f\n", ntohf(imageInfo->white_point_x)); 00175 d_printf("White point y %f\n", ntohf(imageInfo->white_point_y)); 00176 d_printf("Red primary x %f\n", ntohf(imageInfo->red_primary_x)); 00177 d_printf("Red primary y %f\n", ntohf(imageInfo->red_primary_y)); 00178 d_printf("Green primary x %f\n", ntohf(imageInfo->green_primary_x)); 00179 d_printf("Green primary y %f\n", ntohf(imageInfo->green_primary_y)); 00180 d_printf("Blue primary x %f\n", ntohf(imageInfo->blue_primary_x)); 00181 d_printf("Blue primary y %f\n", ntohf(imageInfo->blue_primary_y)); 00182 d_printf("Label \"%s\"\n", imageInfo->label); 00183 } 00184 00185 static void 00186 fillCineonFormatInfo(CineonFile* cineon, CineonFormatInformation* formatInfo) { 00187 00188 (void)cineon; /* unused */ 00189 00190 formatInfo->interleave = 0; 00191 formatInfo->packing = 5; 00192 formatInfo->signage = 0; 00193 formatInfo->sense = 0; 00194 formatInfo->line_padding = htonl(0); 00195 formatInfo->channel_padding = htonl(0); 00196 } 00197 00198 static void 00199 dumpCineonFormatInfo(CineonFormatInformation* formatInfo) { 00200 d_printf("\n--Format Information--\n"); 00201 d_printf("Interleave %d,", formatInfo->interleave); 00202 switch (formatInfo->interleave) { 00203 case 0: d_printf(" pixel interleave\n"); break; 00204 case 1: d_printf(" line interleave\n"); break; 00205 case 2: d_printf(" channel interleave\n"); break; 00206 default: d_printf(" (unknown)\n"); break; 00207 } 00208 d_printf("Packing %d,", formatInfo->packing); 00209 if (formatInfo->packing & 0x80) { 00210 d_printf(" multi pixel,"); 00211 } else { 00212 d_printf(" single pixel,"); 00213 } 00214 switch (formatInfo->packing & 0x7F) { 00215 case 0: d_printf(" tight\n"); break; 00216 case 1: d_printf(" byte packed left\n"); break; 00217 case 2: d_printf(" byte packed right\n"); break; 00218 case 3: d_printf(" word packed left\n"); break; 00219 case 4: d_printf(" word packed right\n"); break; 00220 case 5: d_printf(" long packed left\n"); break; 00221 case 6: d_printf(" long packed right\n"); break; 00222 default: d_printf(" (unknown)\n"); break; 00223 } 00224 d_printf("Sign %d,", formatInfo->signage); 00225 if (formatInfo->signage) { 00226 d_printf(" signed\n"); 00227 } else { 00228 d_printf(" unsigned\n"); 00229 } 00230 d_printf("Sense %d,", formatInfo->signage); 00231 if (formatInfo->signage) { 00232 d_printf(" negative\n"); 00233 } else { 00234 d_printf(" positive\n"); 00235 } 00236 d_printf("End of line padding %ld\n", (intptr_t)ntohl(formatInfo->line_padding)); 00237 d_printf("End of channel padding %ld\n", (intptr_t)ntohl(formatInfo->channel_padding)); 00238 } 00239 00240 static void 00241 fillCineonOriginationInfo(CineonFile* cineon, 00242 CineonOriginationInformation* originInfo, CineonFileInformation* fileInfo) { 00243 00244 (void)cineon; /* unused */ 00245 00246 originInfo->x_offset = htonl(0); 00247 originInfo->y_offset = htonl(0); 00248 strcpy(originInfo->file_name, fileInfo->file_name); 00249 strcpy(originInfo->create_date, fileInfo->create_date); 00250 strcpy(originInfo->create_time, fileInfo->create_time); 00251 strncpy(originInfo->input_device, "David's Cineon writer", 64); 00252 strncpy(originInfo->model_number, "Software", 32); 00253 strncpy(originInfo->serial_number, "001", 32); 00254 originInfo->x_input_samples_per_mm = htonf(undefined()); 00255 originInfo->y_input_samples_per_mm = htonf(undefined()); 00256 /* this should probably be undefined, too */ 00257 originInfo->input_device_gamma = htonf(1.0); 00258 } 00259 00260 static void 00261 dumpCineonOriginationInfo(CineonOriginationInformation* originInfo) { 00262 d_printf("\n--Origination Information--\n"); 00263 d_printf("X offset %ld\n", (intptr_t)ntohl(originInfo->x_offset)); 00264 d_printf("Y offset %ld\n", (intptr_t)ntohl(originInfo->y_offset)); 00265 d_printf("File name \"%s\"\n", originInfo->file_name); 00266 d_printf("Creation date \"%s\"\n", originInfo->create_date); 00267 d_printf("Creation time \"%s\"\n", originInfo->create_time); 00268 d_printf("Input device \"%s\"\n", originInfo->input_device); 00269 d_printf("Model number \"%s\"\n", originInfo->model_number); 00270 d_printf("Serial number \"%s\"\n", originInfo->serial_number); 00271 d_printf("Samples per mm in x %f\n", ntohf(originInfo->x_input_samples_per_mm)); 00272 d_printf("Samples per mm in y %f\n", ntohf(originInfo->y_input_samples_per_mm)); 00273 d_printf("Input device gamma %f\n", ntohf(originInfo->input_device_gamma)); 00274 } 00275 00276 int 00277 initCineonGenericHeader(CineonFile* cineon, CineonGenericHeader* header, const char* imagename) { 00278 00279 fillCineonFileInfo(cineon, &header->fileInfo, imagename); 00280 fillCineonImageInfo(cineon, &header->imageInfo); 00281 fillCineonFormatInfo(cineon, &header->formatInfo); 00282 fillCineonOriginationInfo(cineon, &header->originInfo, &header->fileInfo); 00283 00284 return 0; 00285 } 00286 00287 void 00288 dumpCineonGenericHeader(CineonGenericHeader* header) { 00289 dumpCineonFileInfo(&header->fileInfo); 00290 dumpCineonImageInfo(&header->imageInfo); 00291 dumpCineonFormatInfo(&header->formatInfo); 00292 dumpCineonOriginationInfo(&header->originInfo); 00293 } 00294 00295 static int verbose = 0; 00296 void 00297 cineonSetVerbose(int verbosity) { 00298 verbose = verbosity; 00299 } 00300 00301 static void 00302 verboseMe(CineonFile* cineon) { 00303 00304 d_printf("size %d x %d x %d\n", cineon->width, cineon->height, cineon->depth); 00305 d_printf("ImageStart %d, lineBufferLength %d, implied length %d\n", 00306 cineon->imageOffset, cineon->lineBufferLength * 4, 00307 cineon->imageOffset + cineon->lineBufferLength * 4 * cineon->height); 00308 } 00309 00310 int 00311 cineonGetRowBytes(CineonFile* cineon, unsigned short* row, int y) { 00312 00313 int longsRead; 00314 int pixelIndex; 00315 int longIndex; 00316 int numPixels = cineon->width * cineon->depth; 00317 00318 00319 /* only seek if not reading consecutive lines */ 00320 if (y != cineon->fileYPos) { 00321 int lineOffset = cineon->imageOffset + y * cineon->lineBufferLength * 4; 00322 if (verbose) d_printf("Seek in getRowBytes\n"); 00323 if (logimage_fseek(cineon, lineOffset, SEEK_SET) != 0) { 00324 if (verbose) d_printf("Couldn't seek to line %d at %d\n", y, lineOffset); 00325 return 1; 00326 } 00327 cineon->fileYPos = y; 00328 } 00329 00330 longsRead = logimage_fread(cineon->lineBuffer, 4, cineon->lineBufferLength, cineon); 00331 if (longsRead != cineon->lineBufferLength) { 00332 if (verbose) 00333 { d_printf("Couldn't read line %d length %d\n", y, cineon->lineBufferLength * 4); 00334 perror("cineonGetRowBytes"); 00335 } 00336 return 1; 00337 } 00338 00339 /* remember where we left the car, honey */ 00340 ++cineon->fileYPos; 00341 00342 /* convert longwords to pixels */ 00343 pixelIndex = 0; 00344 for (longIndex = 0; longIndex < cineon->lineBufferLength; ++longIndex) { 00345 unsigned int t = ntohl(cineon->lineBuffer[longIndex]); 00346 t = t >> 2; 00347 cineon->pixelBuffer[pixelIndex+2] = (unsigned short) t & 0x3ff; 00348 t = t >> 10; 00349 cineon->pixelBuffer[pixelIndex+1] = (unsigned short) t & 0x3ff; 00350 t = t >> 10; 00351 cineon->pixelBuffer[pixelIndex] = (unsigned short) t & 0x3ff; 00352 pixelIndex += 3; 00353 } 00354 00355 /* extract required pixels */ 00356 for (pixelIndex = 0; pixelIndex < numPixels; ++pixelIndex) { 00357 if(cineon->params.doLogarithm) 00358 row[pixelIndex] = cineon->lut10_16[cineon->pixelBuffer[pixelIndex]]; 00359 else 00360 row[pixelIndex] = cineon->pixelBuffer[pixelIndex] << 6; 00361 } 00362 00363 return 0; 00364 } 00365 00366 int 00367 cineonSetRowBytes(CineonFile* cineon, const unsigned short* row, int y) { 00368 00369 int pixelIndex; 00370 int numPixels = cineon->width * cineon->depth; 00371 int longIndex; 00372 int longsWritten; 00373 00374 /* put new pixels into pixelBuffer */ 00375 for (pixelIndex = 0; pixelIndex < numPixels; ++pixelIndex) { 00376 if(cineon->params.doLogarithm) 00377 cineon->pixelBuffer[pixelIndex] = cineon->lut16_16[row[pixelIndex]]; 00378 else 00379 cineon->pixelBuffer[pixelIndex] = row[pixelIndex] >> 6; 00380 } 00381 00382 /* pack into longwords */ 00383 pixelIndex = 0; 00384 for (longIndex = 0; longIndex < cineon->lineBufferLength; ++longIndex) { 00385 unsigned int t = 00386 (cineon->pixelBuffer[pixelIndex] << 22) | 00387 (cineon->pixelBuffer[pixelIndex+1] << 12) | 00388 (cineon->pixelBuffer[pixelIndex+2] << 2); 00389 cineon->lineBuffer[longIndex] = htonl(t); 00390 pixelIndex += 3; 00391 } 00392 00393 /* only seek if not reading consecutive lines */ 00394 if (y != cineon->fileYPos) { 00395 int lineOffset = cineon->imageOffset + y * cineon->lineBufferLength * 4; 00396 if (verbose) d_printf("Seek in setRowBytes\n"); 00397 if (logimage_fseek(cineon, lineOffset, SEEK_SET) != 0) { 00398 if (verbose) d_printf("Couldn't seek to line %d at %d\n", y, lineOffset); 00399 return 1; 00400 } 00401 cineon->fileYPos = y; 00402 } 00403 00404 longsWritten = fwrite(cineon->lineBuffer, 4, cineon->lineBufferLength, cineon->file); 00405 if (longsWritten != cineon->lineBufferLength) { 00406 if (verbose) d_printf("Couldn't write line %d length %d\n", y, cineon->lineBufferLength * 4); 00407 return 1; 00408 } 00409 00410 ++cineon->fileYPos; 00411 00412 return 0; 00413 } 00414 00415 int 00416 cineonGetRow(CineonFile* cineon, unsigned short* row, int y) { 00417 00418 int longsRead; 00419 int pixelIndex; 00420 int longIndex; 00421 /* int numPixels = cineon->width * cineon->depth; 00422 */ 00423 /* only seek if not reading consecutive lines */ 00424 if (y != cineon->fileYPos) { 00425 int lineOffset = cineon->imageOffset + y * cineon->lineBufferLength * 4; 00426 if (verbose) d_printf("Seek in getRow\n"); 00427 if (logimage_fseek(cineon, lineOffset, SEEK_SET) != 0) { 00428 if (verbose) d_printf("Couldn't seek to line %d at %d\n", y, lineOffset); 00429 return 1; 00430 } 00431 cineon->fileYPos = y; 00432 } 00433 00434 longsRead = logimage_fread(cineon->lineBuffer, 4, cineon->lineBufferLength, cineon); 00435 if (longsRead != cineon->lineBufferLength) { 00436 if (verbose) d_printf("Couldn't read line %d length %d\n", y, cineon->lineBufferLength * 4); 00437 return 1; 00438 } 00439 00440 /* remember where we left the car, honey */ 00441 ++cineon->fileYPos; 00442 00443 /* convert longwords to pixels */ 00444 pixelIndex = 0; 00445 for (longIndex = 0; longIndex < cineon->lineBufferLength; ++longIndex) { 00446 unsigned int t = ntohl(cineon->lineBuffer[longIndex]); 00447 t = t >> 2; 00448 row[pixelIndex+2] = (unsigned short) t & 0x3ff; 00449 t = t >> 10; 00450 row[pixelIndex+1] = (unsigned short) t & 0x3ff; 00451 t = t >> 10; 00452 row[pixelIndex] = (unsigned short) t & 0x3ff; 00453 pixelIndex += 3; 00454 } 00455 00456 return 0; 00457 } 00458 00459 int 00460 cineonSetRow(CineonFile* cineon, const unsigned short* row, int y) { 00461 00462 int pixelIndex; 00463 /* int numPixels = cineon->width * cineon->depth; 00464 */ int longIndex; 00465 int longsWritten; 00466 00467 /* pack into longwords */ 00468 pixelIndex = 0; 00469 for (longIndex = 0; longIndex < cineon->lineBufferLength; ++longIndex) { 00470 unsigned int t = 00471 (row[pixelIndex] << 22) | 00472 (row[pixelIndex+1] << 12) | 00473 (row[pixelIndex+2] << 2); 00474 cineon->lineBuffer[longIndex] = htonl(t); 00475 pixelIndex += 3; 00476 } 00477 00478 /* only seek if not reading consecutive lines */ 00479 if (y != cineon->fileYPos) { 00480 int lineOffset = cineon->imageOffset + y * cineon->lineBufferLength * 4; 00481 if (verbose) d_printf("Seek in setRowBytes\n"); 00482 if (logimage_fseek(cineon, lineOffset, SEEK_SET) != 0) { 00483 if (verbose) d_printf("Couldn't seek to line %d at %d\n", y, lineOffset); 00484 return 1; 00485 } 00486 cineon->fileYPos = y; 00487 } 00488 00489 longsWritten = fwrite(cineon->lineBuffer, 4, cineon->lineBufferLength, cineon->file); 00490 if (longsWritten != cineon->lineBufferLength) { 00491 if (verbose) d_printf("Couldn't write line %d length %d\n", y, cineon->lineBufferLength * 4); 00492 return 1; 00493 } 00494 00495 ++cineon->fileYPos; 00496 00497 return 0; 00498 } 00499 00500 CineonFile* 00501 cineonOpen(const char* filename) { 00502 00503 CineonGenericHeader header; 00504 00505 CineonFile* cineon = (CineonFile* )malloc(sizeof(CineonFile)); 00506 if (cineon == 0) { 00507 if (verbose) d_printf("Failed to malloc cineon file structure.\n"); 00508 return 0; 00509 } 00510 00511 /* for close routine */ 00512 cineon->file = 0; 00513 cineon->lineBuffer = 0; 00514 cineon->pixelBuffer = 0; 00515 cineon->membuffer = 0; 00516 cineon->memcursor = 0; 00517 cineon->membuffersize = 0; 00518 00519 cineon->file = fopen(filename, "rb"); 00520 if (cineon->file == 0) { 00521 if (verbose) d_printf("Failed to open file \"%s\".\n", filename); 00522 cineonClose(cineon); 00523 return 0; 00524 } 00525 cineon->reading = 1; 00526 00527 if (logimage_fread(&header, sizeof(CineonGenericHeader), 1, cineon) == 0) { 00528 if (verbose) d_printf("Not enough data for header in \"%s\".\n", filename); 00529 cineonClose(cineon); 00530 return 0; 00531 } 00532 00533 /* let's assume cineon files are always network order */ 00534 if (header.fileInfo.magic_num != ntohl(CINEON_FILE_MAGIC)) { 00535 if (verbose) d_printf("Bad magic number %8.8lX in \"%s\".\n", 00536 (uintptr_t)ntohl(header.fileInfo.magic_num), filename); 00537 cineonClose(cineon); 00538 return 0; 00539 } 00540 00541 if (header.formatInfo.packing != 5) { 00542 if (verbose) d_printf("Can't understand packing %d\n", header.formatInfo.packing); 00543 cineonClose(cineon); 00544 return 0; 00545 } 00546 00547 cineon->width = ntohl(header.imageInfo.channel[0].pixels_per_line); 00548 cineon->height = ntohl(header.imageInfo.channel[0].lines_per_image); 00549 cineon->depth = header.imageInfo.channels_per_image; 00550 /* cineon->bitsPerPixel = 10; */ 00551 cineon->bitsPerPixel = header.imageInfo.channel[0].bits_per_pixel; 00552 cineon->imageOffset = ntohl(header.fileInfo.image_offset); 00553 00554 cineon->lineBufferLength = pixelsToLongs(cineon->width * cineon->depth); 00555 cineon->lineBuffer = malloc(cineon->lineBufferLength * 4); 00556 if (cineon->lineBuffer == 0) { 00557 if (verbose) d_printf("Couldn't malloc line buffer of size %d\n", cineon->lineBufferLength * 4); 00558 cineonClose(cineon); 00559 return 0; 00560 } 00561 00562 cineon->pixelBuffer = malloc(cineon->lineBufferLength * 3 * sizeof(unsigned short)); 00563 if (cineon->pixelBuffer == 0) { 00564 if (verbose) d_printf("Couldn't malloc pixel buffer of size %d\n", 00565 (cineon->width * cineon->depth) * (int)sizeof(unsigned short)); 00566 cineonClose(cineon); 00567 return 0; 00568 } 00569 cineon->pixelBufferUsed = 0; 00570 00571 if (logimage_fseek(cineon, cineon->imageOffset, SEEK_SET) != 0) { 00572 if (verbose) d_printf("Couldn't seek to image data at %d\n", cineon->imageOffset); 00573 cineonClose(cineon); 00574 return 0; 00575 } 00576 cineon->fileYPos = 0; 00577 00578 logImageGetByteConversionDefaults(&cineon->params); 00579 setupLut(cineon); 00580 00581 cineon->getRow = &cineonGetRowBytes; 00582 cineon->setRow = 0; 00583 cineon->close = &cineonClose; 00584 00585 if (verbose) { 00586 verboseMe(cineon); 00587 } 00588 00589 return cineon; 00590 } 00591 00592 int cineonIsMemFileCineon(unsigned char *mem) 00593 { 00594 unsigned int num; 00595 memcpy(&num, mem, sizeof(unsigned int)); 00596 00597 if (num != ntohl(CINEON_FILE_MAGIC)) { 00598 return 0; 00599 } else return 1; 00600 } 00601 00602 CineonFile* 00603 cineonOpenFromMem(unsigned char *mem, unsigned int size) { 00604 00605 CineonGenericHeader header; 00606 int i; 00607 00608 CineonFile* cineon = (CineonFile* )malloc(sizeof(CineonFile)); 00609 if (cineon == 0) { 00610 if (verbose) d_printf("Failed to malloc cineon file structure.\n"); 00611 return 0; 00612 } 00613 00614 /* for close routine */ 00615 cineon->file = 0; 00616 cineon->lineBuffer = 0; 00617 cineon->pixelBuffer = 0; 00618 cineon->membuffer = mem; 00619 cineon->membuffersize = size; 00620 cineon->memcursor = mem; 00621 00622 cineon->file = 0; 00623 cineon->reading = 1; 00624 verbose = 0; 00625 if (size < sizeof(CineonGenericHeader)) { 00626 if (verbose) d_printf("Not enough data for header!\n"); 00627 cineonClose(cineon); 00628 return 0; 00629 } 00630 00631 logimage_fread(&header, sizeof(CineonGenericHeader), 1, cineon); 00632 00633 /* let's assume cineon files are always network order */ 00634 if (header.fileInfo.magic_num != ntohl(CINEON_FILE_MAGIC)) { 00635 if (verbose) d_printf("Bad magic number %8.8lX in\n", (uintptr_t)ntohl(header.fileInfo.magic_num)); 00636 00637 cineonClose(cineon); 00638 return 0; 00639 } 00640 00641 if (header.formatInfo.packing != 5) { 00642 if (verbose) d_printf("Can't understand packing %d\n", header.formatInfo.packing); 00643 cineonClose(cineon); 00644 return 0; 00645 } 00646 00647 cineon->width = ntohl(header.imageInfo.channel[0].pixels_per_line); 00648 cineon->height = ntohl(header.imageInfo.channel[0].lines_per_image); 00649 cineon->depth = header.imageInfo.channels_per_image; 00650 /* cineon->bitsPerPixel = 10; */ 00651 cineon->bitsPerPixel = header.imageInfo.channel[0].bits_per_pixel; 00652 cineon->imageOffset = ntohl(header.fileInfo.image_offset); 00653 00654 cineon->lineBufferLength = pixelsToLongs(cineon->width * cineon->depth); 00655 cineon->lineBuffer = malloc(cineon->lineBufferLength * 4); 00656 if (cineon->lineBuffer == 0) { 00657 if (verbose) d_printf("Couldn't malloc line buffer of size %d\n", cineon->lineBufferLength * 4); 00658 cineonClose(cineon); 00659 return 0; 00660 } 00661 00662 cineon->pixelBuffer = malloc(cineon->lineBufferLength * 3 * sizeof(unsigned short)); 00663 if (cineon->pixelBuffer == 0) { 00664 if (verbose) d_printf("Couldn't malloc pixel buffer of size %d\n", 00665 (cineon->width * cineon->depth) * (int)sizeof(unsigned short)); 00666 cineonClose(cineon); 00667 return 0; 00668 } 00669 cineon->pixelBufferUsed = 0; 00670 00671 i = cineon->imageOffset; 00672 00673 if (logimage_fseek(cineon, cineon->imageOffset, SEEK_SET) != 0) { 00674 if (verbose) d_printf("Couldn't seek to image data at %d\n", cineon->imageOffset); 00675 cineonClose(cineon); 00676 return 0; 00677 } 00678 00679 cineon->fileYPos = 0; 00680 00681 logImageGetByteConversionDefaults(&cineon->params); 00682 setupLut(cineon); 00683 00684 cineon->getRow = &cineonGetRowBytes; 00685 cineon->setRow = 0; 00686 cineon->close = &cineonClose; 00687 00688 if (verbose) { 00689 verboseMe(cineon); 00690 } 00691 00692 return cineon; 00693 } 00694 00695 00696 int 00697 cineonGetSize(const CineonFile* cineon, int* width, int* height, int* depth) { 00698 *width = cineon->width; 00699 *height = cineon->height; 00700 *depth = cineon->depth; 00701 return 0; 00702 } 00703 00704 CineonFile* 00705 cineonCreate(const char* filename, int width, int height, int depth) { 00706 00707 /* Note: always write files in network order */ 00708 /* By the spec, it shouldn't matter, but ... */ 00709 00710 CineonGenericHeader header; 00711 const char* shortFilename = 0; 00712 00713 CineonFile* cineon = (CineonFile*)malloc(sizeof(CineonFile)); 00714 if (cineon == 0) { 00715 if (verbose) d_printf("Failed to malloc cineon file structure.\n"); 00716 return 0; 00717 } 00718 00719 memset(&header, 0, sizeof(header)); 00720 00721 /* for close routine */ 00722 cineon->file = 0; 00723 cineon->lineBuffer = 0; 00724 cineon->pixelBuffer = 0; 00725 00726 cineon->file = fopen(filename, "wb"); 00727 if (cineon->file == 0) { 00728 if (verbose) d_printf("Couldn't open file %s\n", filename); 00729 cineonClose(cineon); 00730 return 0; 00731 } 00732 cineon->reading = 0; 00733 00734 cineon->width = width; 00735 cineon->height = height; 00736 cineon->depth = depth; 00737 cineon->bitsPerPixel = 10; 00738 cineon->imageOffset = sizeof(CineonGenericHeader); 00739 00740 cineon->lineBufferLength = pixelsToLongs(cineon->width * cineon->depth); 00741 cineon->lineBuffer = malloc(cineon->lineBufferLength * 4); 00742 if (cineon->lineBuffer == 0) { 00743 if (verbose) d_printf("Couldn't malloc line buffer of size %d\n", cineon->lineBufferLength * 4); 00744 cineonClose(cineon); 00745 return 0; 00746 } 00747 00748 cineon->pixelBuffer = malloc(cineon->lineBufferLength * 3 * sizeof(unsigned short)); 00749 if (cineon->pixelBuffer == 0) { 00750 if (verbose) d_printf("Couldn't malloc pixel buffer of size %d\n", 00751 (cineon->width * cineon->depth) * (int)sizeof(unsigned short)); 00752 cineonClose(cineon); 00753 return 0; 00754 } 00755 cineon->pixelBufferUsed = 0; 00756 00757 /* find trailing part of filename */ 00758 shortFilename = strrchr(filename, '/'); 00759 if (shortFilename == 0) { 00760 shortFilename = filename; 00761 } else { 00762 ++shortFilename; 00763 } 00764 00765 if (initCineonGenericHeader(cineon, &header, shortFilename) != 0) { 00766 cineonClose(cineon); 00767 return 0; 00768 } 00769 00770 if (fwrite(&header, sizeof(header), 1, cineon->file) == 0) { 00771 if (verbose) d_printf("Couldn't write image header\n"); 00772 cineonClose(cineon); 00773 return 0; 00774 } 00775 cineon->fileYPos = 0; 00776 00777 logImageGetByteConversionDefaults(&cineon->params); 00778 setupLut(cineon); 00779 00780 cineon->getRow = 0; 00781 cineon->setRow = &cineonSetRowBytes; 00782 cineon->close = &cineonClose; 00783 00784 return cineon; 00785 } 00786 00787 void 00788 cineonClose(CineonFile* cineon) { 00789 00790 if (cineon == 0) { 00791 return; 00792 } 00793 00794 if (cineon->file) { 00795 fclose(cineon->file); 00796 cineon->file = 0; 00797 } 00798 00799 if (cineon->lineBuffer) { 00800 free(cineon->lineBuffer); 00801 cineon->lineBuffer = 0; 00802 } 00803 00804 if (cineon->pixelBuffer) { 00805 free(cineon->pixelBuffer); 00806 cineon->pixelBuffer = 0; 00807 } 00808 00809 free(cineon); 00810 }
1.7.3