00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <flgrCoreMalloc.h>
00025 #include <flgrCoreCopy.h>
00026 #include <flgrCoreChannel.h>
00027 #include <flgrCoreDataIO.h>
00028 #include <flgrCoreMalloc.h>
00029 #include <flgrCoreSystem.h>
00030 #include <flgrCoreVector.h>
00031 #include <flgrCoreDispatch.h>
00032
00033 #include "flgrImageIO.h"
00034
00035
00036 #define FLGR_IMAGE_IO_BMP_RW "BMP : Read only"
00037
00038 #define FLGR_IMAGE_IO_PGM_RW "PGM : Read/Write"
00039
00040 #define FLGR_IMAGE_IO_RAW_RW "RAW : Read/Write"
00041
00042 #define FLGR_IMAGE_IO_TEXT_RW "TEXT : Read/Write"
00043
00044
00045
00046 #ifdef LIB_PNG_SUPPORT
00047
00048 #define FLGR_IMAGE_IO_PNG_RW "PNG : Read/Write"
00049
00050 #include <png.h>
00051 #include <setjmp.h>
00052
00053 #else
00054 #define FLGR_IMAGE_IO_PNG_RW "PNG : No support"
00055 #endif
00056
00057
00058
00059
00060 #ifdef LIB_JPEG_SUPPORT
00061
00062 #define FLGR_IMAGE_IO_JPEG_RW "JPEG : Read/Write"
00063
00064 #include <jpeglib.h>
00065 #include <setjmp.h>
00066
00067 #else
00068 #define FLGR_IMAGE_IO_JPEG_RW "JPEG : No support"
00069 #endif
00070
00071
00072
00073
00074 #ifdef LIB_TIFF_SUPPORT
00075
00076 #define FLGR_IMAGE_IO_TIFF_RW "TIFF : Read only"
00077
00078 #include <tiffio.h>
00079
00080 #else
00081 #define FLGR_IMAGE_IO_TIFF_RW "TIFF : No support"
00082 #endif
00083
00084
00085 #define FLGR_IMAGE_IO_SUPPORT \
00086 FLGR_IMAGE_IO_PGM_RW \
00087 "\n" \
00088 FLGR_IMAGE_IO_PNG_RW \
00089 "\n" \
00090 FLGR_IMAGE_IO_BMP_RW \
00091 "\n" \
00092 FLGR_IMAGE_IO_JPEG_RW \
00093 "\n" \
00094 FLGR_IMAGE_IO_TIFF_RW \
00095 "\n" \
00096 FLGR_IMAGE_IO_RAW_RW \
00097 "\n" \
00098 FLGR_IMAGE_IO_TEXT_RW
00099
00100
00111 int flgr_check_extension(char *filename, char *extToCheck) {
00112
00113
00114 int len_ext = strlen(extToCheck);
00115 int len=strlen(filename)-len_ext;
00116 char *fname = &(filename[len]);
00117
00118 return (strcmp(fname,extToCheck)==0);
00119 }
00120
00121
00122 char *flgr_set_extension(char *filename, char *ext) {
00123
00124
00125 int len_ext = strlen(ext);
00126 int len = strlen(filename);
00127 char *fname = (char *) flgr_malloc(sizeof(char)*(len+1+len_ext));
00128
00129 strcpy(fname,filename);
00130 strcat(fname,ext);
00131
00132 return fname;
00133 }
00134
00136
00139
00140 char *flgr_get_supported_image_format(void) {
00141 return FLGR_IMAGE_IO_SUPPORT;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00153
00157
00158 FLGR_Data2D *flgr2d_load_png(char *filename) {
00159 #ifdef LIB_PNG_SUPPORT
00160
00161 FLGR_Data2D *img;
00162 png_uint_32 size_x, size_y;
00163 png_byte magic[8];
00164 png_structp png_ptr;
00165 png_infop info_ptr;
00166 png_infop end_info;
00167 png_bytep *row_pointers = NULL;
00168 int bit_depth, color_type;
00169 FILE *fp = NULL;
00170 int i;
00171 int img_spp;
00172 FLGR_Type img_type;
00173
00174
00175
00176
00177
00178 fp = fopen (filename, "rb");
00179 if (!fp) {
00180 POST_ERROR("couldn't open \"%s\"!\n",filename);
00181 return NULL;
00182 }
00183
00184
00185 fread (magic, 1, sizeof (magic), fp);
00186
00187
00188 if (!png_check_sig (magic, sizeof (magic))) {
00189
00190 fclose (fp);
00191 return NULL;
00192 }
00193
00194
00195 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00196 if (!png_ptr) {
00197 POST_ERROR("Could not allocate png structure\n");
00198 fclose (fp);
00199 return NULL;
00200 }
00201
00202
00203 info_ptr = png_create_info_struct(png_ptr);
00204 if (!info_ptr) {
00205 POST_ERROR("Could not create info structure\n");
00206 fclose (fp);
00207 png_destroy_read_struct (&png_ptr, NULL, NULL);
00208 return NULL;
00209 }
00210
00211 end_info = png_create_info_struct(png_ptr);
00212 if (!end_info) {
00213 POST_ERROR("Could not create end info structure\n");
00214 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)NULL);
00215 fclose (fp);
00216 return NULL;
00217 }
00218
00219
00220
00221 if (setjmp(png_jmpbuf(png_ptr))) {
00222 POST_ERROR("Error during init_io\n");
00223 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
00224 fclose (fp);
00225 return NULL;
00226 }
00227
00228
00229
00230 png_init_io (png_ptr, fp);
00231
00232
00233 png_set_sig_bytes (png_ptr, sizeof (magic));
00234
00235
00236 png_read_info (png_ptr, info_ptr);
00237
00238
00239 bit_depth = png_get_bit_depth (png_ptr, info_ptr);
00240 color_type = png_get_color_type (png_ptr, info_ptr);
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 if(color_type == PNG_COLOR_TYPE_PALETTE)
00251 png_set_palette_to_rgb(png_ptr);
00252
00253
00254 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
00255 png_set_gray_1_2_4_to_8(png_ptr);
00256
00257
00258 if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
00259 png_set_tRNS_to_alpha(png_ptr);
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 png_read_update_info (png_ptr, info_ptr);
00271
00272
00273 png_get_IHDR(png_ptr, info_ptr,
00274 (png_uint_32*)(&size_x),
00275 (png_uint_32*)(&size_y),
00276 &bit_depth, &color_type,
00277 NULL, NULL, NULL);
00278
00279
00280 switch(bit_depth) {
00281 case 8 : img_type=FLGR_UINT8; break;
00282 case 16 : img_type=FLGR_UINT16; break;
00283 default:
00284 POST_ERROR("\"%s\" dont have standard gray levels : 1,2,4,8,16!\n",filename);
00285 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
00286 fclose (fp);
00287 return NULL;
00288 }
00289
00290 switch(color_type) {
00291 case PNG_COLOR_TYPE_GRAY : img_spp=1; break;
00292 case PNG_COLOR_TYPE_RGB : img_spp=3; break;
00293 case PNG_COLOR_TYPE_RGB_ALPHA : img_spp=4; break;
00294 default:
00295 POST_ERROR("\"%s\" dont have standard gray levels : 1,2,4,8,16!\n",filename);
00296 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
00297 fclose (fp);
00298 return NULL;
00299 }
00300
00301 img = flgr2d_create_pixmap(size_y,size_x,img_spp,img_type);
00302
00303
00304 row_pointers = (png_bytep*) flgr_malloc(sizeof(png_bytep)*size_y);
00305
00306 for(i=0;i<size_y;i++) {
00307 row_pointers[i]=(png_bytep) img->array[i];
00308 }
00309
00310 png_read_image(png_ptr,row_pointers);
00311
00312 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
00313
00314 flgr_free(row_pointers);
00315
00316 fclose(fp);
00317
00318 return img;
00319 #else
00320
00321
00322 POST_ERROR("No support for png. you must recompile with -DLIB_PNG_SUPPORT flag!\n");
00323 return FLGR_RET_NOT_IMPLEMENTED;
00324 #endif
00325
00326 }
00328
00333
00334 FLGR_Ret flgr2d_save_png(FLGR_Data2D *img, char *filename) {
00335 #ifdef LIB_PNG_SUPPORT
00336
00337 FILE *fp = NULL;
00338 char *fname;
00339 png_structp png_ptr = NULL;
00340 png_infop info_ptr = NULL;
00341 int bitdepth, colortype;
00342 png_bytep *row_pointers = NULL;
00343 int i;
00344
00345
00346
00347
00348 if(img==NULL) {
00349 POST_ERROR("Null objects!\n");
00350 return FLGR_RET_NULL_OBJECT;
00351 }
00352
00353 if(!flgr_check_extension(filename,".png"))
00354 fname = flgr_set_extension(filename,".png");
00355 else
00356 fname = flgr_set_extension(filename,"");
00357
00358
00359
00360
00361
00362
00363
00364 if((fp=fopen(fname, "wb")) == NULL) {
00365 POST_ERROR("couldn't open \"%s\"!\n",fname);
00366 flgr_free(fname);
00367 return FLGR_RET_ALLOCATION_ERROR;
00368 }
00369
00370 png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00371 if (!png_ptr) {
00372 POST_ERROR("Could not create write structure\n");
00373 flgr_free(fname);
00374 fclose(fp);
00375 return FLGR_RET_UNDEFINED_ERROR;
00376 }
00377
00378 info_ptr = png_create_info_struct( png_ptr );
00379 if (!info_ptr) {
00380 POST_ERROR("Could not create info structure\n");
00381 flgr_free(fname);
00382 png_destroy_write_struct(&png_ptr, NULL);
00383 fclose(fp);
00384 return FLGR_RET_UNDEFINED_ERROR;
00385 }
00386
00387 if (setjmp(png_jmpbuf(png_ptr))) {
00388 POST_ERROR("Error during writing header\n");
00389 flgr_free(fname);
00390 png_destroy_write_struct(&png_ptr, &info_ptr);
00391 fclose(fp);
00392 return FLGR_RET_UNDEFINED_ERROR;
00393 }
00394
00395
00396 png_init_io( png_ptr, fp );
00397
00398 png_set_compression_level(png_ptr, Z_NO_COMPRESSION);
00399
00400
00401 switch(img->type) {
00402 case FLGR_UINT8: bitdepth = 8; break;
00403 case FLGR_UINT16: bitdepth = 16; break;
00404 case FLGR_UINT32: bitdepth = 32; break;
00405 case FLGR_INT8: bitdepth = 8; break;
00406 case FLGR_INT16: bitdepth = 16;break;
00407 case FLGR_INT32: bitdepth = 32;break;
00408 default:
00409 POST_ERROR("Unsupported image type\n");
00410 flgr_free(fname);
00411 png_destroy_write_struct(&png_ptr, &info_ptr);
00412 fclose(fp);
00413 return FLGR_RET_UNDEFINED_ERROR;
00414 }
00415
00416 switch(img->spp) {
00417 case 1: colortype = PNG_COLOR_TYPE_GRAY; break;
00418 case 3: colortype = PNG_COLOR_TYPE_RGB; break;
00419 case 4: colortype = PNG_COLOR_TYPE_RGB_ALPHA; break;
00420 default:
00421 POST_ERROR("Unsupported pixel sample format\n");
00422 flgr_free(fname);
00423 png_destroy_write_struct(&png_ptr, &info_ptr);
00424 fclose(fp);
00425 return FLGR_RET_UNDEFINED_ERROR;
00426 }
00427
00428
00429
00430 png_set_IHDR( png_ptr, info_ptr, img->size_x, img->size_y, bitdepth,
00431 colortype, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
00432 PNG_FILTER_TYPE_DEFAULT );
00433
00434
00435 if (setjmp(png_jmpbuf(png_ptr))) {
00436 POST_ERROR("Error during writing bytes\n");
00437 png_destroy_write_struct(&png_ptr, &info_ptr);
00438 flgr_free(fname);
00439 fclose(fp);
00440 return FLGR_RET_UNDEFINED_ERROR;
00441 }
00442
00443 png_write_info( png_ptr, info_ptr );
00444
00445 row_pointers = (png_bytep*) flgr_malloc(sizeof(png_bytep)*img->size_y);
00446
00447 for(i=0;i<img->size_y;i++) {
00448 row_pointers[i]=(png_bytep) img->array[i];
00449 }
00450
00451 png_write_image(png_ptr,row_pointers);
00452 png_write_end(png_ptr, NULL);
00453
00454 flgr_free(row_pointers);
00455 flgr_free(fname);
00456 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
00457 fclose(fp);
00458
00459 return FLGR_RET_OK;
00460
00461 #else
00462
00463
00464 POST_ERROR("No support for png. you must recompile with -DLIB_PNG_SUPPORT flag!\n");
00465 return FLGR_RET_NOT_IMPLEMENTED;
00466 #endif
00467
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 #ifdef LIB_JPEG_SUPPORT
00483
00484 struct my_error_mgr {
00485 struct jpeg_error_mgr pub;
00486 jmp_buf setjmp_buffer;
00487 };
00488
00489 typedef struct my_error_mgr *my_error_ptr;
00490
00491 METHODDEF(void) my_error_exit(j_common_ptr cinfo) {
00492
00493 my_error_ptr myerr = (my_error_ptr) cinfo->err;
00494
00495
00496
00497 (*cinfo->err->output_message) (cinfo);
00498
00499
00500 longjmp(myerr->setjmp_buffer, 1);
00501 }
00502
00503 #endif
00504
00505
00506
00508
00512
00513 FLGR_Data2D *flgr2d_load_jpeg(char *filename) {
00514 #ifdef LIB_JPEG_SUPPORT
00515
00516 struct jpeg_decompress_struct cinfo;
00517 struct my_error_mgr jerr;
00518
00519 unsigned char *imgbuffer;
00520 JSAMPROW buffer[1];
00521 int row_stride;
00522
00523 FLGR_Data2D *img = NULL;
00524 FILE *infile = NULL;
00525
00526
00527
00528
00529
00530 infile = fopen (filename, "rb");
00531 if (!infile) {
00532 POST_ERROR("couldn't open \"%s\"!\n",filename);
00533 return NULL;
00534 }
00535
00536
00537
00538
00539 cinfo.err = jpeg_std_error(&jerr.pub);
00540 jerr.pub.error_exit = my_error_exit;
00541
00542 if (setjmp(jerr.setjmp_buffer)) {
00543
00544
00545
00546 POST_ERROR("Decompress Error\n");
00547 jpeg_destroy_decompress(&cinfo);
00548 fclose(infile);
00549 return NULL;
00550 }
00551
00552 jpeg_create_decompress(&cinfo);
00553 jpeg_stdio_src(&cinfo, infile);
00554 jpeg_read_header(&cinfo, TRUE);
00555 jpeg_start_decompress(&cinfo);
00556
00557 row_stride = cinfo.output_width * cinfo.output_components;
00558
00559 imgbuffer = flgr_malloc(row_stride * cinfo.output_height);
00560 buffer[0] = imgbuffer;
00561
00562 while (cinfo.output_scanline < cinfo.output_height) {
00563 jpeg_read_scanlines(&cinfo, buffer, 1);
00564 buffer[0] += row_stride;
00565 }
00566
00567 jpeg_finish_decompress(&cinfo);
00568 jpeg_destroy_decompress(&cinfo);
00569
00570
00571 if((cinfo.output_components == 1) && (cinfo.out_color_space == JCS_GRAYSCALE)) {
00572 img = flgr2d_create_pixmap(cinfo.output_height, cinfo.output_width, 1, FLGR_UINT8);
00573 flgr2d_import_raw_ptr(img,imgbuffer);
00574
00575 }else if ((cinfo.output_components == 3) && (cinfo.out_color_space == JCS_RGB)) {
00576 img = flgr2d_create_pixmap(cinfo.output_height, cinfo.output_width, 3, FLGR_UINT8);
00577 flgr2d_import_raw_ptr(img,imgbuffer);
00578
00579 } else {
00580 POST_ERROR("Support only 8 bits gray and 24 bits RGB format\n");
00581 }
00582
00583 fclose(infile);
00584 flgr_free(imgbuffer);
00585 return img;
00586
00587 #else
00588
00589
00590 POST_ERROR("No support for jpeg. you must recompile with -DLIB_JPEG_SUPPORT flag!\n");
00591 return NULL;
00592 #endif
00593 }
00594
00595
00596
00598
00604
00605 FLGR_Ret flgr2d_save_jpeg(FLGR_Data2D *img, char *filename, int quality) {
00606 #ifdef LIB_JPEG_SUPPORT
00607 struct jpeg_compress_struct cinfo;
00608 struct my_error_mgr jerr;
00609
00610
00611 FILE * outfile;
00612 char *fname;
00613 FLGR_Data1D *signal;
00614 int row_stride;
00615
00616
00617
00618 if(img->type != FLGR_UINT8) return FLGR_RET_NOT_IMPLEMENTED;
00619
00620
00621
00622
00623 cinfo.err = jpeg_std_error(&jerr.pub);
00624 jerr.pub.error_exit = my_error_exit;
00625
00626 if (setjmp(jerr.setjmp_buffer)) {
00627
00628
00629
00630 POST_ERROR("Decompress Error\n");
00631 jpeg_destroy_compress(&cinfo);
00632 return FLGR_RET_UNDEFINED_ERROR;
00633 }
00634
00635
00636 jpeg_create_compress(&cinfo);
00637
00638
00639 if(!flgr_check_extension(filename,".jpeg"))
00640 fname = flgr_set_extension(filename,".jpeg");
00641 else
00642 fname = flgr_set_extension(filename,"");
00643
00644 if ((outfile = fopen(fname, "wb")) == NULL) {
00645 POST_ERROR("couldn't open \"%s\"!\n",fname);
00646 jpeg_destroy_compress(&cinfo);
00647 flgr_free(fname);
00648 return FLGR_RET_ALLOCATION_ERROR;
00649 }
00650 jpeg_stdio_dest(&cinfo, outfile);
00651
00652 cinfo.image_width = img->size_x;
00653 cinfo.image_height = img->size_y;
00654
00655 signal = (FLGR_Data1D *) img->row;
00656
00657 switch(img->spp) {
00658 case 1: cinfo.in_color_space = JCS_GRAYSCALE; break;
00659 case 3: cinfo.in_color_space = JCS_RGB; break;
00660 default:
00661 POST_ERROR("Support only 8 bits gray scale and 24 Bits RGB images\n");
00662 jpeg_destroy_compress(&cinfo);
00663 flgr_free(fname);
00664 fclose(outfile);
00665 return FLGR_RET_NOT_IMPLEMENTED;
00666 }
00667
00668 cinfo.input_components = img->spp;
00669 jpeg_set_defaults(&cinfo);
00670 jpeg_set_quality(&cinfo, quality, TRUE);
00671 jpeg_start_compress(&cinfo, TRUE);
00672 row_stride = img->spp*img->size_x;
00673 jpeg_write_scanlines(&cinfo, (JSAMPLE **) img->array, cinfo.image_height);
00674
00675 jpeg_finish_compress(&cinfo);
00676
00677 fclose(outfile);
00678
00679 jpeg_destroy_compress(&cinfo);
00680
00681
00682 return FLGR_RET_OK;
00683 #else
00684
00685
00686 POST_ERROR("No support for jpeg. you must recompile with -DLIB_JPEG_SUPPORT flag!\n");
00687 return FLGR_RET_NOT_IMPLEMENTED;
00688 #endif
00689 }
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00703
00707
00708 char *flgr2d_load_tiff_info(char *filename) {
00709 #ifdef LIB_TIFF_SUPPORT
00710 TIFF *image;
00711 fgUINT16 photo=0, bps=0, spp=0,compression=0, fillorder=FILLORDER_MSB2LSB;
00712 fgUINT16 orientation=0,planarconfig=0;
00713 fgUINT32 width=0,height=0;
00714 char *documentname, *description,*make,*model;
00715 int fileBigEndian;
00716
00717 char info_orientation[128];
00718 char info_width[128];
00719 char info_height[128];
00720 char info_bps[128];
00721 char info_spp[128];
00722 char info_photo[128];
00723 char info_planarconfig[128];
00724 char info_fillorder[128];
00725 char info_compression[128];
00726 char info_bigendian[128];
00727 char info_hardendian[128];
00728 char info_documentname[1024];
00729 char info_description[1024];
00730 char info_make[1024];
00731 char info_model[1024];
00732
00733 char *info;
00734
00735
00736
00737
00738 if((image = TIFFOpen(filename, "r")) == NULL){
00739 POST_ERROR("Could not open %s\n",filename);
00740 return NULL;
00741 }
00742
00743 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
00744 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
00745 TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps);
00746 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
00747 TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo);
00748 TIFFGetField(image, TIFFTAG_FILLORDER, &fillorder);
00749 TIFFGetField(image, TIFFTAG_COMPRESSION, &compression);
00750 TIFFGetField(image, TIFFTAG_DOCUMENTNAME, &documentname);
00751 TIFFGetField(image, TIFFTAG_IMAGEDESCRIPTION, &description);
00752 TIFFGetField(image, TIFFTAG_MAKE, &make);
00753 TIFFGetField(image, TIFFTAG_MODEL, &model);
00754 TIFFGetField(image, TIFFTAG_ORIENTATION, &orientation);
00755 TIFFGetField(image, TIFFTAG_PLANARCONFIG, &planarconfig);
00756 fileBigEndian = TIFFIsBigEndian(image);
00757
00758 sprintf(info_width,"# Tiff Width : %d\n",width);
00759 sprintf(info_height,"# Tiff Height : %d\n",height);
00760 sprintf(info_bps,"# Tiff Bit Per Sample : %d\n",bps);
00761 sprintf(info_spp,"# Tiff Sample Per Pixel : %d\n",spp);
00762 sprintf(info_documentname,"# Tiff Document name : %s\n",documentname);
00763 sprintf(info_description,"# Tiff Description : %s\n",description);
00764 sprintf(info_make,"# Tiff Scanner manufacturer name : %s\n",make);
00765 sprintf(info_model,"# Tiff Scanner model name/number : %s\n",model);
00766
00767 switch(planarconfig) {
00768 case PLANARCONFIG_CONTIG:
00769 strcpy(info_planarconfig,"# Tiff Planar Configuration : single image plane\n");
00770 break;
00771 case PLANARCONFIG_SEPARATE:
00772 strcpy(info_planarconfig,"# Tiff Planar Configuration : separate planes of data\n");
00773 break;
00774 default:
00775 strcpy(info_planarconfig,"# Tiff Planar Configuration : **unknown**\n");
00776 }
00777
00778 switch(orientation) {
00779 case ORIENTATION_TOPLEFT:
00780 strcpy(info_orientation,"# Tiff Orientation : Top Left\n");
00781 break;
00782 case ORIENTATION_TOPRIGHT:
00783 strcpy(info_orientation,"# Tiff Orientation : Top Right\n");
00784 break;
00785 case ORIENTATION_BOTRIGHT:
00786 strcpy(info_orientation,"# Tiff Orientation : Bottom Right\n");
00787 break;
00788 case ORIENTATION_BOTLEFT:
00789 strcpy(info_orientation,"# Tiff Orientation : Bottom Left\n");
00790 break;
00791 case ORIENTATION_LEFTTOP:
00792 strcpy(info_orientation,"# Tiff Orientation : Left Top\n");
00793 break;
00794 case ORIENTATION_RIGHTTOP:
00795 strcpy(info_orientation,"# Tiff Orientation : Right Top\n");
00796 break;
00797 case ORIENTATION_RIGHTBOT:
00798 strcpy(info_orientation,"# Tiff Orientation : Right Bottom\n");
00799 break;
00800 case ORIENTATION_LEFTBOT:
00801 strcpy(info_orientation,"# Tiff Orientation : Left Bottom\n");
00802 break;
00803 default:
00804 strcpy(info_orientation,"# Tiff Orientation : **unknown**\n");
00805 }
00806
00807 switch(photo) {
00808 case PHOTOMETRIC_MINISWHITE:
00809 strcpy(info_photo,"# Tiff Photometric : min value is white\n");
00810 break;
00811 case PHOTOMETRIC_MINISBLACK:
00812 strcpy(info_photo,"# Tiff Photometric : min value is black\n");
00813 break;
00814 case PHOTOMETRIC_RGB:
00815 strcpy(info_photo,"# Tiff Photometric : RGB color model\n");
00816 break;
00817 case PHOTOMETRIC_PALETTE:
00818 strcpy(info_photo,"# Tiff Photometric : color map indexed\n");
00819 break;
00820 case PHOTOMETRIC_MASK:
00821 strcpy(info_photo,"# Tiff Photometric : $holdout mask\n");
00822 break;
00823 case PHOTOMETRIC_SEPARATED:
00824 strcpy(info_photo,"# Tiff Photometric : !color separations\n");
00825 break;
00826 case PHOTOMETRIC_YCBCR:
00827 strcpy(info_photo,"# Tiff Photometric : !CCIR 601\n");
00828 break;
00829 case PHOTOMETRIC_CIELAB:
00830 strcpy(info_photo,"# Tiff Photometric : !1976 CIE L*a*b*\n");
00831 break;
00832 case PHOTOMETRIC_ICCLAB:
00833 strcpy(info_photo,"# Tiff Photometric : ICC L*a*b* [Adobe TIFF Technote 4]\n");
00834 break;
00835 case PHOTOMETRIC_ITULAB:
00836 strcpy(info_photo,"# Tiff Photometric : ITU L*a*b*\n");
00837 break;
00838 case PHOTOMETRIC_LOGL:
00839 strcpy(info_photo,"# Tiff Photometric : CIE Log2(L)\n");
00840 break;
00841 case PHOTOMETRIC_LOGLUV:
00842 strcpy(info_photo,"# Tiff Photometric : CIE Log2(L) (u',v')\n");
00843 break;
00844 default:
00845 strcpy(info_photo,"# Tiff Photometric : **unknown**\n");
00846 }
00847
00848 switch(compression) {
00849 case COMPRESSION_NONE:
00850 strcpy(info_compression,"# Tiff Compression : dump mode\n");
00851 break;
00852 case COMPRESSION_CCITTRLE:
00853 strcpy(info_compression,"# Tiff Compression : CCITT modified Huffman RLE\n");
00854 break;
00855 case COMPRESSION_CCITTFAX3:
00856 strcpy(info_compression,"# Tiff Compression : CCITT Group 3 fax encoding\n");
00857 break;
00858 case COMPRESSION_CCITTFAX4:
00859 strcpy(info_compression,"# Tiff Compression : CCITT Group 4 fax encoding\n");
00860 break;
00861 case COMPRESSION_LZW:
00862 strcpy(info_compression,"# Tiff Compression : Lempel-Ziv & Welch\n");
00863 break;
00864 case COMPRESSION_OJPEG:
00865 strcpy(info_compression,"# Tiff Compression : !6.0 JPEG\n");
00866 break;
00867 case COMPRESSION_JPEG:
00868 strcpy(info_compression,"# Tiff Compression : %JPEG DCT compression\n");
00869 break;
00870 case COMPRESSION_NEXT:
00871 strcpy(info_compression,"# Tiff Compression : NeXT 2-bit RLE\n");
00872 break;
00873 case COMPRESSION_CCITTRLEW:
00874 strcpy(info_compression,"# Tiff Compression : #1 w/ word alignment\n");
00875 break;
00876 case COMPRESSION_PACKBITS:
00877 strcpy(info_compression,"# Tiff Compression : Macintosh RLE\n");
00878 break;
00879 case COMPRESSION_THUNDERSCAN:
00880 strcpy(info_compression,"# Tiff Compression : ThunderScan RLE\n");
00881 break;
00882 case COMPRESSION_IT8CTPAD:
00883 strcpy(info_compression,"# Tiff Compression : IT8 CT w/padding\n");
00884 break;
00885 case COMPRESSION_IT8LW:
00886 strcpy(info_compression,"# Tiff Compression : IT8 Linework RLE\n");
00887 break;
00888 case COMPRESSION_IT8MP:
00889 strcpy(info_compression,"# Tiff Compression : IT8 Monochrome picture\n");
00890 break;
00891 case COMPRESSION_IT8BL:
00892 strcpy(info_compression,"# Tiff Compression : IT8 Binary line art\n");
00893 break;
00894 case COMPRESSION_PIXARFILM:
00895 strcpy(info_compression,"# Tiff Compression : Pixar companded 10bit LZW\n");
00896 break;
00897 case COMPRESSION_PIXARLOG:
00898 strcpy(info_compression,"# Tiff Compression : Pixar companded 11bit ZIP\n");
00899 break;
00900 case COMPRESSION_DEFLATE:
00901 strcpy(info_compression,"# Tiff Compression : Deflate compression\n");
00902 break;
00903 case COMPRESSION_ADOBE_DEFLATE:
00904 strcpy(info_compression,"# Tiff Compression : Deflate compression Adobe\n");
00905 break;
00906 case COMPRESSION_DCS:
00907 strcpy(info_compression,"# Tiff Compression : Kodak DCS encoding\n");
00908 break;
00909 case COMPRESSION_JBIG:
00910 strcpy(info_compression,"# Tiff Compression : ISO JBIG\n");
00911 break;
00912 case COMPRESSION_SGILOG:
00913 strcpy(info_compression,"# Tiff Compression : SGI Log Luminance RLE\n");
00914 break;
00915 case COMPRESSION_SGILOG24:
00916 strcpy(info_compression,"# Tiff Compression : SGI Log 24-bit packed\n");
00917 break;
00918 case COMPRESSION_JP2000:
00919 strcpy(info_compression,"# Tiff Compression : Leadtools JPEG2000\n");
00920 break;
00921 default:
00922 strcpy(info_compression,"# Tiff Compression : **unknown**\n");
00923 }
00924
00925 switch(fillorder) {
00926 case FILLORDER_MSB2LSB: strcpy(info_fillorder,"# Tiff Fill Order : MSB downto LSB\n"); break;
00927 default: strcpy(info_fillorder,"# Tiff Fill Order : LSB to MSB\n");
00928 }
00929
00930 if(fileBigEndian) {
00931 strcpy(info_bigendian,"# Tiff Data : Big Endian\n");
00932 }else{
00933 strcpy(info_bigendian,"# Tiff Data : Little Endian\n");
00934 }
00935
00936 if(flgr_system_is_little_endian()) {
00937 strcpy(info_hardendian,"# System : Little Endian Hardware\n");
00938 }else{
00939 strcpy(info_hardendian,"# System : Big Endian Hardware\n");
00940 }
00941
00942 info = flgr_malloc((strlen(info_hardendian)+strlen(info_bigendian)+strlen(info_fillorder)+
00943 strlen(info_compression)+strlen(info_photo)+strlen(info_spp)+strlen(info_bps)+
00944 strlen(info_height)+strlen(info_width)+strlen(info_documentname)+
00945 strlen(info_description)+strlen(info_make)+strlen(info_model)+
00946 strlen(info_orientation)+strlen(info_planarconfig)+1)*sizeof(char));
00947
00948 strcpy(info,info_documentname);
00949 strcat(info,info_description);
00950 strcat(info,info_make);
00951 strcat(info,info_model);
00952 strcat(info,info_orientation);
00953 strcat(info,info_planarconfig);
00954 strcat(info,info_width);
00955 strcat(info,info_height);
00956 strcat(info,info_bps);
00957 strcat(info,info_spp);
00958 strcat(info,info_photo);
00959 strcat(info,info_compression);
00960 strcat(info,info_fillorder);
00961 strcat(info,info_bigendian);
00962 strcat(info,info_hardendian);
00963
00964 TIFFClose(image);
00965
00966 return info;
00967
00968 #else
00969
00970
00971 POST_ERROR("No support for tiff. you must recompile with -DLIB_TIFF_SUPPORT flag!\n");
00972 return NULL;
00973 #endif
00974 }
00975
00976
00978
00982
00983 FLGR_Data2D *flgr2d_load_tiff(char *filename) {
00984 #ifdef LIB_TIFF_SUPPORT
00985 TIFF *image;
00986
00987 int fileBigEndian;
00988 fgUINT16 photo=0, bps=0, spp=0, fillorder=FILLORDER_MSB2LSB;
00989 fgUINT32 width=0,height=0;
00990 FLGR_Data2D *img = NULL;
00991
00992
00993
00994
00995 if((image = TIFFOpen(filename, "r")) == NULL){
00996 POST_ERROR("Could not open %s\n",filename);
00997 return NULL;
00998 }
00999
01000 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
01001 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
01002 TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps);
01003 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
01004 TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo);
01005 TIFFGetField(image, TIFFTAG_FILLORDER, &fillorder);
01006
01007 fileBigEndian = TIFFIsBigEndian(image);
01008
01009
01010 if( (spp==3) || (spp==4) ) {
01011 if( bps <= 8 || (bps<=16 && photo==PHOTOMETRIC_RGB) ) {
01012 fgUINT32 *raster = flgr_malloc(width*height*4);
01013
01014 img = flgr2d_create_pixmap(height,width,4,FLGR_UINT8);
01015
01016 TIFFReadRGBAImage(image, width, height, raster, ORIENTATION_TOPLEFT);
01017
01018 flgr2d_import_raw_ptr(img,raster);
01019 flgr2d_mirror_vertical_hmorph(img);
01020
01021 flgr_free(raster);
01022
01023 }else if (( bps <= 16 ) && (photo==PHOTOMETRIC_CIELAB)) {
01024 float *whitePoint;
01025 float refWhite[3];
01026 TIFFCIELabToRGB* cielab = 0;
01027 FLGR_Vector *vec = flgr_vector_create(3,FLGR_UINT8);
01028 int i,j;
01029 char *scanlineBuffer;
01030 TIFFDisplay display_sRGB = {
01031 {
01032 { 3.2410F, -1.5374F, -0.4986F },
01033 { -0.9692F, 1.8760F, 0.0416F },
01034 { 0.0556F, -0.2040F, 1.0570F }
01035 },
01036 100.0F, 100.0F, 100.0F,
01037 255, 255, 255,
01038 1.0F, 1.0F, 1.0F,
01039 2.4F, 2.4F, 2.4F,
01040 };
01041
01042
01043
01044
01045 cielab = (TIFFCIELabToRGB *) flgr_malloc(sizeof(TIFFCIELabToRGB));
01046
01047 if (!cielab) {
01048 POST_ERROR("No space for CIE L*a*b*->RGB conversion state\n");
01049 TIFFClose(image);
01050 return NULL;
01051 }
01052
01053 TIFFGetFieldDefaulted(image, TIFFTAG_WHITEPOINT, &whitePoint);
01054 refWhite[1] = 100.0F;
01055 refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
01056 refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];
01057
01058 if (TIFFCIELabToRGBInit(cielab, &display_sRGB, refWhite) < 0) {
01059 POST_ERROR("Failed to initialize CIE L*a*b*->RGB conversion state\n");
01060 flgr_free(cielab);
01061 TIFFClose(image);
01062 return NULL;
01063 }
01064
01065
01066 img = flgr2d_create_pixmap(height,width,3,FLGR_UINT8);
01067
01068 scanlineBuffer = flgr_malloc(TIFFScanlineSize(image));
01069
01070 for(i=0 ; i<height ; i++) {
01071 fgINT16* p;
01072
01073 TIFFReadScanline(image, scanlineBuffer, i,0);
01074 p = (fgINT16*)scanlineBuffer;
01075
01076 for(j=0 ; j<width ; j++,p+=spp) {
01077 fgFLOAT32 X, Y, Z;
01078 fgUINT32 r, g, b;
01079
01080 TIFFCIELabToXYZ(cielab, ((fgUINT16) (p[0]))>>8, p[1]>>8, p[2]>>8, &X, &Y, &Z);
01081 TIFFXYZToRGB(cielab, X, Y, Z, &r, &g, &b);
01082
01083 flgr_vector_set_element_fgUINT8(vec,0,(fgUINT8) r);
01084 flgr_vector_set_element_fgUINT8(vec,1,(fgUINT8) g);
01085 flgr_vector_set_element_fgUINT8(vec,2,(fgUINT8) b);
01086
01087 flgr2d_set_data_vector_fgUINT8(img,i,j,vec);
01088
01089 }
01090 }
01091
01092
01093
01094 flgr_free(scanlineBuffer);
01095 flgr_free(cielab);
01096 flgr_vector_destroy(vec);
01097 }
01098 }
01099
01100 TIFFClose(image);
01101 return img;
01102
01103 #else
01104
01105
01106 POST_ERROR("No support for tiff. you must recompile with -DLIB_TIFF_SUPPORT flag!\n");
01107 return NULL;
01108 #endif
01109 }
01110
01111
01112
01114
01120
01121 FLGR_Ret flgr2d_save_tiff(FLGR_Data2D *img, char *filename, int quality) {
01122 #ifdef LIB_TIFF_SUPPORT
01123
01124
01125 POST_ERROR("Not supported yet\n");
01126
01127 return FLGR_RET_NOT_IMPLEMENTED;
01128
01129 return FLGR_RET_OK;
01130 #else
01131
01132
01133 POST_ERROR("No support for tiff. you must recompile with -DLIB_TIFF_SUPPORT flag!\n");
01134 return FLGR_RET_NOT_IMPLEMENTED;
01135 #endif
01136 }
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01149
01161
01162 FLGR_Data2D *flgr2d_load_raw_desc(FILE *f, int byte_offset, int size_y, int size_x, int spp, FLGR_Type type) {
01163 FLGR_Data2D *img=NULL;
01164 FLGR_Data1D *img_1d_buf=NULL;
01165 int read_size, size;
01166
01167
01168
01169 fseek(f,byte_offset,SEEK_CUR);
01170
01171 if((type==FLGR_UINT64) || (type==FLGR_INT64)) {
01172 POST_ERROR("Unsupported raw type\n");
01173 return NULL;
01174 }else if(type==FLGR_BIT) {
01175 read_size = ((((size_x*size_y*spp)>>3)+1)<<3)>>3;
01176 size = 1;
01177 }else {
01178 read_size = size_x*size_y;
01179 size = spp;
01180 }
01181
01182 img_1d_buf = flgr1d_create_signal(size_x*size_y,spp,type);
01183 img = flgr2d_create_pixmap(size_y,size_x,spp,type);
01184
01185 fread(img_1d_buf->array, size, read_size, f);
01186
01187 flgr1d_copy_to_2d(img, img_1d_buf);
01188
01189 flgr1d_destroy(img_1d_buf);
01190
01191 return img;
01192 }
01193
01194
01195
01197
01207
01208 FLGR_Data2D *flgr2d_load_raw_file(char *filename, int byte_offset, int size_y, int size_x, int spp, FLGR_Type type) {
01209 FILE *f;
01210 FLGR_Data2D *img;
01211
01212
01213
01214
01215 if( (f=fopen(filename,"rb"))==NULL) {
01216 POST_ERROR("couldn't open \"%s\"!\n",filename);
01217 return NULL;
01218 }
01219
01220 img = flgr2d_load_raw_desc(f, byte_offset, size_y, size_x, spp, type);
01221
01222 fclose(f);
01223
01224 return img;
01225
01226 }
01227
01228
01229
01230
01231
01233
01239
01240 FLGR_Ret flgr2d_save_raw_desc(FLGR_Data2D *dat, FILE *f) {
01241 FLGR_Data1D *img_1d_buf=NULL;
01242 int read_size, size;
01243
01244
01245
01246 if(dat->type==FLGR_BIT) {
01247 read_size = ((((dat->size_x*dat->size_y*dat->spp)>>3)+1)<<3)>>3;
01248 size = 1;
01249 }else {
01250 read_size = dat->size_x*dat->size_y;
01251 size = dat->spp;
01252 }
01253
01254 img_1d_buf = flgr1d_create_signal(dat->size_x*dat->size_y,dat->spp,dat->type);
01255 flgr2d_copy_to_1d(img_1d_buf, dat);
01256
01257 fwrite(img_1d_buf->array, size, read_size, f);
01258
01259 flgr1d_destroy(img_1d_buf);
01260
01261 return FLGR_RET_OK;
01262 }
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01275
01286
01287 FLGR_Data2D *flgr2d_load_text_desc(FILE *f, int size_y, int size_x, int spp, FLGR_Type type) {
01288 FLGR_Data2D *img=NULL;
01289 FLGR_Vector *vec;
01290 int i,j,k;
01291
01292
01293
01294
01295
01296
01297 if((type==FLGR_UINT64) || (type==FLGR_INT64)) {
01298 POST_ERROR("Unsupported file type\n");
01299 return NULL;
01300 }
01301
01302 vec = flgr_vector_create(spp,type);
01303
01304 img = flgr2d_create_pixmap(size_y,size_x,spp,type);
01305
01306 for(i=0 ; i<size_y ; i++) {
01307 for(j=0 ; j<size_x ; j++) {
01308
01309 for(k=0 ; k<spp ; k++) {
01310 if(type == FLGR_BIT) {
01311 fgUINT32 val;
01312 fscanf(f,"%u",&val); flgr_vector_set_element_fgUINT32(vec, k, ((fgBIT) val)&1);
01313 }else if(type == FLGR_UINT8) {
01314 fgUINT32 val;
01315 fscanf(f,"%u",&val); flgr_vector_set_element_fgUINT32(vec, k, (fgUINT8) val);
01316 }else if(type == FLGR_UINT16) {
01317 fgUINT32 val;
01318 fscanf(f,"%u",&val); flgr_vector_set_element_fgUINT32(vec, k, (fgUINT16) val);
01319 }else if(type == FLGR_UINT32) {
01320 fgUINT32 val;
01321 fscanf(f,"%u",&val); flgr_vector_set_element_fgUINT32(vec, k, val);
01322 }else if(type == FLGR_INT8) {
01323 fgINT32 val;
01324 fscanf(f,"%d",&val); flgr_vector_set_element_fgINT8(vec, k, (fgINT8) val);
01325 }else if(type == FLGR_INT16) {
01326 fgINT32 val;
01327 fscanf(f,"%d",&val); flgr_vector_set_element_fgINT16(vec, k, (fgINT16) val);
01328 }else if(type == FLGR_INT32) {
01329 fgINT32 val;
01330 fscanf(f,"%d",&val); flgr_vector_set_element_fgINT32(vec, k, val);
01331 }else if(type == FLGR_FLOAT32) {
01332 fgFLOAT32 val;
01333 fscanf(f,"%g",&val); flgr_vector_set_element_fgFLOAT32(vec, k, val);
01334 }else {
01335 fgFLOAT64 val;
01336 fscanf(f,"%lg",&val); flgr_vector_set_element_fgFLOAT64(vec, k, val);
01337 }
01338 }
01339
01340 flgr2d_set_data_vector(img,i,j,vec);
01341
01342 }
01343 }
01344
01345 flgr_vector_destroy(vec);
01346
01347 return img;
01348 }
01349
01350
01351
01353
01362
01363 FLGR_Data2D *flgr2d_load_text_file(char *filename, int size_y, int size_x, int spp, FLGR_Type type) {
01364 FILE *f;
01365 FLGR_Data2D *img;
01366
01367
01368
01369
01370 if( (f=fopen(filename,"rt"))==NULL) {
01371 POST_ERROR("couldn't open \"%s\"!\n",filename);
01372 return NULL;
01373 }
01374
01375 img = flgr2d_load_text_desc(f, size_y, size_x, spp, type);
01376
01377 fclose(f);
01378
01379 return img;
01380
01381 }
01382
01383
01384
01385
01386
01388
01394
01395 FLGR_Ret flgr2d_save_text_desc(FLGR_Data2D *img, FILE *f) {
01396 FLGR_Vector *vec;
01397 FLGR_Type type = img->type;
01398 int spp = img->spp;
01399 int size_x = img->size_x;
01400 int size_y = img->size_y;
01401 int i,j,k;
01402
01403
01404
01405
01406
01407 vec = flgr_vector_create(spp,type);
01408
01409
01410 for(i=0 ; i<size_y ; i++) {
01411 for(j=0 ; j<size_x ; j++) {
01412 flgr2d_get_data_vector(img,i,j,vec);
01413
01414 for(k=0 ; k<spp ; k++) {
01415 if(type == FLGR_BIT) {
01416 fgBIT val;
01417 flgr_vector_get_element_fgBIT(vec, k, &val );
01418 fprintf(f,"%u\n",(unsigned int) val);
01419
01420 }else if(type == FLGR_UINT8) {
01421 fgUINT8 val;
01422 flgr_vector_get_element_fgUINT8(vec, k, &val );
01423 fprintf(f,"%u\n",(unsigned int) val);
01424
01425 }else if(type == FLGR_UINT16) {
01426 fgUINT16 val;
01427 flgr_vector_get_element_fgUINT16(vec, k, &val);
01428 fprintf(f,"%u\n",(unsigned int) val);
01429
01430 }else if(type == FLGR_UINT32) {
01431 fgUINT32 val;
01432 flgr_vector_get_element_fgUINT32(vec, k, &val);
01433 fprintf(f,"%u\n",val);
01434
01435 }else if(type == FLGR_INT8) {
01436 fgINT8 val;
01437 flgr_vector_get_element_fgINT8(vec, k, &val);
01438 fprintf(f,"%d\n",(int) val);
01439
01440 }else if(type == FLGR_INT16) {
01441 fgINT16 val;
01442 flgr_vector_get_element_fgINT16(vec, k, &val);
01443 fprintf(f,"%d\n",(int) val);
01444
01445 }else if(type == FLGR_INT32) {
01446 fgINT32 val;
01447 flgr_vector_get_element_fgINT32(vec, k, &val);
01448 fprintf(f,"%d\n",val);
01449
01450 }else if(type == FLGR_FLOAT32) {
01451 fgFLOAT32 val;
01452 flgr_vector_get_element_fgFLOAT32(vec, k, &val);
01453 fprintf(f,"%g\n",val);
01454
01455 }else if(type == FLGR_FLOAT64) {
01456 fgFLOAT64 val;
01457 flgr_vector_get_element_fgFLOAT64(vec, k, &val);
01458 fprintf(f,"%lg\n",val);
01459
01460 }
01461 }
01462
01463 }
01464 }
01465
01466 flgr_vector_destroy(vec);
01467
01468 return FLGR_RET_OK;
01469 }
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01486
01490
01491 FLGR_Data2D *flgr2d_load_pgm(char *filename) {
01492 FLGR_Data2D *img=NULL;
01493 FILE *f;
01494 fpos_t fposition;
01495 char buff[16];
01496 int size_x,size_y;
01497 int color_range;
01498 int car;
01499 unsigned char stop;
01500 int isP2=0;
01501
01502
01503
01504
01505 if( (f=fopen(filename,"rb"))==NULL) {
01506 POST_ERROR("couldn't open \"%s\"!\n",filename);
01507 return NULL;
01508 }
01509
01510
01511 fscanf(f,"%s\n",buff);
01512 if(strcmp(buff,"P5") == 0) {
01513 isP2 = 0;
01514 }else if(strcmp(buff,"P2") == 0) {
01515 isP2 = 1;
01516 }else{
01517 fclose(f);
01518 POST_ERROR("\"%s\" is not a valid pgm (P2 or P5) !\n",filename);
01519 return NULL;
01520 }
01521
01522
01523 stop=0;
01524 do {
01525 fgetpos(f, &fposition);
01526 car=fgetc(f);
01527 if(car=='#') {
01528 while(fgetc(f)!='\n');
01529 stop=0;
01530 }else {
01531 stop=1;
01532 }
01533 }while(stop==0);
01534 fseek(f,-2,SEEK_CUR);
01535
01536
01537
01538 fscanf(f,"%d %d\n", &size_x, &size_y);
01539
01540 fscanf(f,"%d\n",&color_range);
01541
01542 if(color_range==255) {
01543 if(isP2==1)
01544 img = flgr2d_load_text_desc(f, size_y, size_x, 1, FLGR_UINT8);
01545 else
01546 img = flgr2d_load_raw_desc(f, 0, size_y, size_x, 1, FLGR_UINT8);
01547
01548 }else if(color_range==65535) {
01549 if(isP2==1)
01550 img = flgr2d_load_text_desc(f, size_y, size_x, 1, FLGR_UINT16);
01551 else
01552 img = flgr2d_load_raw_desc(f, 0, size_y, size_x, 1, FLGR_UINT16);
01553
01554 }else {
01555 POST_ERROR("\"%s\" is not a valid pgm (P2 or P5). Load only brut 8 and 16 bits pgm file !\n",filename);
01556 }
01557
01558
01559 fclose(f);
01560
01561 return img;
01562 }
01563
01564
01565
01567
01573
01574 FLGR_Ret flgr2d_save_pgm(FLGR_Data2D *img, char *filename, int format) {
01575 FILE *f;
01576 char *fname;
01577 int colorWidth = 0;
01578
01579
01580
01581 if((format != 2) && (format != 5))
01582 return FLGR_RET_NOT_IMPLEMENTED;
01583
01584
01585 if(img==NULL) {
01586 POST_ERROR("Null objects!\n");
01587 return FLGR_RET_NULL_OBJECT;
01588 }
01589
01590 if(img->spp!=1) return FLGR_RET_NOT_IMPLEMENTED;
01591
01592 if(!flgr_check_extension(filename,".pgm")) {
01593 fname = flgr_set_extension(filename,".pgm");
01594 } else {
01595 fname = flgr_set_extension(filename,"");
01596 }
01597
01598
01599 if(img->type == FLGR_UINT8){
01600 colorWidth = 255;
01601 }else if(img->type == FLGR_UINT16) {
01602 colorWidth = 65535;
01603 }else {
01604 return FLGR_RET_NOT_IMPLEMENTED;
01605 }
01606
01607
01608 if((f=fopen(fname,"wb"))==NULL) {
01609 POST_ERROR("\"%s\" can not be created!\n",fname);
01610 free(fname);
01611 return FLGR_RET_ALLOCATION_ERROR;
01612 }
01613
01614 if(format==5) {
01615 fprintf(f,"P5\n# fulguro by Christophe Clienti\n%d %d\n%d\n",img->size_x,img->size_y,colorWidth);
01616 flgr2d_save_raw_desc(img,f);
01617 }else {
01618 fprintf(f,"P2\n# fulguro by Christophe Clienti\n%d %d\n%d\n",img->size_x,img->size_y,colorWidth);
01619 flgr2d_save_text_desc(img,f);
01620 }
01621
01622 fclose(f);
01623 free(fname);
01624
01625 return FLGR_RET_OK;
01626 }
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01646
01650
01651 FLGR_Data2D *flgr2d_load_bmp(char *filename) {
01652 FLGR_Data2D *img;
01653 FILE *f;
01654 char buff[16];
01655 unsigned int offset;
01656 unsigned int size_x,size_y;
01657 unsigned short bps;
01658 unsigned int compression;
01659 int spp;
01660 FLGR_Type type;
01661
01662
01663
01664
01665 if( (f=fopen(filename,"rb"))==NULL) {
01666 POST_ERROR("couldn't open \"%s\"!\n",filename);
01667 return NULL;
01668 }
01669
01670
01671 fread(buff,1,2,f);
01672 if((buff[0] != 'B') && (buff[1] != 'M')) {
01673 fclose(f);
01674 return NULL;
01675 }
01676
01677 fread(buff,1,8,f);
01678 fread(&offset,4,1,f);
01679 fread(buff,1,4,f);
01680
01681 fread(&size_x,4,1,f);
01682 fread(&size_y,4,1,f);
01683 fread(buff,1,2,f);
01684
01685 fread(&bps,2,1,f);
01686 fread(&compression,4,1,f);
01687
01688
01689 if(compression != 0) {
01690 POST_ERROR("could not open compressed bitmap\n");
01691 fclose(f);
01692 return NULL;
01693 }
01694
01695 rewind(f);
01696
01697 switch(bps) {
01698 case 1: spp=1; type = FLGR_BIT; break;
01699 case 8: spp=1; type = FLGR_UINT8; break;
01700 case 16: bps=8; spp=2; type = FLGR_UINT8; break;
01701 case 24: bps=8; spp=3; type = FLGR_UINT8; break;
01702 case 32: bps=8; spp=4; type = FLGR_UINT8; break;
01703 default:
01704 POST_ERROR("Unsupported bitmap file\n");
01705 fclose(f);
01706 return NULL;
01707 }
01708
01709 img = flgr2d_load_raw_desc(f, offset, size_y, size_x, spp, type);
01710
01711 flgr2d_mirror_vertical_hmorph(img);
01712
01713 flgr2d_revert_channel_hmorph(img);
01714
01715 fclose(f);
01716
01717 return img;
01718 }
01719
01720
01721
01722
01723
01724
01725