/* Sampe code uses JPEG library. This program reads a jpeg image, converts to a B/W image, and writes to a RAS image. Project files : jpeg2ras.c + rasio.c JPEG library can be obtained from http://www.ijg.org */ #include #include #include "jpeglib.h" unsigned char **cpicalloc(int, int); unsigned char **process(unsigned char **, int, int); void write_raster_file(char *,unsigned char **,int, int); extern swabb; main() { FILE *jpegin, *jpegout; char file_name[50]; unsigned char **pic, **out_pic; int width, height, row_stride; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; // Open an image file sprintf(file_name, "%s.jpg", "testimg"); if((jpegin = fopen(file_name, "rb")) == NULL){ fprintf(stderr, "cannot open %s\n", file_name); return 0; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, jpegin); (void) jpeg_read_header(&cinfo, TRUE); // read header (void) jpeg_start_decompress(&cinfo); // decompressing row_stride = cinfo.output_width * cinfo.output_components; width = cinfo.output_width; height = cinfo.output_height; pic = cpicalloc(cinfo.output_height, cinfo.output_width*3); // allocate a buffer for the input jpeg color(RGB 3 channels) image out_pic = cpicalloc(cinfo.output_height, cinfo.output_width); // allocare a buffer fot the output sunraster B/W image while (cinfo.output_scanline < cinfo.output_height) { (void) jpeg_read_scanlines(&cinfo, &pic[cinfo.output_scanline], 1); // read the jpeg image } (void) jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(jpegin); /* Processing (conver to a B/W picture) */ out_pic = process(pic, cinfo.output_height, cinfo.output_width); // processs the original image sprintf(file_name, "%s.ras", "output"); if((jpegout = fopen(file_name, "wb")) == NULL){ // open another file for writing the sunraster fprintf(stderr, "cannot open %s\n", file_name); return 0; } // write to a sunraster file swabb = 1; // set the swabb flag to 1, Don't forget write_raster_file( file_name, out_pic, height, width); // write sunraster buffer to the image file return 1; } unsigned char **cpicalloc( int row, int col) { int i; unsigned char **p; p = (unsigned char **) calloc(row,sizeof(unsigned char *)); if(!p){ fprintf(stderr,"memory allocation error\n"); exit(1); } for(i=0;i