/* Makefile for octave image read/write scripts Copyright (C) 2000 Andy Adler. This code has no warranty whatsoever. You may do what you like with this code as long as you leave this copyright in place. If you modify the code then include a notice saying so. This is modified from the Independent JPEG Group's software See the accompanying Makefile for instructions $Id: octjpglib.cc,v 1.2 2000/06/08 04:23:22 andy Exp andy $ */ #include #ifdef __cplusplus extern "C" { #endif // we need to undef it here because jconfig redefines it # undef HAVE_STDLIB_H # include "cdjpeg.h" /* # include # include # include # include /usr/include/jconfig.h /usr/include/jerror.h /usr/include/jmorecfg.h /usr/include/jpeglib.h */ #ifdef __cplusplus } //extern "C" #endif /* * read a non-jpeg image from a file * * this stuff is slightly wierd, since it's based * on the cjpeg functionality to read from other * image types to create jpeg files */ octave_value_list read_other_image( FILE * infile, int nargout, int input_file_type ) { octave_value_list retval; struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); /* * select input file type */ cjpeg_source_ptr src_mgr ; switch (input_file_type) { case 'B': src_mgr= jinit_read_bmp(&cinfo); break; case 'G': src_mgr= jinit_read_gif(&cinfo); break; case 'P': src_mgr= jinit_read_ppm(&cinfo); break; default: error("unknown image format"); return retval; break; } src_mgr->input_file = infile; (*src_mgr->start_input) (&cinfo, src_mgr); jpeg_default_colorspace(&cinfo); jpeg_stdio_dest(&cinfo, stdout ); jpeg_start_compress(&cinfo, TRUE); unsigned long wid = cinfo.image_width; unsigned long hig = cinfo.image_height; // printf("w=%ld, h=%ld\n",wid,hig); // unsigned long comp= cinfo.image_components; // unsigned long row_stride= wid*comp; // // Create 3 matrices, One each for the Red, Green, and Blue component of the image. // or create 1 matrix for the avg intensity // // Now, loop through each of the scanlines. For each, copy the image // data from the buffer, convert to double. // if (nargout == 3) { Matrix red ( hig , wid ); Matrix green ( hig , wid ); Matrix blue ( hig , wid ); for (unsigned int j=0; j< hig; ) { JDIMENSION num_scanlines= (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); for (unsigned int k=0; kbuffer; for (unsigned long i=0; iget_pixel_rows) (&cinfo, src_mgr); for (unsigned int k=0; kbuffer; for (unsigned long i=0; ifinish_input) (&cinfo, src_mgr); return retval; } /* * read a jpeg image from a file */ octave_value_list read_jpeg_image( FILE * infile, int nargout ) { octave_value_list retval; // // Initialize the jpeg library // struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); // // Read the jpg header to get info about size and color depth // jpeg_stdio_src(&cinfo, infile); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); if (cinfo.output_components == 1) { // Grayscale jpeg_destroy_decompress(&cinfo); error("Grayscale jpegs not supported"); return retval; } // // Allocate buffer for one scan line // unsigned long wid = cinfo.output_width; unsigned long hig = cinfo.output_height; unsigned long comp= cinfo.output_components; unsigned long row_stride= wid*comp; JSAMPARRAY buffer= (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); // // Create 3 matrices, One each for the Red, Green, and Blue component of the image. // or create 1 matrix for the avg intensity // // Now, loop through each of the scanlines. For each, copy the image // data from the buffer, convert to double. // if (nargout == 3) { Matrix red ( hig , wid ); Matrix green ( hig , wid ); Matrix blue ( hig , wid ); for (unsigned long j=0; cinfo.output_scanline < hig; j++) { jpeg_read_scanlines(&cinfo, buffer,1); for (unsigned long i=0; i im is a greyscale (0-255) of image in fname\n\ [r,g,b]= imread(fname, fmt); -> rgb are red,green,blue (0-255) components\n\ [im,map]=imread(fname, fmt); -> index and colourmap (UNSUPPORTED RIGHT NOW) \n\ filename -> image to read\n\ \n\ fmt -> image storage type\n\ if format is not provided, it will be autodetected\n\ \n\ currently the following formats are supported\n\ if fmt is not supplied, imread will attempt to get it from the filename\n\ \n\ fmt == 'gif' (LIBJPEG NO LONGER PROVIDES THIS)\n\ fmt == 'bmp'\n\ fmt == 'jpg' or 'jpeg'\n\ fmt == 'ppm' or 'pgm' or 'pnm' or 'pbm'" ) { octave_value_list retval; int nargin = args.length(); // // We bail out if the input parameters are bad // if (nargin < 1 || nargin > 2 || !args(0).is_string() ) { print_usage ("imread"); return retval; } string filename = args(0).string_value(); FILE * infile = fopen(filename.c_str(), "rb"); // // Open file // if (infile == NULL) { error("Couldn't open file"); return retval; } /* * if nargin==2 , filetype is given * otherwise check for it */ int input_file_type= 'J'; if (nargin ==2 ) { string ftype= args(1).string_value(); if ( ftype == "jpeg" || ftype == "jpg" ) input_file_type= 'J'; else if ( ftype == "gif" ) input_file_type= 'G'; else if ( ftype == "bmp" ) input_file_type= 'B'; else if ( ftype=="ppm" || ftype=="pgm" || ftype=="pnm" || ftype=="pbm" ) input_file_type= 'P'; else { error("Couldn't recognize file type"); return retval; } } else { if ((input_file_type = getc(infile)) == EOF) { error("can't read input file"); return retval; } if (ungetc(input_file_type, infile) == EOF) { error("can't read input file"); return retval; } if (input_file_type == 0xFF) input_file_type= 'J'; } printf("filetype=%c\n", input_file_type ); if ( input_file_type== 'J' ) retval= read_jpeg_image( infile, nargout ); else retval= read_other_image( infile, nargout, input_file_type ); fclose(infile); return retval; }