70 int read_PNM_file(
char * file_name,
75 unsigned char ** data )
88 char PNM_descriptor[ 10 ] ;
89 char buffer, can_continue, endlin[ 10 ] ;
90 unsigned char byte_read ;
92 long total_number_of_pixels ;
94 int image_stats[ 3 ] ;
99 if ( file_name != NULL )
101 PNM_file.open( file_name ) ;
104 cerr <<
"ERROR! File " << file_name
105 <<
" could not be opened! Exiting.\n" ;
113 ( ( file_name != NULL )?PNM_file.peek():cin.peek() )
116 ( file_name != NULL )?PNM_file.ignore( 1000, (
int)
'\n' ):
117 cin.ignore( 1000, (
int)
'\n' ) ;
122 ( file_name != NULL )?PNM_file.getline( PNM_descriptor, 10 ):
123 cin.getline( PNM_descriptor, 10 ) ;
125 switch( PNM_descriptor[1] )
144 <<
"Unrecognized PNM format! Exiting\n" ;
160 (file_name != NULL)?buffer = PNM_file.peek():
161 buffer = cin.peek() ;
166 PNM_file.ignore( 1000, (
int)
'\n' ):
167 cin.ignore( 1000, (
int)
'\n' ) ;
171 else if ( !isdigit( buffer ) )
173 ( file_name != NULL )?
174 PNM_file.ignore( 1 ):
180 while ( !can_continue ) ;
182 ( file_name != NULL )?
183 PNM_file >> image_stats[ index ]:
184 cin >> image_stats[ index ] ;
188 while ( index < 3 ) ;
191 width = image_stats[ 0 ] ;
192 height = image_stats[ 1 ] ;
193 max_color_depth = image_stats[ 2 ] ;
197 total_number_of_pixels = width * height ;
200 if ( ( type == 3 ) || ( type == 6 ) )
202 total_number_of_pixels *= 3 ;
205 ( *data ) =
new unsigned char [ total_number_of_pixels ] ;
209 ( file_name != NULL )?
210 PNM_file.ignore( 1000, (
int)
'\n' ):
211 cin.ignore( 1000, (
int)
'\n' ) ;
220 for ( index = 0 ; index < total_number_of_pixels ; index++ )
223 if ( (file_name != NULL)?PNM_file.good():cin.good() )
226 (file_name != NULL)?PNM_file.get():cin.get() ;
239 for ( index = 0 ; index < total_number_of_pixels ; index++ )
242 while( !( isdigit((file_name != NULL)?PNM_file.peek():
244 || ( (file_name != NULL)?PNM_file.eof():0 )
249 PNM_file.ignore( 1 ):
252 if ( (file_name != NULL)?PNM_file.good():cin.good() )
255 (file_name != NULL)?PNM_file >> int_read:
257 (*data)[ index ] = (
unsigned char)int_read ;
272 cerr <<
"Error! The PNM file appears to be too short! Exiting!\n" ;
277 if ( file_name != NULL )
291 int read_GTE_file(
char * file_name,
296 unsigned char ** data )
303 long total_number_of_points, position_in_dataset ;
304 int index_x, index_y ;
305 double delta_x, delta_y ;
311 if ( file_name != NULL )
313 GTE_file.open( file_name ) ;
316 cerr <<
"ERROR! File " << file_name
317 <<
" could not be opened! Exiting.\n" ;
324 while( !( isdigit((file_name != NULL)?GTE_file.peek():
326 || ( (file_name != NULL)?GTE_file.eof():0 )
330 (file_name != NULL)?GTE_file.ignore( 1 ):cin.ignore( 1 );
333 ( file_name != NULL)?(GTE_file >> length_x):( cin >> length_x ) ;
336 while( !( isdigit((file_name != NULL)?GTE_file.peek():
338 || ( (file_name != NULL)?GTE_file.eof():0 )
342 (file_name != NULL)?GTE_file.ignore( 1 ):cin.ignore( 1 );
345 ( file_name != NULL)?(GTE_file >> numread):( cin >> numread ) ;
346 points_x = (int)numread ;
348 while( !( isdigit((file_name != NULL)?GTE_file.peek():
350 || ( (file_name != NULL)?GTE_file.eof():0 )
354 (file_name != NULL)?GTE_file.ignore( 1 ):cin.ignore( 1 );
357 ( file_name != NULL)?(GTE_file >> length_y):( cin >> length_y ) ;
360 while( !( isdigit((file_name != NULL)?GTE_file.peek():
362 || ( (file_name != NULL)?GTE_file.eof():0 )
366 (file_name != NULL)?GTE_file.ignore( 1 ):cin.ignore( 1 );
369 ( file_name != NULL)?(GTE_file >> numread):( cin >> numread ) ;
370 points_y = (int)(numread) ;
372 delta_x = length_x / ( (double)( points_x - 1 ) ) ;
373 length_x += delta_x ;
375 delta_y = length_y / ( (double)( points_y - 1 ) ) ;
376 length_y += delta_y ;
380 total_number_of_points = points_x * points_y ;
382 (*data) =
new unsigned char [ total_number_of_points ] ;
384 for ( index_y = 0 ; index_y < points_y ; index_y++ )
387 position_in_dataset = ( points_y - 1 - index_y ) * points_x ;
389 for ( index_x = 0 ; index_x < points_x ; index_x++ )
391 while( !( isdigit((file_name != NULL)?GTE_file.peek():
393 || ( (file_name != NULL)?GTE_file.eof():0 )
398 GTE_file.ignore( 1 ):cin.ignore( 1 );
401 if ( (file_name != NULL)?GTE_file.good():cin.good() )
404 (file_name != NULL)?GTE_file >> value:
407 (*data)[ position_in_dataset ]
408 = (
unsigned char)value ;
410 position_in_dataset++ ;
421 if ( status == 1) break ;
427 cerr <<
"Error! The GTE file appears to be too short! Exiting!\n" ;
443 int emit_PNM_file(
int points_x,
445 unsigned char * data )
448 long total_number_of_pixels, index ;
454 cout <<
"P5" << endl ;
455 cout << points_x <<
" " << points_y << endl ;
456 cout <<
"255" << endl ;
458 total_number_of_pixels = points_x * points_y ;
462 for ( index = 0 ; index < total_number_of_pixels ; index++ )
464 cout << data[ index ] ;
480 int emit_data(
int no_of_columns,
485 unsigned char * image_data )
489 double delta_x, delta_y ;
490 int index_x, index_y ;
491 int image_pixel_row, image_pixel_column, index_in_image_data ;
492 double position_x, position_y ;
497 if (length_x == 0.0 ) length_x = (double)no_of_columns ;
498 if (length_y == 0.0 ) length_y = (double)no_of_rows ;
500 delta_x = length_x / ((double) no_of_columns ) ;
501 delta_y = length_y / ((double) no_of_rows ) ;
506 if ( mode == EM_GTE )
508 cout << length_x - delta_x <<
" " 509 << no_of_columns << endl ;
510 cout << length_y - delta_y <<
" " 511 << no_of_rows << endl ;
530 for ( index_y = 0 ; index_y < no_of_rows ; index_y++)
536 position_y = (double)index_y * delta_y ;
538 image_pixel_row = no_of_rows - 1 - index_y ;
540 for ( index_x = 0 ; index_x < no_of_columns ; index_x++ )
543 position_x = (double)index_x * delta_x ;
544 image_pixel_column = index_x ;
546 index_in_image_data = image_pixel_row * no_of_columns ;
547 index_in_image_data += image_pixel_column ;
549 if ( mode == EM_GTE )
552 (int) image_data[index_in_image_data] <<
" " ;
554 else if ( mode == EM_DAT )
556 cout << position_x <<
" " 558 <<(int) image_data[index_in_image_data]
563 if ( mode == EM_GTE )
578 int scale_data(
int scale,
581 unsigned char * data )
587 unsigned char saturation_max, saturation_min, value ;
596 no_of_points = points_x * points_y ;
602 saturation_min = 255 ;
604 for ( index = 0 ; index < no_of_points ; index++ )
607 value = data[ index ] ;
608 if ( value > saturation_max )
610 saturation_max = value ;
612 if ( value < saturation_min )
614 saturation_min = value ;
619 saturation_max = saturation_max - saturation_min ;
624 for ( index = 0 ; index < no_of_points ; index++ )
626 value = data[ index ] ;
627 value = value - saturation_min ;
628 value = (
unsigned char)( (
double)value * 255.0 /
629 (double)saturation_max ) ;
630 data[ index ] = value ;
641 int invert_data(
int points_x,
644 unsigned char * data )
650 unsigned char saturation_max, saturation_min, temp ;
654 no_of_points = points_x * points_y ;
658 for ( index = 0 ; index < no_of_points ; index++ )
661 temp = 255 - data[ index ] ;
662 data[ index ] = temp ;
671 saturation_min = 255 ;
673 for ( index = 0 ; index < no_of_points ; index++ )
675 temp = data[ index ];
676 if ( temp < saturation_min )
678 saturation_min = temp ;
680 if ( temp > saturation_max )
682 saturation_max = temp ;
688 for ( index = 0 ; index < no_of_points ; index++ )
690 temp = saturation_max - data[ index ];
691 temp += saturation_min ;
693 data[ index ] = temp ;
708 int blow_up(
int scale,
713 unsigned char ** data )
717 long no_of_points_old, no_of_points_new, index ;
718 int index_x, index_y, index_x_old, index_y_old ;
719 int points_x_new, points_y_new ;
720 long position_in_old, position_in_new ;
721 unsigned char value ;
722 unsigned char * new_data ;
730 scale_data( scale, points_x, points_y, (*data) ) ;
734 scale_x = fabs( scale_x ) ;
735 scale_y = fabs( scale_y ) ;
738 if ( scale_x == 0.0 ) scale_x = 0.01 ;
739 if ( scale_y == 0.0 ) scale_y = 0.01 ;
741 points_x_new = (int)( (
double)points_x * scale_x ) ;
742 points_y_new = (int)( (
double)points_y * scale_y ) ;
745 if ( ( scale_x != 1.0 ) || ( scale_y != 1.0 ) )
748 no_of_points_old = points_x * points_y ;
749 no_of_points_new = points_x_new * points_y_new ;
752 new_data =
new unsigned char [ no_of_points_new ] ;
754 for ( index_y = 0 ; index_y < points_y_new ; index_y++ )
757 position_in_new = index_y * points_x_new ;
759 index_y_old = (int)( (
double)index_y / scale_y ) ;
761 for ( index_x = 0 ; index_x < points_x_new ; index_x++ )
764 index_x_old = (int)( (
double)index_x / scale_x ) ;
767 position_in_old = index_y_old * points_x +
770 new_data[ position_in_new ] =
771 (*data)[ position_in_old ] ;
782 points_x = points_x_new ;
783 points_y = points_y_new ;