Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

rgb_io_ppm.c

Go to the documentation of this file.
00001 /* 00002 * Input and Output (2D) image using PPM format. 00003 */ 00004 00005 #include "config.h" 00006 00007 #include <volume_io/internal_volume_io.h> 00008 #include <bicpl/objects.h> 00009 #include <bicpl/images.h> 00010 00011 #include <ppm.h> 00012 00013 00014 00015 public Status input_rgb_file( 00016 STRING filename, 00017 pixels_struct *pixels ) 00018 { 00019 FILE* f; 00020 pixel* rowbuf; 00021 int n_cols, n_rows, format; 00022 pixval max_pixval; 00023 int x,y; 00024 00025 if ( (f = fopen(filename,"r")) == NULL ) { 00026 print_error( "Error: output file could not be opened for reading: %s\n", 00027 filename ); 00028 return( ERROR ); 00029 } 00030 00031 ppm_readppminit( f, &n_cols, &n_rows, &max_pixval, &format ); 00032 00033 if ( (rowbuf = ppm_allocrow( n_cols ) ) == NULL ) { 00034 print_error( "Error: could not allocate memory for image\n" ); 00035 return( ERROR ); 00036 } 00037 00038 initialize_pixels( pixels, 0, 0, n_cols, n_rows, 1.0, 1.0, RGB_PIXEL ); 00039 00040 00041 /* The image appears to be scanned from left to right, 00042 and bottom to top, so we scan from the largest row index 00043 to the smallest. */ 00044 00045 for ( y = n_rows - 1; y >= 0; --y ) { 00046 ppm_readppmrow( f, rowbuf, n_cols, max_pixval, format ); 00047 for( x = 0; x < n_cols; ++x ) { 00048 PIXEL_RGB_COLOUR( *pixels, x, y ) 00049 = make_rgba_Colour( PPM_GETR( rowbuf[x] ), 00050 PPM_GETG( rowbuf[x] ), 00051 PPM_GETB( rowbuf[x] ), 00052 255 ); 00053 } 00054 } 00055 00056 ppm_freerow( rowbuf ); 00057 fclose( f ); 00058 00059 return( OK ); 00060 } 00061 00062 00063 public Status output_rgb_file( 00064 STRING filename, 00065 pixels_struct *pixels ) 00066 { 00067 FILE* f; 00068 pixel* rowbuf; 00069 int x,y; 00070 00071 if ( pixels->pixel_type != RGB_PIXEL ) { 00072 print_error( "Error: only RGB_PIXEL images are handled\n" ); 00073 return( ERROR ); 00074 } 00075 00076 if( !file_directory_exists( filename ) ) 00077 { 00078 print_error( "Error: output file directory does not exist: %s\n", 00079 filename ); 00080 return( ERROR ); 00081 } 00082 00083 if ( (f = fopen(filename,"w")) == NULL ) { 00084 print_error( "Error: output file could not be opened for writing: %s\n", 00085 filename ); 00086 return( ERROR ); 00087 } 00088 00089 /* I think some errors detected by the ppm_ routines 00090 are handled by PPM, which can be changed, 00091 and probably ought to be. */ 00092 00093 /* The last argument is set to 0 to allow "raw" mode, 00094 and the penultimate argument is the max. value for 00095 a red/green/blue value. Note we are assuming RGB_PIXEL 00096 uses 8-bit values, which was true when I wrote this. */ 00097 ppm_writeppminit( f, pixels->x_size, pixels->y_size, 255, 0 ); 00098 00099 00100 if ( (rowbuf = ppm_allocrow( pixels->x_size ) ) == NULL ) { 00101 print_error( "Error: could not allocate memory for image\n" ); 00102 return( ERROR ); 00103 } 00104 00105 /* The image appears to be scanned from left to right, 00106 and bottom to top, so we scan from the largest row index 00107 to the smallest. */ 00108 00109 for ( y = pixels->y_size - 1; y >= 0; --y ) { 00110 for( x = 0; x < pixels->x_size; ++x ) { 00111 Colour col = PIXEL_RGB_COLOUR( *pixels, x, y ); 00112 PPM_ASSIGN( rowbuf[x], 00113 get_Colour_r( col ), 00114 get_Colour_g( col ), 00115 get_Colour_b( col ) ); 00116 } 00117 00118 ppm_writeppmrow( f, rowbuf, pixels->x_size, 255, 0 ); 00119 } 00120 00121 ppm_freerow( rowbuf ); 00122 fclose( f ); 00123 00124 return( OK ); 00125 } 00126

Generated on Wed Jul 28 09:10:58 2004 for BICPL by doxygen 1.3.7