home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libcnv / writejpg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.4 KB  |  123 lines

  1. /*
  2.  * Copyright (C) 1994-1995, Thomas G. Lane.
  3.  * This file is part of the Independent JPEG Group's software.
  4.  * For conditions of distribution and use, see the accompanying README file.
  5.  * This code was taken from an example file.
  6.  *
  7.  */
  8.  
  9. #include "xp_core.h" //used to make library compile faster on win32 do not ifdef this or it wont work
  10.  
  11. /*writejpg.c jpeg compression wrapper for jpeg compression utilities*/
  12. #include <stdio.h>
  13. #include <setjmp.h>/*for error handler*/
  14. #include "xp_core.h"/*defines of int32 ect*/
  15.  
  16. #include "ntypes.h" /* for MWContext to include libcnv.h*/
  17. #include "libcnv.h"
  18. #include "writejpg.h"
  19. #include "..\jpeg\jinclude.h"
  20. #include "..\jpeg\jpeglib.h"
  21. //#include "jerror.h"        /* get library error codes too */
  22.  
  23. /* incomming data= (RGB,RGB,RGB)*/
  24.  
  25. struct my_error_mgr {
  26.   struct jpeg_error_mgr pub;    /* "public" fields */
  27.  
  28.   jmp_buf setjmp_buffer;    /* for return to caller */ /*must include setjmp*/
  29. };
  30.  
  31. typedef struct my_error_mgr * my_error_ptr;
  32.  
  33.  
  34. /*
  35.  * Here's the routine that will replace the standard error_exit method:
  36.  */
  37.  
  38. void
  39. my_error_exit (j_common_ptr cinfo)
  40. {
  41.   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  42.   my_error_ptr myerr = (my_error_ptr) cinfo->err;
  43.  
  44.   /* Always display the message. */
  45.   /* We could postpone this until after returning, if we chose. */
  46.   (*cinfo->err->output_message) (cinfo);
  47.   /* Return control to the setjmp point */
  48.   longjmp(myerr->setjmp_buffer, 1);
  49. }
  50.  
  51. /*given an array of pixels, writes a jpeg file*/
  52. CONVERT_IMAGERESULT
  53. write_JPEG_file (CONVERT_IMG_ARRAY p_rowarray,CONVERT_IMGCONTEXT *output,CONVERT_IMG_INFO *p_imageinfo,CONVERT_CALLBACKS p_callbacks)
  54. {
  55.   struct jpeg_compress_struct cinfo;
  56.   struct my_error_mgr jerr;
  57.   int16 row_stride;        /* physical row width in image buffer */
  58.  
  59.   cinfo.err = jpeg_std_error((struct jpeg_error_mgr *)&jerr);
  60.  
  61.   jerr.pub.error_exit = my_error_exit;
  62.   jerr.pub.output_message=p_callbacks.m_displaybuffercallback;
  63.   /* Establish the setjmp return context for my_error_exit to use. */
  64.   if (setjmp(jerr.setjmp_buffer)) {
  65.   /* If we get here, the JPEG code has signaled an error.
  66.   * We need to clean up the JPEG object, close the input file, and return.
  67.     */
  68.     jpeg_close_file(&cinfo);
  69.     jpeg_destroy_compress(&cinfo);
  70.     return CONVERR_JPEGERROR;
  71.   }
  72.   /* Now we can initialize the JPEG compression object. */
  73.   jpeg_create_compress(&cinfo);
  74.  
  75.   /* Here we use the library-supplied code to send compressed data to a
  76.   * stdio stream.*/
  77.   jpeg_file_dest(&cinfo, output->m_filename);
  78.  
  79.   cinfo.image_width = p_imageinfo->m_image_width;     /* image width and height, in pixels */
  80.   cinfo.image_height = p_imageinfo->m_image_height;
  81.   cinfo.input_components = 3;        /* # of color components per pixel */
  82.   cinfo.in_color_space = JCS_RGB;     /* colorspace of input image */
  83.                                     /* Now use the library's routine to set default compression parameters.
  84.                                     * (You must set at least cinfo.in_color_space before calling this,
  85.                                     * since the defaults depend on the source color space.)
  86.   */
  87.   jpeg_set_defaults(&cinfo);
  88.   /* Now you can set any non-default parameters you wish to.
  89.   * Here we just illustrate the use of quality (quantization table) scaling:
  90.   */
  91.   jpeg_set_quality(&cinfo, output->m_quality, TRUE /* limit to baseline-JPEG values */);
  92.  
  93.   /* Step 4: Start compressor */
  94.  
  95.   /* TRUE ensures that we will write a complete interchange-JPEG file.
  96.   * Pass TRUE unless you are very sure of what you're doing.
  97.   */
  98.   jpeg_start_compress(&cinfo, TRUE);
  99.  
  100.   /* Step 5: while (scan lines remain to be written) */
  101.   /*           jpeg_write_scanlines(...); */
  102.  
  103.   /* Here we use the library's state variable cinfo.next_scanline as the
  104.   * loop counter, so that we don't have to keep track ourselves.
  105.   * To keep things simple, we pass one scanline per call; you can pass
  106.   * more if you wish, though.
  107.   */
  108.   row_stride = p_imageinfo->m_image_width * 3;    /* JSAMPLEs per row in image_buffer */
  109.  
  110.   while (cinfo.next_scanline < cinfo.image_height) 
  111.     (void) jpeg_write_scanlines(&cinfo, (JSAMPARRAY)p_rowarray, cinfo.image_height);
  112.  
  113.   /* Step 6: Finish compression */
  114.  
  115.   jpeg_finish_compress(&cinfo);
  116.  
  117.   /* Step 7: release JPEG compression object */
  118.   /* This is an important step since it will release a good deal of memory. */
  119.   jpeg_destroy_compress(&cinfo);
  120.   return CONV_OK;
  121. }
  122.  
  123.