|
Blender V2.27
|
00001 00033 /* This little block needed for linking to Blender... */ 00034 #include <stdio.h> 00035 #include <setjmp.h> 00036 00037 #include "MEM_guardedalloc.h" 00038 00039 #include "BLI_blenlib.h" 00040 00041 #include "imbuf.h" 00042 #include "IMB_imbuf_types.h" 00043 #include "IMB_imbuf.h" 00044 #include "IMB_metadata.h" 00045 #include "IMB_filetype.h" 00046 #include "jpeglib.h" 00047 #include "jerror.h" 00048 00049 #define IS_jpg(x) (x->ftype & JPG) 00050 #define IS_stdjpg(x) ((x->ftype & JPG_MSK) == JPG_STD) 00051 #define IS_vidjpg(x) ((x->ftype & JPG_MSK) == JPG_VID) 00052 #define IS_jstjpg(x) ((x->ftype & JPG_MSK) == JPG_JST) 00053 #define IS_maxjpg(x) ((x->ftype & JPG_MSK) == JPG_MAX) 00054 00055 /* the types are from the jpeg lib */ 00056 static void jpeg_error (j_common_ptr cinfo); 00057 static void init_source(j_decompress_ptr cinfo); 00058 static boolean fill_input_buffer(j_decompress_ptr cinfo); 00059 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes); 00060 static void term_source(j_decompress_ptr cinfo); 00061 static void memory_source(j_decompress_ptr cinfo, unsigned char *buffer, size_t size); 00062 static boolean handle_app1 (j_decompress_ptr cinfo); 00063 static ImBuf * ibJpegImageFromCinfo(struct jpeg_decompress_struct * cinfo, int flags); 00064 00065 00066 /* 00067 * In principle there are 4 jpeg formats. 00068 * 00069 * 1. jpeg - standard printing, u & v at quarter of resulution 00070 * 2. jvid - standaard video, u & v half resolution, frame not interlaced 00071 00072 type 3 is unsupported as of jul 05 2000 Frank. 00073 00074 * 3. jstr - as 2, but written in 2 separate fields 00075 00076 * 4. jmax - no scaling in the components 00077 */ 00078 00079 static int jpeg_default_quality; 00080 static int ibuf_ftype; 00081 00082 int imb_is_a_jpeg(unsigned char *mem) { 00083 00084 if ((mem[0]== 0xFF) && (mem[1] == 0xD8))return 1; 00085 return 0; 00086 } 00087 00088 //---------------------------------------------------------- 00089 // JPG ERROR HANDLING 00090 //---------------------------------------------------------- 00091 00092 typedef struct my_error_mgr { 00093 struct jpeg_error_mgr pub; /* "public" fields */ 00094 00095 jmp_buf setjmp_buffer; /* for return to caller */ 00096 } my_error_mgr; 00097 00098 typedef my_error_mgr * my_error_ptr; 00099 00100 static void jpeg_error (j_common_ptr cinfo) 00101 { 00102 my_error_ptr err = (my_error_ptr)cinfo->err; 00103 00104 /* Always display the message */ 00105 (*cinfo->err->output_message) (cinfo); 00106 00107 /* Let the memory manager delete any temp files before we die */ 00108 jpeg_destroy(cinfo); 00109 00110 /* return control to the setjmp point */ 00111 longjmp(err->setjmp_buffer, 1); 00112 } 00113 00114 //---------------------------------------------------------- 00115 // INPUT HANDLER FROM MEMORY 00116 //---------------------------------------------------------- 00117 00118 typedef struct { 00119 unsigned char *buffer; 00120 int filled; 00121 } buffer_struct; 00122 00123 typedef struct { 00124 struct jpeg_source_mgr pub; /* public fields */ 00125 00126 unsigned char *buffer; 00127 int size; 00128 JOCTET terminal[2]; 00129 } my_source_mgr; 00130 00131 typedef my_source_mgr * my_src_ptr; 00132 00133 static void init_source(j_decompress_ptr cinfo) 00134 { 00135 (void)cinfo; /* unused */ 00136 } 00137 00138 00139 static boolean fill_input_buffer(j_decompress_ptr cinfo) 00140 { 00141 my_src_ptr src = (my_src_ptr) cinfo->src; 00142 00143 /* Since we have given all we have got already 00144 * we simply fake an end of file 00145 */ 00146 00147 src->pub.next_input_byte = src->terminal; 00148 src->pub.bytes_in_buffer = 2; 00149 src->terminal[0] = (JOCTET) 0xFF; 00150 src->terminal[1] = (JOCTET) JPEG_EOI; 00151 00152 return TRUE; 00153 } 00154 00155 00156 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) 00157 { 00158 my_src_ptr src = (my_src_ptr) cinfo->src; 00159 00160 if(num_bytes > 0) { 00161 src->pub.next_input_byte = src->pub.next_input_byte + num_bytes; 00162 src->pub.bytes_in_buffer = src->pub.bytes_in_buffer - num_bytes; 00163 } 00164 } 00165 00166 00167 static void term_source(j_decompress_ptr cinfo) 00168 { 00169 (void)cinfo; /* unused */ 00170 } 00171 00172 static void memory_source(j_decompress_ptr cinfo, unsigned char *buffer, size_t size) 00173 { 00174 my_src_ptr src; 00175 00176 if (cinfo->src == NULL) 00177 { /* first time for this JPEG object? */ 00178 cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small) 00179 ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr)); 00180 } 00181 00182 src = (my_src_ptr) cinfo->src; 00183 src->pub.init_source = init_source; 00184 src->pub.fill_input_buffer = fill_input_buffer; 00185 src->pub.skip_input_data = skip_input_data; 00186 src->pub.resync_to_restart = jpeg_resync_to_restart; 00187 src->pub.term_source = term_source; 00188 00189 src->pub.bytes_in_buffer = size; 00190 src->pub.next_input_byte = buffer; 00191 00192 src->buffer = buffer; 00193 src->size = size; 00194 } 00195 00196 00197 #define MAKESTMT(stuff) do { stuff } while (0) 00198 00199 #define INPUT_VARS(cinfo) \ 00200 struct jpeg_source_mgr * datasrc = (cinfo)->src; \ 00201 const JOCTET * next_input_byte = datasrc->next_input_byte; \ 00202 size_t bytes_in_buffer = datasrc->bytes_in_buffer 00203 00204 /* Unload the local copies --- do this only at a restart boundary */ 00205 #define INPUT_SYNC(cinfo) \ 00206 ( datasrc->next_input_byte = next_input_byte, \ 00207 datasrc->bytes_in_buffer = bytes_in_buffer ) 00208 00209 /* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */ 00210 #define INPUT_RELOAD(cinfo) \ 00211 ( next_input_byte = datasrc->next_input_byte, \ 00212 bytes_in_buffer = datasrc->bytes_in_buffer ) 00213 00214 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available. 00215 * Note we do *not* do INPUT_SYNC before calling fill_input_buffer, 00216 * but we must reload the local copies after a successful fill. 00217 */ 00218 #define MAKE_BYTE_AVAIL(cinfo,action) \ 00219 if (bytes_in_buffer == 0) { \ 00220 if (! (*datasrc->fill_input_buffer) (cinfo)) \ 00221 { action; } \ 00222 INPUT_RELOAD(cinfo); \ 00223 } \ 00224 bytes_in_buffer-- 00225 00226 /* Read a byte into variable V. 00227 * If must suspend, take the specified action (typically "return FALSE"). 00228 */ 00229 #define INPUT_BYTE(cinfo,V,action) \ 00230 MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ 00231 V = GETJOCTET(*next_input_byte++); ) 00232 00233 /* As above, but read two bytes interpreted as an unsigned 16-bit integer. 00234 * V should be declared unsigned int or perhaps INT32. 00235 */ 00236 #define INPUT_2BYTES(cinfo,V,action) \ 00237 MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ 00238 V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \ 00239 MAKE_BYTE_AVAIL(cinfo,action); \ 00240 V += GETJOCTET(*next_input_byte++); ) 00241 00242 00243 static boolean 00244 handle_app1 (j_decompress_ptr cinfo) 00245 { 00246 INT32 length, i; 00247 char neogeo[128]; 00248 00249 INPUT_VARS(cinfo); 00250 00251 INPUT_2BYTES(cinfo, length, return FALSE); 00252 length -= 2; 00253 00254 if (length < 16) { 00255 for (i = 0; i < length; i++) INPUT_BYTE(cinfo, neogeo[i], return FALSE); 00256 length = 0; 00257 if (strncmp(neogeo, "NeoGeo", 6) == 0) memcpy(&ibuf_ftype, neogeo + 6, 4); 00258 ibuf_ftype = BIG_LONG(ibuf_ftype); 00259 } 00260 INPUT_SYNC(cinfo); /* do before skip_input_data */ 00261 if (length > 0) (*cinfo->src->skip_input_data) (cinfo, length); 00262 return TRUE; 00263 } 00264 00265 00266 static ImBuf * ibJpegImageFromCinfo(struct jpeg_decompress_struct * cinfo, int flags) 00267 { 00268 JSAMPARRAY row_pointer; 00269 JSAMPLE * buffer = 0; 00270 int row_stride; 00271 int x, y, depth, r, g, b, k; 00272 struct ImBuf * ibuf = 0; 00273 uchar * rect; 00274 jpeg_saved_marker_ptr marker; 00275 char *str, *key, *value; 00276 00277 /* install own app1 handler */ 00278 ibuf_ftype = 0; 00279 jpeg_set_marker_processor(cinfo, 0xe1, handle_app1); 00280 cinfo->dct_method = JDCT_FLOAT; 00281 jpeg_save_markers(cinfo, JPEG_COM, 0xffff); 00282 00283 if (jpeg_read_header(cinfo, FALSE) == JPEG_HEADER_OK) { 00284 x = cinfo->image_width; 00285 y = cinfo->image_height; 00286 depth = cinfo->num_components; 00287 00288 if (cinfo->jpeg_color_space == JCS_YCCK) cinfo->out_color_space = JCS_CMYK; 00289 00290 jpeg_start_decompress(cinfo); 00291 00292 if (ibuf_ftype == 0) { 00293 ibuf_ftype = JPG_STD; 00294 if (cinfo->max_v_samp_factor == 1) { 00295 if (cinfo->max_h_samp_factor == 1) ibuf_ftype = JPG_MAX; 00296 else ibuf_ftype = JPG_VID; 00297 } 00298 } 00299 00300 if (flags & IB_test) { 00301 jpeg_abort_decompress(cinfo); 00302 ibuf = IMB_allocImBuf(x, y, 8 * depth, 0); 00303 } 00304 else if ((ibuf = IMB_allocImBuf(x, y, 8 * depth, IB_rect)) == NULL) { 00305 jpeg_abort_decompress(cinfo); 00306 } 00307 else { 00308 row_stride = cinfo->output_width * depth; 00309 00310 row_pointer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, row_stride, 1); 00311 00312 for (y = ibuf->y - 1; y >= 0; y--) { 00313 jpeg_read_scanlines(cinfo, row_pointer, 1); 00314 rect = (uchar *) (ibuf->rect + y * ibuf->x); 00315 buffer = row_pointer[0]; 00316 00317 switch(depth) { 00318 case 1: 00319 for (x=ibuf->x; x >0; x--) { 00320 rect[3] = 255; 00321 rect[0] = rect[1] = rect[2] = *buffer++; 00322 rect += 4; 00323 } 00324 break; 00325 case 3: 00326 for (x=ibuf->x; x >0; x--) { 00327 rect[3] = 255; 00328 rect[0] = *buffer++; 00329 rect[1] = *buffer++; 00330 rect[2] = *buffer++; 00331 rect += 4; 00332 } 00333 break; 00334 case 4: 00335 for (x=ibuf->x; x >0; x--) { 00336 r = *buffer++; 00337 g = *buffer++; 00338 b = *buffer++; 00339 k = *buffer++; 00340 00341 k = 255 - k; 00342 r -= k; 00343 if (r & 0xffffff00) { 00344 if (r < 0) r = 0; 00345 else r = 255; 00346 } 00347 g -= k; 00348 if (g & 0xffffff00) { 00349 if (g < 0) g = 0; 00350 else g = 255; 00351 } 00352 b -= k; 00353 if (b & 0xffffff00) { 00354 if (b < 0) b = 0; 00355 else b = 255; 00356 } 00357 00358 rect[3] = 255 - k; 00359 rect[2] = b; 00360 rect[1] = g; 00361 rect[0] = r; 00362 rect += 4; 00363 } 00364 } 00365 } 00366 00367 marker= cinfo->marker_list; 00368 while(marker) { 00369 if(marker->marker != JPEG_COM) 00370 goto next_stamp_marker; 00371 00372 /* 00373 * Because JPEG format don't support the 00374 * pair "key/value" like PNG, we store the 00375 * stampinfo in a single "encode" string: 00376 * "Blender:key:value" 00377 * 00378 * That is why we need split it to the 00379 * common key/value here. 00380 */ 00381 if(strncmp((char *) marker->data, "Blender", 7)) { 00382 /* 00383 * Maybe the file have text that 00384 * we don't know "what it's", in that 00385 * case we keep the text (with a 00386 * key "None"). 00387 * This is only for don't "lose" 00388 * the information when we write 00389 * it back to disk. 00390 */ 00391 IMB_metadata_add_field(ibuf, "None", (char *) marker->data); 00392 ibuf->flags |= IB_metadata; 00393 goto next_stamp_marker; 00394 } 00395 00396 str = BLI_strdup ((char *) marker->data); 00397 key = strchr (str, ':'); 00398 /* 00399 * A little paranoid, but the file maybe 00400 * is broken... and a "extra" check is better 00401 * that a segfaul ;) 00402 */ 00403 if (!key) { 00404 MEM_freeN(str); 00405 goto next_stamp_marker; 00406 } 00407 00408 key++; 00409 value = strchr (key, ':'); 00410 if (!value) { 00411 MEM_freeN(str); 00412 goto next_stamp_marker; 00413 } 00414 00415 *value = '\0'; /* need finish the key string */ 00416 value++; 00417 IMB_metadata_add_field(ibuf, key, value); 00418 ibuf->flags |= IB_metadata; 00419 MEM_freeN(str); 00420 next_stamp_marker: 00421 marker= marker->next; 00422 } 00423 00424 jpeg_finish_decompress(cinfo); 00425 } 00426 00427 jpeg_destroy((j_common_ptr) cinfo); 00428 if(ibuf) { 00429 ibuf->ftype = ibuf_ftype; 00430 ibuf->profile = IB_PROFILE_SRGB; 00431 } 00432 } 00433 00434 return(ibuf); 00435 } 00436 00437 ImBuf * imb_ibJpegImageFromFilename (const char * filename, int flags) 00438 { 00439 struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo; 00440 struct my_error_mgr jerr; 00441 FILE * infile; 00442 ImBuf * ibuf; 00443 00444 if ((infile = fopen(filename, "rb")) == NULL) return 0; 00445 00446 cinfo->err = jpeg_std_error(&jerr.pub); 00447 jerr.pub.error_exit = jpeg_error; 00448 00449 /* Establish the setjmp return context for my_error_exit to use. */ 00450 if (setjmp(jerr.setjmp_buffer)) { 00451 /* If we get here, the JPEG code has signaled an error. 00452 * We need to clean up the JPEG object, close the input file, and return. 00453 */ 00454 jpeg_destroy_decompress(cinfo); 00455 fclose(infile); 00456 return NULL; 00457 } 00458 00459 jpeg_create_decompress(cinfo); 00460 jpeg_stdio_src(cinfo, infile); 00461 00462 ibuf = ibJpegImageFromCinfo(cinfo, flags); 00463 00464 fclose(infile); 00465 return(ibuf); 00466 } 00467 00468 ImBuf * imb_load_jpeg (unsigned char * buffer, size_t size, int flags) 00469 { 00470 struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo; 00471 struct my_error_mgr jerr; 00472 ImBuf * ibuf; 00473 00474 if(!imb_is_a_jpeg(buffer)) return NULL; 00475 00476 cinfo->err = jpeg_std_error(&jerr.pub); 00477 jerr.pub.error_exit = jpeg_error; 00478 00479 /* Establish the setjmp return context for my_error_exit to use. */ 00480 if (setjmp(jerr.setjmp_buffer)) { 00481 /* If we get here, the JPEG code has signaled an error. 00482 * We need to clean up the JPEG object, close the input file, and return. 00483 */ 00484 jpeg_destroy_decompress(cinfo); 00485 return NULL; 00486 } 00487 00488 jpeg_create_decompress(cinfo); 00489 memory_source(cinfo, buffer, size); 00490 00491 ibuf = ibJpegImageFromCinfo(cinfo, flags); 00492 00493 return(ibuf); 00494 } 00495 00496 00497 static void write_jpeg(struct jpeg_compress_struct * cinfo, struct ImBuf * ibuf) 00498 { 00499 JSAMPLE * buffer = 0; 00500 JSAMPROW row_pointer[1]; 00501 uchar * rect; 00502 int x, y; 00503 char neogeo[128]; 00504 ImMetaData *iptr; 00505 char *text; 00506 00507 jpeg_start_compress(cinfo, TRUE); 00508 00509 strcpy(neogeo, "NeoGeo"); 00510 ibuf_ftype = BIG_LONG(ibuf->ftype); 00511 00512 memcpy(neogeo + 6, &ibuf_ftype, 4); 00513 jpeg_write_marker(cinfo, 0xe1, (JOCTET*) neogeo, 10); 00514 00515 if(ibuf->metadata) { 00516 /* key + max value + "Blender" */ 00517 text= MEM_mallocN(530, "stamp info read"); 00518 iptr= ibuf->metadata; 00519 while(iptr) { 00520 if (!strcmp (iptr->key, "None")) { 00521 jpeg_write_marker(cinfo, JPEG_COM, (JOCTET *) iptr->value, strlen (iptr->value) + 1); 00522 goto next_stamp_info; 00523 } 00524 00525 /* 00526 * The JPEG format don't support a pair "key/value" 00527 * like PNG, so we "encode" the stamp in a 00528 * single string: 00529 * "Blender:key:value" 00530 * 00531 * The first "Blender" is a simple identify to help 00532 * in the read process. 00533 */ 00534 sprintf (text, "Blender:%s:%s", iptr->key, iptr->value); 00535 jpeg_write_marker(cinfo, JPEG_COM, (JOCTET *) text, strlen (text)+1); 00536 next_stamp_info: 00537 iptr = iptr->next; 00538 } 00539 MEM_freeN(text); 00540 } 00541 00542 row_pointer[0] = 00543 MEM_mallocN(sizeof(JSAMPLE) * 00544 cinfo->input_components * 00545 cinfo->image_width, "jpeg row_pointer"); 00546 00547 for(y = ibuf->y - 1; y >= 0; y--){ 00548 rect = (uchar *) (ibuf->rect + y * ibuf->x); 00549 buffer = row_pointer[0]; 00550 00551 switch(cinfo->in_color_space){ 00552 case JCS_RGB: 00553 for (x = 0; x < ibuf->x; x++) { 00554 *buffer++ = rect[0]; 00555 *buffer++ = rect[1]; 00556 *buffer++ = rect[2]; 00557 rect += 4; 00558 } 00559 break; 00560 case JCS_GRAYSCALE: 00561 for (x = 0; x < ibuf->x; x++) { 00562 *buffer++ = rect[0]; 00563 rect += 4; 00564 } 00565 break; 00566 case JCS_UNKNOWN: 00567 memcpy(buffer, rect, 4 * ibuf->x); 00568 break; 00569 /* default was missing... intentional ? */ 00570 default: 00571 ; /* do nothing */ 00572 } 00573 00574 jpeg_write_scanlines(cinfo, row_pointer, 1); 00575 } 00576 00577 jpeg_finish_compress(cinfo); 00578 MEM_freeN(row_pointer[0]); 00579 } 00580 00581 00582 static int init_jpeg(FILE * outfile, struct jpeg_compress_struct * cinfo, struct ImBuf *ibuf) 00583 { 00584 int quality; 00585 00586 quality = ibuf->ftype & 0xff; 00587 if (quality <= 0) quality = jpeg_default_quality; 00588 if (quality > 100) quality = 100; 00589 00590 jpeg_create_compress(cinfo); 00591 jpeg_stdio_dest(cinfo, outfile); 00592 00593 cinfo->image_width = ibuf->x; 00594 cinfo->image_height = ibuf->y; 00595 00596 cinfo->in_color_space = JCS_RGB; 00597 if (ibuf->depth == 8) cinfo->in_color_space = JCS_GRAYSCALE; 00598 if (ibuf->depth == 32) cinfo->in_color_space = JCS_UNKNOWN; 00599 00600 switch(cinfo->in_color_space){ 00601 case JCS_RGB: 00602 cinfo->input_components = 3; 00603 break; 00604 case JCS_GRAYSCALE: 00605 cinfo->input_components = 1; 00606 break; 00607 case JCS_UNKNOWN: 00608 cinfo->input_components = 4; 00609 break; 00610 /* default was missing... intentional ? */ 00611 default: 00612 ; /* do nothing */ 00613 } 00614 jpeg_set_defaults(cinfo); 00615 00616 /* own settings */ 00617 00618 cinfo->dct_method = JDCT_FLOAT; 00619 jpeg_set_quality(cinfo, quality, TRUE); 00620 00621 return(0); 00622 } 00623 00624 00625 static int save_stdjpeg(const char *name, struct ImBuf *ibuf) 00626 { 00627 FILE * outfile; 00628 struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo; 00629 struct my_error_mgr jerr; 00630 00631 if ((outfile = fopen(name, "wb")) == NULL) return 0; 00632 jpeg_default_quality = 75; 00633 00634 cinfo->err = jpeg_std_error(&jerr.pub); 00635 jerr.pub.error_exit = jpeg_error; 00636 00637 /* Establish the setjmp return context for jpeg_error to use. */ 00638 if (setjmp(jerr.setjmp_buffer)) { 00639 /* If we get here, the JPEG code has signaled an error. 00640 * We need to clean up the JPEG object, close the input file, and return. 00641 */ 00642 jpeg_destroy_compress(cinfo); 00643 fclose(outfile); 00644 remove(name); 00645 return 0; 00646 } 00647 00648 init_jpeg(outfile, cinfo, ibuf); 00649 00650 write_jpeg(cinfo, ibuf); 00651 00652 fclose(outfile); 00653 jpeg_destroy_compress(cinfo); 00654 00655 return 1; 00656 } 00657 00658 00659 static int save_vidjpeg(const char *name, struct ImBuf *ibuf) 00660 { 00661 FILE * outfile; 00662 struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo; 00663 struct my_error_mgr jerr; 00664 00665 if ((outfile = fopen(name, "wb")) == NULL) return 0; 00666 jpeg_default_quality = 90; 00667 00668 cinfo->err = jpeg_std_error(&jerr.pub); 00669 jerr.pub.error_exit = jpeg_error; 00670 00671 /* Establish the setjmp return context for jpeg_error to use. */ 00672 if (setjmp(jerr.setjmp_buffer)) { 00673 /* If we get here, the JPEG code has signaled an error. 00674 * We need to clean up the JPEG object, close the input file, and return. 00675 */ 00676 jpeg_destroy_compress(cinfo); 00677 fclose(outfile); 00678 remove(name); 00679 return 0; 00680 } 00681 00682 init_jpeg(outfile, cinfo, ibuf); 00683 00684 /* adjust scaling factors */ 00685 if (cinfo->in_color_space == JCS_RGB) { 00686 cinfo->comp_info[0].h_samp_factor = 2; 00687 cinfo->comp_info[0].v_samp_factor = 1; 00688 } 00689 00690 write_jpeg(cinfo, ibuf); 00691 00692 fclose(outfile); 00693 jpeg_destroy_compress(cinfo); 00694 00695 return 1; 00696 } 00697 00698 static int save_jstjpeg(const char *name, struct ImBuf *ibuf) 00699 { 00700 char fieldname[1024]; 00701 struct ImBuf * tbuf; 00702 int oldy, returnval; 00703 00704 tbuf = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 24, IB_rect); 00705 tbuf->ftype = ibuf->ftype; 00706 tbuf->flags = ibuf->flags; 00707 00708 oldy = ibuf->y; 00709 ibuf->x *= 2; 00710 ibuf->y /= 2; 00711 00712 IMB_rectcpy(tbuf, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y); 00713 sprintf(fieldname, "%s.jf0", name); 00714 00715 returnval = save_vidjpeg(fieldname, tbuf) ; 00716 if (returnval == 1) { 00717 IMB_rectcpy(tbuf, ibuf, 0, 0, tbuf->x, 0, ibuf->x, ibuf->y); 00718 sprintf(fieldname, "%s.jf1", name); 00719 returnval = save_vidjpeg(fieldname, tbuf); 00720 } 00721 00722 ibuf->y = oldy; 00723 ibuf->x /= 2; 00724 IMB_freeImBuf(tbuf); 00725 00726 return returnval; 00727 } 00728 00729 static int save_maxjpeg(const char *name, struct ImBuf *ibuf) 00730 { 00731 FILE * outfile; 00732 struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo; 00733 struct my_error_mgr jerr; 00734 00735 if ((outfile = fopen(name, "wb")) == NULL) return 0; 00736 jpeg_default_quality = 100; 00737 00738 cinfo->err = jpeg_std_error(&jerr.pub); 00739 jerr.pub.error_exit = jpeg_error; 00740 00741 /* Establish the setjmp return context for jpeg_error to use. */ 00742 if (setjmp(jerr.setjmp_buffer)) { 00743 /* If we get here, the JPEG code has signaled an error. 00744 * We need to clean up the JPEG object, close the input file, and return. 00745 */ 00746 jpeg_destroy_compress(cinfo); 00747 fclose(outfile); 00748 remove(name); 00749 return 0; 00750 } 00751 00752 init_jpeg(outfile, cinfo, ibuf); 00753 00754 /* adjust scaling factors */ 00755 if (cinfo->in_color_space == JCS_RGB) { 00756 cinfo->comp_info[0].h_samp_factor = 1; 00757 cinfo->comp_info[0].v_samp_factor = 1; 00758 } 00759 00760 write_jpeg(cinfo, ibuf); 00761 00762 fclose(outfile); 00763 jpeg_destroy_compress(cinfo); 00764 00765 return 1; 00766 } 00767 00768 int imb_savejpeg(struct ImBuf *ibuf, const char *name, int flags) 00769 { 00770 00771 ibuf->flags = flags; 00772 if (IS_stdjpg(ibuf)) return save_stdjpeg(name, ibuf); 00773 if (IS_jstjpg(ibuf)) return save_jstjpeg(name, ibuf); 00774 if (IS_maxjpg(ibuf)) return save_maxjpeg(name, ibuf); 00775 return save_vidjpeg(name, ibuf); 00776 } 00777
1.7.3