From octave-sources-request at bevo dot che dot wisc dot edu Wed Jun 7 23:01:29 2000 Subject: *oct file to read jpg,bmp,p?m files From: en254 at freenet dot carleton dot ca To: octave-sources at bevo dot che dot wisc dot edu Date: Thu, 8 Jun 2000 00:30:26 -0400 (EDT) The following *oct files will read jpg,bmp,p?m. FILES: 1) Makefile 2) ocjpglib.cc ______________________________________________________________ Andy Adler, en254 at ncf dot ca ------------START OF FILE: Makefile ------------------- # Makefile for octave jpeg 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. # # INSTRUCTIONS: # # 1. Make sure you have the following files: # 1) Makefile # 2) ocjpglib.cc # 3) jpegsrc.v6b.tar.gz # this is available from ftp://ftp.uu.net/graphics/jpeg/ # and many other places # # 2. untar jpegsrc.v6b.tar.gz into the ./jpeg directory # # 3. type make (in the current directory) # (note, you do not need to make the jpeg sources, # this Makefile will do that for you) # # TODO: # # 1. Get the writing of files to work # 2. Fix the jpeg error handler to not break octave # # # This is modified from the # Makefile for Independent JPEG Group's software # ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6.tar.gz # # $Id: Makefile,v 1.2 2000/06/08 04:22:37 andy Exp andy $ CC= gcc CFLAGS= -O2 # Generally, we recommend defining any configuration symbols in jconfig.h, # NOT via -D switches here. LDFLAGS= LDLIBS= LN= $(CC) RM= rm -f MV= mv AR= ar rc # second step in .a creation (use "touch" if not needed) AR2= ranlib # End of configurable options. OCTLNKS= imread.oct imwrite.oct OCTSRC= octjpglib.cc # source files: JPEG library proper J= jpeg-6b LIBSOURCES= $(J)/jcapimin.c $(J)/jcapistd.c $(J)/jccoefct.c $(J)/jccolor.c \ $(J)/jcdctmgr.c $(J)/jchuff.c $(J)/jcinit.c $(J)/jcmainct.c \ $(J)/jcmarker.c $(J)/jcmaster.c $(J)/jcomapi.c $(J)/jcparam.c \ $(J)/jcphuff.c $(J)/jcprepct.c $(J)/jcsample.c $(J)/jctrans.c \ $(J)/jdapimin.c $(J)/jdapistd.c $(J)/jdatadst.c $(J)/jdatasrc.c \ $(J)/jdcoefct.c $(J)/jdcolor.c $(J)/jddctmgr.c $(J)/jdhuff.c \ $(J)/jdinput.c $(J)/jdmainct.c $(J)/jdmarker.c $(J)/jdmaster.c \ $(J)/jdmerge.c $(J)/jdphuff.c $(J)/jdpostct.c $(J)/jdsample.c \ $(J)/jdtrans.c $(J)/jerror.c $(J)/jfdctflt.c $(J)/jfdctfst.c \ $(J)/jfdctint.c $(J)/jidctflt.c $(J)/jidctfst.c $(J)/jidctint.c \ $(J)/jidctred.c $(J)/jquant1.c $(J)/jquant2.c $(J)/jutils.c \ $(J)/jmemmgr.c $(J)/rdcolmap.c $(J)/rdppm.c $(J)/wrppm.c \ $(J)/rdgif.c $(J)/wrgif.c \ $(J)/rdbmp.c $(J)/wrbmp.c $(J)/jmemnobs.c # files included by source files INCLUDES= $(J)/jchuff.h $(J)/jdhuff.h $(J)/jdct.h $(J)/jerror.h \ $(J)/jinclude.h $(J)/jmemsys.h $(J)/jmorecfg.h $(J)/jpegint.h \ $(J)/jpeglib.h $(J)/jversion.h $(J)/cdjpeg.h $(J)/cderror.h \ $(J)/transupp.h $(J)/jconfig.h LIBOBJECTS = $(patsubst %.c,%.o,$(LIBSOURCES)) all: $(OCTLNKS) # build jconfig.h using the configure script from the jpeg lib $(J)/jconfig.h: $(J)/configure echo $(LIBOBJECTS) ( cd $(J); ./configure ) libjpeg.a: $(J)/jconfig.h $(LIBOBJECTS) $(RM) libjpeg.a $(AR) libjpeg.a $(LIBOBJECTS) $(AR2) libjpeg.a $(OCTLNKS): $(OCTSRC) libjpeg.a mkoctfile -I$(J) $(OCTSRC) -L. -ljpeg -o octjpglib.oct for l in $(OCTLNKS) ; do ln -sf octjpglib.oct $$l ; done clean: -rm $(LIBOBJECTS) -rm $(OCTLNKS) -rm libjpeg.a octjpglib.o* ------------END OF FILE: Makefile ------------------- ------------START OF FILE: ocjpglib.cc ------------------- /* 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; } ------------END OF FILE: ocjpglib.cc ------------------- ----------------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.che.wisc.edu/octave/octave.html How to fund new projects: http://www.che.wisc.edu/octave/funding.html Subscription information: http://www.che.wisc.edu/octave/archive.html -----------------------------------------------------------------------