home *** CD-ROM | disk | FTP | other *** search
/ AMIGA PD 1 / AMIGA-PD-1.iso / Programme_zum_Heft / Anwendungen / Kurztests / PBM / HPCDTOPPM_0_6.LHA / hpcdtoppm.0.6 / src / jpeg.c < prev    next >
C/C++ Source or Header  |  1994-10-08  |  8KB  |  219 lines

  1. /* jpeg.c written by Günther Röhrich */
  2.  
  3. #include "hpcdtoppm.h"
  4. #include "jinclude.h"
  5. #include <setjmp.h>
  6.  
  7. FLTPT JPEG_QUALITY;
  8. sINT QUALITY_SET=0;
  9. FLTPT JPEG_MAXMEMORY;
  10. sINT MAXMEMORY_SET=0;
  11. sINT OPTIMIZE_JPEG=0;
  12.  
  13. static uBYTE *pr, *pg, *pb;
  14. static uBYTE *srptr, *sgptr, *sbptr;
  15. static sdim srzeil, sgzeil, sbzeil;
  16. static sdim srpix, sgpix, sbpix;
  17.  
  18. METHODDEF void
  19. input_init (compress_info_ptr cinfo)
  20. /* Initialize for input; return image size and component data. */
  21. {
  22.   /* This routine must return five pieces of information about the incoming
  23.    * image, and must do any setup needed for the get_input_row routine.
  24.    * The image information is returned in fields of the cinfo struct.
  25.    * (If you don't care about modularity, you could initialize these fields
  26.    * in the main JPEG calling routine, and make this routine be a no-op.)
  27.    * We show some example values here.
  28.    */
  29.   /* cinfo->image_width = 640;    */    /* width in pixels */
  30.   /* cinfo->image_height = 480;    */    /* height in pixels */
  31.   /* JPEG views an image as being a rectangular array of pixels, with each
  32.    * pixel having the same number of "component" values (color channels).
  33.    * You must specify how many components there are and the colorspace
  34.    * interpretation of the components.  Most applications will use RGB data or
  35.    * grayscale data.  If you want to use something else, you'll need to study
  36.    * and perhaps modify jcdeflts.c, jccolor.c, and jdcolor.c.
  37.    */
  38.   cinfo->input_components = 3;        /* or 1 for grayscale */
  39.   cinfo->in_color_space = CS_RGB;    /* or CS_GRAYSCALE for grayscale */
  40.   cinfo->data_precision = 8;        /* bits per pixel component value */
  41.   /* In the current JPEG software, data_precision must be set equal to
  42.    * BITS_IN_JSAMPLE, which is 8 unless you twiddle jconfig.h.  Future
  43.    * versions might allow you to say either 8 or 12 if compiled with
  44.    * 12-bit JSAMPLEs, or up to 16 in lossless mode.  In any case,
  45.    * it is up to you to scale incoming pixel values to the range
  46.    *   0 .. (1<<data_precision)-1.
  47.    * If your image data format is fixed at a byte per component,
  48.    * then saying "8" is probably the best long-term solution.
  49.    */
  50. }
  51.  
  52.  
  53. METHODDEF void
  54. get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  55. /* Read next row of pixels into pixel_row[][] */
  56. {
  57.   /* This example shows how you might read RGB data (3 components)
  58.    * from an input file in which the data is stored 3 bytes per pixel
  59.    * in left-to-right, top-to-bottom order.
  60.    */
  61.   register JSAMPROW ptr0, ptr1, ptr2;
  62.   register long col;
  63.   
  64.   ptr0 = pixel_row[0];
  65.   ptr1 = pixel_row[1];
  66.   ptr2 = pixel_row[2];
  67.   
  68.   pr = srptr; srptr+=srzeil;
  69.   pg = sgptr; sgptr+=sgzeil;
  70.   pb = sbptr; sbptr+=sbzeil;
  71.   for (col = 0; col < cinfo->image_width; col++) {
  72.     *ptr0++ = (JSAMPLE) *pr; /* red */
  73.     *ptr1++ = (JSAMPLE) *pg; /* green */
  74.     *ptr2++ = (JSAMPLE) *pb; /* blue */
  75.     pr+=srpix; pg+=sgpix; pb+=sbpix;
  76.   }
  77. }
  78.  
  79.  
  80.  
  81. METHODDEF void
  82. input_term (compress_info_ptr cinfo)
  83. /* Finish up at the end of the input */
  84. {
  85.   /* This termination routine will very often have no work to do, */
  86.   /* but you must provide it anyway. */
  87.   /* Note that the JPEG code will only call it during successful exit; */
  88.   /* if you want it called during error exit, you gotta do that yourself. */
  89. }
  90.  
  91. METHODDEF void
  92. c_ui_method_selection (compress_info_ptr cinfo)
  93. {
  94.   /* If the input is gray scale, generate a monochrome JPEG file. */
  95.   if (cinfo->in_color_space == CS_GRAYSCALE)
  96.     j_monochrome_default(cinfo);
  97.   /* For now, always select JFIF output format. */
  98.   jselwjfif(cinfo);
  99. }
  100.  
  101. void write_jpeg(FILE *fout,dim w,dim h, 
  102.                uBYTE *rptr,sdim rzeil,sdim rpix,  
  103.                uBYTE *gptr,sdim gzeil,sdim gpix,  
  104.                uBYTE *bptr,sdim bzeil,sdim bpix) 
  105.  {
  106.   
  107.   
  108.   /* These three structs contain JPEG parameters and working data.
  109.    * They must survive for the duration of parameter setup and one
  110.    * call to jpeg_compress; typically, making them local data in the
  111.    * calling routine is the best strategy.
  112.    */
  113.   struct Compress_info_struct cinfo;
  114.   struct Compress_methods_struct c_methods;
  115.   struct External_methods_struct e_methods;
  116.  
  117.   srptr = rptr; sgptr = gptr; sbptr = bptr;
  118.   srzeil = rzeil; sgzeil = gzeil; sbzeil = bzeil;
  119.   srpix = rpix; sgpix = gpix; sbpix = bpix;
  120.  
  121.  
  122.   /* Initialize the system-dependent method pointers. */
  123.   cinfo.methods = &c_methods;    /* links to method structs */
  124.   cinfo.emethods = &e_methods;
  125.   /* Here we use the default JPEG error handler, which will just print
  126.    * an error message on stderr and call exit().  See the second half of
  127.    * this file for an example of more graceful error recovery.
  128.    */
  129.   jselerror(&e_methods);    /* select std error/trace message routines */
  130.   /* Here we use the standard memory manager provided with the JPEG code.
  131.    * In some cases you might want to replace the memory manager, or at
  132.    * least the system-dependent part of it, with your own code.
  133.    */
  134.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  135.   /* If the compressor requires full-image buffers (for entropy-coding
  136.    * optimization or a noninterleaved JPEG file), it will create temporary
  137.    * files for anything that doesn't fit within the maximum-memory setting.
  138.    * (Note that temp files are NOT needed if you use the default parameters.)
  139.    * You can change the default maximum-memory setting by changing
  140.    * e_methods.max_memory_to_use after jselmemmgr returns.
  141.    * On some systems you may also need to set up a signal handler to
  142.    * ensure that temporary files are deleted if the program is interrupted.
  143.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  144.    * memory manager back end; it will try to grab extended memory for
  145.    * temp files, and that space will NOT be freed automatically.)
  146.    * See jcmain.c or jdmain.c for an example signal handler.
  147.    */
  148.  
  149.    if(MAXMEMORY_SET)
  150.      e_methods.max_memory_to_use = JPEG_MAXMEMORY * 1000L;
  151.    else
  152.      e_methods.max_memory_to_use = 50000;
  153.  
  154.   /* Here, set up pointers to your own routines for input data handling
  155.    * and post-init parameter selection.
  156.    */
  157.   c_methods.input_init = input_init;
  158.   c_methods.get_input_row = get_input_row;
  159.   c_methods.input_term = input_term;
  160.   c_methods.c_ui_method_selection = c_ui_method_selection;
  161.  
  162.   /* Set up default JPEG parameters in the cinfo data structure. */
  163.   if(QUALITY_SET)
  164.     j_c_defaults(&cinfo, JPEG_QUALITY, FALSE);
  165.   else
  166.     j_c_defaults(&cinfo, 75, FALSE);
  167.   /* Note: 75 is the recommended default quality level; you may instead pass
  168.    * a user-specified quality level.  Be aware that values below 25 will cause
  169.    * non-baseline JPEG files to be created (and a warning message to that
  170.    * effect to be emitted on stderr).  This won't bother our decoder, but some
  171.    * commercial JPEG implementations may choke on non-baseline JPEG files.
  172.    * If you want to force baseline compatibility, pass TRUE instead of FALSE.
  173.    * (If non-baseline files are fine, but you could do without that warning
  174.    * message, set e_methods.trace_level to -1.)
  175.    */
  176.  
  177.   /* At this point you can modify the default parameters set by j_c_defaults
  178.    * as needed.  For a minimal implementation, you shouldn't need to change
  179.    * anything.  See jcmain.c for some examples of what you might change.
  180.    */
  181.  
  182.   /* Select the input and output files.
  183.    * Note that cinfo.input_file is only used if your input reading routines
  184.    * use it; otherwise, you can just make it NULL.
  185.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  186.    * requires it in order to write binary files.
  187.    */
  188.  
  189.   cinfo.input_file = NULL;    /* if no actual input file involved */
  190.  
  191.   /*
  192.   if ((cinfo.output_file = fopen(filename, "wb")) == NULL) {
  193.     fprintf(stderr, "can't open %s\n", filename);
  194.     exit(1);
  195.   }
  196.   */
  197.  
  198.   cinfo.output_file = fout;
  199.   cinfo.image_width = w;
  200.   cinfo.image_height = h;
  201.   if(OPTIMIZE_JPEG)
  202.     cinfo.optimize_coding = TRUE;
  203.  
  204.   /* Here we go! */
  205.   jpeg_compress(&cinfo);
  206.  
  207.   /* That's it, son.  Nothin' else to do, except close files. */
  208.   /* Here we assume only the output file need be closed. */
  209.   /* fclose(cinfo.output_file); */
  210.  
  211.   /* Note: if you want to compress more than one image, we recommend you
  212.    * repeat this whole routine.  You MUST repeat the j_c_defaults()/alter
  213.    * parameters/jpeg_compress() sequence, as some data structures allocated
  214.    * in j_c_defaults are freed upon exit from jpeg_compress.
  215.    */
  216.  
  217. }
  218.  
  219.