home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gfx / jpegaga-1.0.lha / jpegAGA-1.0 / jpegAGA.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  21KB  |  592 lines

  1. /* jpegAGA written by Günther Röhrich                */
  2. /* this file is based on example.c, which is part of */
  3. /* the Independent JPEG Group's JPEG software        */
  4.  
  5.  
  6. #include "jinclude.h"
  7.  
  8.  
  9. /*
  10.  * <setjmp.h> is used for the optional error recovery mechanism shown in
  11.  * the second part of the example.
  12.  */
  13.  
  14. #include <setjmp.h>
  15. #include <signal.h> 
  16.  
  17. /* definitions for display.c */
  18.  
  19. #define HAM8 1
  20.  
  21. #ifdef __GNUC__
  22. #define MYSTRCMP strcasecmp
  23. #define MYSTRNCMP strncasecmp
  24. #else
  25. #define MYSTRCMP strcmp
  26. #define MYSTRNCMP strncmp
  27. #endif
  28.  
  29.  
  30. char  *ver = "\0$VER: jpegAGA 1.0 (4.6.94)";
  31. int VGAenable = 0;
  32. static int GrayEnable=0;
  33. static int BlockSmoothing=0;
  34. static FILE *ColorMapFile=NULL;
  35. char MapFileName[120];
  36.  
  37. extern int InitDisplay(int cols, int rows, unsigned long Mode, int NumPlanes);
  38. extern void SetDisplayColor(int ColorNumber, unsigned char r, unsigned char g, unsigned char b);
  39. extern void CloseDisplay(void);
  40. extern void DisplayRow(char *array, int cols);
  41. extern int CheckButton(void);
  42. extern void FinalWait(void);
  43. JSAMPROW OutputBuffer=NULL;
  44.  
  45. extern void EncodeHAM8(char *rorig, char *gorig, char *borig, char *yham, int xsize);
  46. unsigned short Mult_Table[2*256];
  47.  
  48.  
  49. /* NOTE: this array is in brgbrg order */
  50. /* when a mapfile is available it will be overwritten */
  51.  
  52. char *ColorCache;
  53. unsigned char ColorTable[64*3] =
  54.  { 0, 0, 0,  4, 4, 4,  8, 8, 8, 12,12,12,   
  55.   16,16,16, 20,20,20, 24,24,24, 28,28,28,  /* 16 colors */
  56.   32,32,32, 36,36,36, 41,41,41, 46,46,46,
  57.   51,51,51, 55,55,55, 59,59,59, 63,63,63,
  58.  
  59.  
  60.                       17,17,39, 17,17,55, /* 13 colors */ 
  61.   17,29,17,           17,29,39, 17,29,55, 
  62.   17,39,17, 17,39,29, 17,39,39, 17,39,55,
  63.   17,55,17, 17,55,39, 17,55,39, 17,55,55,
  64.  
  65.  
  66.             29,17,29, 29,17,39, 29,17,55, /* 11 colors */
  67.                                 29,29,55,
  68.   29,39,17, 29,39,29,           29,39,55,
  69.   29,55,17, 29,55,29, 29,55,39, 29,55,55,
  70.  
  71.  
  72.   39,17,17, 39,17,29, 39,17,39, 39,17,55, /* 12 colors */
  73.   39,29,17, 39,29,29,           39,29,55,
  74.   39,39,17, 39,39,29,          
  75.   39,55,17, 39,55,29,  
  76.  
  77.   
  78.   55,17,17, 55,17,29, 55,17,39, 55,17,55, /* 13 colors */
  79.   55,29,27, 55,29,29, 55,29,39, 55,29,55,
  80.   55,39,17, 55,39,29, 55,39,39, 
  81.   55,55,17, 55,55,29
  82. };
  83.  
  84.  
  85.  
  86.  
  87.  
  88. /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
  89.  
  90. /* This half of the example shows how to read data from the JPEG decompressor.
  91.  * It's a little more refined than the above in that we show how to do your
  92.  * own error recovery.  If you don't care about that, you don't need these
  93.  * next two routines.
  94.  */
  95.  
  96.  
  97. /*
  98.  * These routines replace the default trace/error routines included with the
  99.  * JPEG code.  The example trace_message routine shown here is actually the
  100.  * same as the standard one, but you could modify it if you don't want messages
  101.  * sent to stderr.  The example error_exit routine is set up to return
  102.  * control to read_JPEG_file() rather than calling exit().  You can use the
  103.  * same routines for both compression and decompression error recovery.
  104.  */
  105.  
  106. /* These static variables are needed by the error routines. */
  107. static jmp_buf setjmp_buffer;    /* for return to caller */
  108. static external_methods_ptr emethods; /* needed for access to message_parm */
  109.  
  110.  
  111. /* This routine is used for any and all trace, debug, or error printouts
  112.  * from the JPEG code.  The parameter is a printf format string; up to 8
  113.  * integer data values for the format string have been stored in the
  114.  * message_parm[] field of the external_methods struct.
  115.  */
  116.  
  117. METHODDEF void
  118. trace_message (const char *msgtext)
  119. {
  120.   fprintf(stderr, msgtext,
  121.       emethods->message_parm[0], emethods->message_parm[1],
  122.       emethods->message_parm[2], emethods->message_parm[3],
  123.       emethods->message_parm[4], emethods->message_parm[5],
  124.       emethods->message_parm[6], emethods->message_parm[7]);
  125.   fprintf(stderr, "\n");    /* there is no \n in the format string! */
  126. }
  127.  
  128. /*
  129.  * The error_exit() routine should not return to its caller.  The default
  130.  * routine calls exit(), but here we assume that we want to return to
  131.  * read_JPEG_file, which has set up a setjmp context for the purpose.
  132.  * You should make sure that the free_all method is called, either within
  133.  * error_exit or after the return to the outer-level routine.
  134.  */
  135.  
  136. void
  137. error_exit (const char *msgtext)
  138. {
  139.   trace_message(msgtext);    /* report the error message */
  140.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  141.   longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  142. }
  143.  
  144.  
  145.  
  146. /*
  147.  * To accept the image data from decompression, you must define four routines
  148.  * output_init, put_color_map, put_pixel_rows, and output_term.
  149.  *
  150.  * You must understand the distinction between full color output mode
  151.  * (N independent color components) and colormapped output mode (a single
  152.  * output component representing an index into a color map).  You should use
  153.  * colormapped mode to write to a colormapped display screen or output file.
  154.  * Colormapped mode is also useful for reducing grayscale output to a small
  155.  * number of gray levels: when using the 1-pass quantizer on grayscale data,
  156.  * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
  157.  * can regard the indexes are directly representing gray levels at reduced
  158.  * precision.  In any other case, you should not depend on the colormap
  159.  * entries having any particular order.
  160.  * To get colormapped output, set cinfo->quantize_colors to TRUE and set
  161.  * cinfo->desired_number_of_colors to the maximum number of entries in the
  162.  * colormap.  This can be done either in your main routine or in
  163.  * d_ui_method_selection.  For grayscale quantization, also set
  164.  * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
  165.  * (presently this is the default, but it may not be so in the future).
  166.  *
  167.  * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
  168.  * useful examples of what these routines should actually do, although each of
  169.  * them is encrusted with a lot of specialized code for its own file format.
  170.  */
  171.  
  172.  
  173. METHODDEF void
  174. output_init (decompress_info_ptr cinfo)
  175. /* This routine should do any setup required */
  176. {
  177.   /* This routine can initialize for output based on the data passed in cinfo.
  178.    * Useful fields include:
  179.    *    image_width, image_height    Pretty obvious, I hope.
  180.    *    data_precision            bits per pixel value; typically 8.
  181.    *    out_color_space            output colorspace previously requested
  182.    *    color_out_comps            number of color components in same
  183.    *    final_out_comps            number of components actually output
  184.    * final_out_comps is 1 if quantize_colors is true, else it is equal to
  185.    * color_out_comps.
  186.    *
  187.    * If you have requested color quantization, the colormap is NOT yet set.
  188.    * You may wish to defer output initialization until put_color_map is called.
  189.    */
  190.  
  191.   int DisplaySuccess, i;
  192.   
  193.   if(cinfo->out_color_space == CS_GRAYSCALE)
  194.   {
  195.     DisplaySuccess = InitDisplay(cinfo->image_width, cinfo->image_height, 0, 8);
  196.     if(DisplaySuccess != 1)
  197.     {
  198.       CloseDisplay();
  199.       error_exit("Could not open display!");
  200.     }
  201.     for(i=0; i<256; i++) SetDisplayColor(i, (unsigned char)i, (unsigned char)i, (unsigned char)i);
  202.   }
  203.   else
  204.   {
  205.     ColorCache = calloc(262145, 1);
  206.     if(ColorCache == NULL) error_exit("Out of memory.");
  207.  
  208.     /* create the multiplication table */
  209.     for(i=-255; i<256; i++) Mult_Table[i+255] = (unsigned short)(i*i);
  210.  
  211.     printf("Using HAM8-Mode");
  212.  
  213.     strcat(MapFileName, ".map");
  214.     ColorMapFile = fopen(MapFileName, "r");
  215.     if(!ColorMapFile)
  216.     {
  217.       int i = strlen(MapFileName) - 4;
  218.       while(i > 0 && MapFileName[i-1] != '.') i--;
  219.       if(MapFileName[i-1] == '.')
  220.       {
  221.         strcpy(&MapFileName[i], "map");
  222.         ColorMapFile = fopen(MapFileName, "r");
  223.       }
  224.     }
  225.  
  226.     if(ColorMapFile)
  227.     {
  228.       unsigned short MagicNumber;
  229.       unsigned int Reserved;
  230.  
  231.       if(fread(&MagicNumber, 2, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
  232.       if(MagicNumber != 0x1203) error_exit("Wrong mapfile header!");    
  233.       if(fread(&Reserved,    4, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
  234.       if(fread(ColorTable,  64*3, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
  235.  
  236.       printf(" wit