"Fossies" - the Fresh Open Source Software Archive

Member "jpegoptim-1.4.6/jpegdest.c" (18 Apr 2018, 2819 Bytes) of package /linux/privat/jpegoptim-1.4.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "jpegdest.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.4.5_vs_1.4.6.

    1 /*
    2  * jpegdest.c
    3  *
    4  * Copyright (C) 2014 Timo Kokkonen
    5  * All Rights Reserved.
    6  *
    7  * custom libjpeg "Destination Manager" for saving into RAM
    8  *
    9  * $Id: 2689b6f6dc660d3386bf5c0ca3ea2e616faa46f7 $
   10  */
   11 
   12 #include <stdio.h>
   13 #include <stdlib.h>
   14 #include <string.h>
   15 #include <jpeglib.h>
   16 #include <jerror.h>
   17 
   18 #include "jpegoptim.h"
   19 
   20 
   21 
   22 /* custom jpeg destination manager object */
   23 
   24 typedef struct {
   25   struct jpeg_destination_mgr pub; /* public fields */
   26 
   27   unsigned char **buf_ptr;
   28   size_t *bufsize_ptr;
   29   size_t incsize;
   30 
   31   unsigned char *buf;       
   32   size_t bufsize;
   33 
   34 } jpeg_memory_destination_mgr;
   35 
   36 typedef jpeg_memory_destination_mgr* jpeg_memory_destination_ptr;
   37 
   38 
   39 
   40 
   41 void jpeg_memory_init_destination (j_compress_ptr cinfo)
   42 {
   43   jpeg_memory_destination_ptr dest = (jpeg_memory_destination_ptr) cinfo->dest;
   44 
   45   dest->pub.next_output_byte = dest->buf;
   46   dest->pub.free_in_buffer = dest->bufsize;
   47 }
   48 
   49 
   50 boolean jpeg_memory_empty_output_buffer (j_compress_ptr cinfo)
   51 {
   52   jpeg_memory_destination_ptr dest = (jpeg_memory_destination_ptr) cinfo->dest;
   53   unsigned char *newbuf;
   54 
   55   /* abort if incsize is 0 (no expansion of buffer allowed) */
   56   if (dest->incsize == 0) ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 42);
   57 
   58   /* otherwise, try expanding buffer... */
   59   newbuf = realloc(dest->buf,dest->bufsize + dest->incsize);
   60   if (!newbuf) ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 42);
   61 
   62   dest->pub.next_output_byte = newbuf + dest->bufsize;
   63   dest->pub.free_in_buffer = dest->incsize;
   64 
   65   *dest->buf_ptr = newbuf;
   66   dest->buf = newbuf;
   67   dest->bufsize += dest->incsize;
   68   dest->incsize *= 2;
   69 
   70   return TRUE;
   71 }
   72 
   73 
   74 
   75 void jpeg_memory_term_destination (j_compress_ptr cinfo)
   76 {
   77   jpeg_memory_destination_ptr dest = (jpeg_memory_destination_ptr) cinfo->dest;
   78 
   79   *dest->buf_ptr = dest->buf;
   80   *dest->bufsize_ptr = dest->bufsize - dest->pub.free_in_buffer;
   81 }
   82 
   83 
   84 
   85 void jpeg_memory_dest (j_compress_ptr cinfo, unsigned char **bufptr, size_t *bufsizeptr, size_t incsize)
   86 {
   87   jpeg_memory_destination_ptr dest;
   88 
   89   if (!cinfo || !bufptr || !bufsizeptr) 
   90     fatal("invalid call to jpeg_memory_dest()");
   91   if (!*bufptr || *bufsizeptr == 0) 
   92     fatal("invalid buffer passed to jpeg_memory_dest()");
   93   
   94 
   95   /* allocate destination manager object for compress object, if needed */
   96   if (!cinfo->dest) {
   97     cinfo->dest = (struct jpeg_destination_mgr *)
   98       (*cinfo->mem->alloc_small) ( (j_common_ptr) cinfo,
   99                    JPOOL_PERMANENT,
  100                    sizeof(jpeg_memory_destination_mgr) );
  101   }
  102 
  103   dest = (jpeg_memory_destination_ptr)cinfo->dest;
  104 
  105   dest->buf_ptr = bufptr;
  106   dest->buf = *bufptr;
  107   dest->bufsize_ptr = bufsizeptr;
  108   dest->bufsize = *bufsizeptr;
  109   dest->incsize = incsize;
  110 
  111   dest->pub.init_destination = jpeg_memory_init_destination;
  112   dest->pub.empty_output_buffer = jpeg_memory_empty_output_buffer;
  113   dest->pub.term_destination = jpeg_memory_term_destination;
  114 }
  115