git.y1.nz

fbui

Framebuffer-based graphical environment
download: https://git.y1.nz/archives/fbui.tar.gz
README | Files | Log | Refs

libfbui/Viewer/jpeg.c

      1 
      2 /* This file is just a simple modification of the jpeglib
      3  * sample program.
      4  */
      5 
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 
     10 
     11 #include <jpeglib.h>
     12 
     13 #include <setjmp.h>
     14 
     15 #include "libfbui.h"
     16 
     17 
     18 extern JSAMPLE * image_buffer;	/* Points to large array of R,G,B-order data */
     19 extern int image_height;	/* Number of rows in image */
     20 extern int image_width;		/* Number of columns in image */
     21 extern int image_ncomponents;
     22 
     23 static int y=0;
     24 
     25 extern unsigned char *get_buffer (unsigned long size);
     26 
     27 int put_scanline_someplace(JSAMPLE* data, int width)
     28 {
     29 	if (!image_buffer) {
     30 printf ("image_width=%d image_height=%d\n", image_width,image_height);
     31 		image_buffer = get_buffer (image_width *image_height*3);
     32 		if (!image_buffer)
     33 			FATAL ("failed to malloc image buffer");
     34 	}
     35 
     36 	if (y >= image_height)
     37 		return 1;
     38 	if (width >= 3*image_width)
     39 		width = 3 * image_width;
     40 
     41 	unsigned char *tmp = (unsigned char*) data;
     42 	unsigned char *tmp2 = (unsigned char*) image_buffer;
     43 	tmp2 = &tmp2 [3 * y * image_width];
     44 	memcpy (tmp2,tmp,width);
     45 	y++;
     46 	return 1;
     47 }
     48 
     49 
     50 
     51 struct my_error_mgr {
     52   struct jpeg_error_mgr pub;	/* "public" fields */
     53 
     54   jmp_buf setjmp_buffer;	/* for return to caller */
     55 };
     56 
     57 typedef struct my_error_mgr * my_error_ptr;
     58 
     59 METHODDEF(void)
     60 my_error_exit (j_common_ptr cinfo)
     61 {
     62   my_error_ptr myerr = (my_error_ptr) cinfo->err;
     63 
     64   (*cinfo->err->output_message) (cinfo);
     65 
     66   longjmp(myerr->setjmp_buffer, 1);
     67 }
     68 
     69 GLOBAL(int)
     70 read_JPEG_file (char * filename)
     71 {
     72   struct jpeg_decompress_struct cinfo;
     73   struct my_error_mgr jerr;
     74   FILE * infile;		/* source file */
     75   JSAMPARRAY buffer;		/* Output row buffer */
     76   int row_stride;		/* physical row width in output buffer */
     77 
     78 y=0;
     79 
     80   if ((infile = fopen(filename, "rb")) == NULL) {
     81     fprintf(stderr, "can't open %s\n", filename);
     82     return 0;
     83   }
     84 
     85   /* Step 1: allocate and initialize JPEG decompression object */
     86 
     87   /* We set up the normal JPEG error routines, then override error_exit. */
     88   cinfo.err = jpeg_std_error(&jerr.pub);
     89   jerr.pub.error_exit = my_error_exit;
     90   /* Establish the setjmp return context for my_error_exit to use. */
     91   if (setjmp(jerr.setjmp_buffer)) {
     92     /* If we get here, the JPEG code has signaled an error.
     93      * We need to clean up the JPEG object, close the input file, and return.
     94      */
     95     jpeg_destroy_decompress(&cinfo);
     96     fclose(infile);
     97     return 0;
     98   }
     99   /* Now we can initialize the JPEG decompression object. */
    100   jpeg_create_decompress(&cinfo);
    101 
    102   /* Step 2: specify data source (eg, a file) */
    103 
    104   jpeg_stdio_src(&cinfo, infile);
    105 
    106   /* Step 3: read file parameters with jpeg_read_header() */
    107 
    108   (void) jpeg_read_header(&cinfo, TRUE);
    109   /* We can ignore the return value from jpeg_read_header since
    110    *   (a) suspension is not possible with the stdio data source, and
    111    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
    112    * See libjpeg.doc for more info.
    113    */
    114 
    115   /* Step 4: set parameters for decompression */
    116 
    117   /* In this example, we don't need to change any of the defaults set by
    118    * jpeg_read_header(), so we do nothing here.
    119    */
    120 
    121   /* Step 5: Start decompressor */
    122 
    123   (void) jpeg_start_decompress(&cinfo);
    124   /* We can ignore the return value since suspension is not possible
    125    * with the stdio data source.
    126    */
    127 
    128   /* We may need to do some setup of our own at this point before reading
    129    * the data.  After jpeg_start_decompress() we have the correct scaled
    130    * output image dimensions available, as well as the output colormap
    131    * if we asked for color quantization.
    132    * In this example, we need to make an output work buffer of the right size.
    133    */ 
    134   /* JSAMPLEs per row in output buffer */
    135 
    136   image_height = cinfo.output_height;
    137   image_width = cinfo.output_width;
    138   row_stride = cinfo.output_width * cinfo.output_components;
    139   image_ncomponents = cinfo.output_components;
    140 
    141   /* Make a one-row-high sample array that will go away when done with image */
    142   buffer = (*cinfo.mem->alloc_sarray)
    143 		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
    144 
    145   /* Step 6: while (scan lines remain to be read) */
    146   /*           jpeg_read_scanlines(...); */
    147 
    148   /* Here we use the library's state variable cinfo.output_scanline as the
    149    * loop counter, so that we don't have to keep track ourselves.
    150    */
    151   while (cinfo.output_scanline < cinfo.output_height) {
    152     /* jpeg_read_scanlines expects an array of pointers to scanlines.
    153      * Here the array is only one element long, but you could ask for
    154      * more than one scanline at a time if that's more convenient.
    155      */
    156     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
    157     /* Assume put_scanline_someplace wants a pointer and sample count. */
    158     put_scanline_someplace(buffer[0], row_stride);
    159   }
    160 
    161   /* Step 7: Finish decompression */
    162 
    163   (void) jpeg_finish_decompress(&cinfo);
    164   /* We can ignore the return value since suspension is not possible
    165    * with the stdio data source.
    166    */
    167 
    168   /* Step 8: Release JPEG decompression object */
    169 
    170   /* This is an important step since it will release a good deal of memory. */
    171   jpeg_destroy_decompress(&cinfo);
    172 
    173   /* After finish_decompress, we can close the input file.
    174    * Here we postpone it until after no more JPEG errors are possible,
    175    * so as to simplify the setjmp error logic above.  (Actually, I don't
    176    * think that jpeg_destroy can do an error exit, but why assume anything...)
    177    */
    178   fclose(infile);
    179 
    180   /* At this point you may want to check to see whether any corrupt-data
    181    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
    182    */
    183 
    184   /* And we're done! */
    185   return 1;
    186 }
    187 

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.