home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
gfx
/
jpegaga-1.0.lha
/
jpegAGA
/
jpegAGA.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-06-04
|
21KB
|
592 lines
/* jpegAGA written by Günther Röhrich */
/* this file is based on example.c, which is part of */
/* the Independent JPEG Group's JPEG software */
#include "jinclude.h"
/*
* <setjmp.h> is used for the optional error recovery mechanism shown in
* the second part of the example.
*/
#include <setjmp.h>
#include <signal.h>
/* definitions for display.c */
#define HAM8 1
#ifdef __GNUC__
#define MYSTRCMP strcasecmp
#define MYSTRNCMP strncasecmp
#else
#define MYSTRCMP strcmp
#define MYSTRNCMP strncmp
#endif
char *ver = "\0$VER: jpegAGA 1.0 (4.6.94)";
int VGAenable = 0;
static int GrayEnable=0;
static int BlockSmoothing=0;
static FILE *ColorMapFile=NULL;
char MapFileName[120];
extern int InitDisplay(int cols, int rows, unsigned long Mode, int NumPlanes);
extern void SetDisplayColor(int ColorNumber, unsigned char r, unsigned char g, unsigned char b);
extern void CloseDisplay(void);
extern void DisplayRow(char *array, int cols);
extern int CheckButton(void);
extern void FinalWait(void);
JSAMPROW OutputBuffer=NULL;
extern void EncodeHAM8(char *rorig, char *gorig, char *borig, char *yham, int xsize);
unsigned short Mult_Table[2*256];
/* NOTE: this array is in brgbrg order */
/* when a mapfile is available it will be overwritten */
char *ColorCache;
unsigned char ColorTable[64*3] =
{ 0, 0, 0, 4, 4, 4, 8, 8, 8, 12,12,12,
16,16,16, 20,20,20, 24,24,24, 28,28,28, /* 16 colors */
32,32,32, 36,36,36, 41,41,41, 46,46,46,
51,51,51, 55,55,55, 59,59,59, 63,63,63,
17,17,39, 17,17,55, /* 13 colors */
17,29,17, 17,29,39, 17,29,55,
17,39,17, 17,39,29, 17,39,39, 17,39,55,
17,55,17, 17,55,39, 17,55,39, 17,55,55,
29,17,29, 29,17,39, 29,17,55, /* 11 colors */
29,29,55,
29,39,17, 29,39,29, 29,39,55,
29,55,17, 29,55,29, 29,55,39, 29,55,55,
39,17,17, 39,17,29, 39,17,39, 39,17,55, /* 12 colors */
39,29,17, 39,29,29, 39,29,55,
39,39,17, 39,39,29,
39,55,17, 39,55,29,
55,17,17, 55,17,29, 55,17,39, 55,17,55, /* 13 colors */
55,29,27, 55,29,29, 55,29,39, 55,29,55,
55,39,17, 55,39,29, 55,39,39,
55,55,17, 55,55,29
};
/******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
/* This half of the example shows how to read data from the JPEG decompressor.
* It's a little more refined than the above in that we show how to do your
* own error recovery. If you don't care about that, you don't need these
* next two routines.
*/
/*
* These routines replace the default trace/error routines included with the
* JPEG code. The example trace_message routine shown here is actually the
* same as the standard one, but you could modify it if you don't want messages
* sent to stderr. The example error_exit routine is set up to return
* control to read_JPEG_file() rather than calling exit(). You can use the
* same routines for both compression and decompression error recovery.
*/
/* These static variables are needed by the error routines. */
static jmp_buf setjmp_buffer; /* for return to caller */
static external_methods_ptr emethods; /* needed for access to message_parm */
/* This routine is used for any and all trace, debug, or error printouts
* from the JPEG code. The parameter is a printf format string; up to 8
* integer data values for the format string have been stored in the
* message_parm[] field of the external_methods struct.
*/
METHODDEF void
trace_message (const char *msgtext)
{
fprintf(stderr, msgtext,
emethods->message_parm[0], emethods->message_parm[1],
emethods->message_parm[2], emethods->message_parm[3],
emethods->message_parm[4], emethods->message_parm[5],
emethods->message_parm[6], emethods->message_parm[7]);
fprintf(stderr, "\n"); /* there is no \n in the format string! */
}
/*
* The error_exit() routine should not return to its caller. The default
* routine calls exit(), but here we assume that we want to return to
* read_JPEG_file, which has set up a setjmp context for the purpose.
* You should make sure that the free_all method is called, either within
* error_exit or after the return to the outer-level routine.
*/
void
error_exit (const char *msgtext)
{
trace_message(msgtext); /* report the error message */
(*emethods->free_all) (); /* clean up memory allocation & temp files */
longjmp(setjmp_buffer, 1); /* return control to outer routine */
}
/*
* To accept the image data from decompression, you must define four routines
* output_init, put_color_map, put_pixel_rows, and output_term.
*
* You must understand the distinction between full color output mode
* (N independent color components) and colormapped output mode (a single
* output component representing an index into a color map). You should use
* colormapped mode to write to a colormapped display screen or output file.
* Colormapped mode is also useful for reducing grayscale output to a small
* number of gray levels: when using the 1-pass quantizer on grayscale data,
* the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
* can regard the indexes are directly representing gray levels at reduced
* precision. In any other case, you should not depend on the colormap
* entries having any particular order.
* To get colormapped output, set cinfo->quantize_colors to TRUE and set
* cinfo->desired_number_of_colors to the maximum number of entries in the
* colormap. This can be done either in your main routine or in
* d_ui_method_selection. For grayscale quantization, also set
* cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
* (presently this is the default, but it may not be so in the future).
*
* The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
* useful examples of what these routines should actually do, although each of
* them is encrusted with a lot of specialized code for its own file format.
*/
METHODDEF void
output_init (decompress_info_ptr cinfo)
/* This routine should do any setup required */
{
/* This routine can initialize for output based on the data passed in cinfo.
* Useful fields include:
* image_width, image_height Pretty obvious, I hope.
* data_precision bits per pixel value; typically 8.
* out_color_space output colorspace previously requested
* color_out_comps number of color components in same
* final_out_comps number of components actually output
* final_out_comps is 1 if quantize_colors is true, else it is equal to
* color_out_comps.
*
* If you have requested color quantization, the colormap is NOT yet set.
* You may wish to defer output initialization until put_color_map is called.
*/
int DisplaySuccess, i;
if(cinfo->out_color_space == CS_GRAYSCALE)
{
DisplaySuccess = InitDisplay(cinfo->image_width, cinfo->image_height, 0, 8);
if(DisplaySuccess != 1)
{
CloseDisplay();
error_exit("Could not open display!");
}
for(i=0; i<256; i++) SetDisplayColor(i, (unsigned char)i, (unsigned char)i, (unsigned char)i);
}
else
{
ColorCache = calloc(262145, 1);
if(ColorCache == NULL) error_exit("Out of memory.");
/* create the multiplication table */
for(i=-255; i<256; i++) Mult_Table[i+255] = (unsigned short)(i*i);
printf("Using HAM8-Mode");
strcat(MapFileName, ".map");
ColorMapFile = fopen(MapFileName, "r");
if(!ColorMapFile)
{
int i = strlen(MapFileName) - 4;
while(i > 0 && MapFileName[i-1] != '.') i--;
if(MapFileName[i-1] == '.')
{
strcpy(&MapFileName[i], "map");
ColorMapFile = fopen(MapFileName, "r");
}
}
if(ColorMapFile)
{
unsigned short MagicNumber;
unsigned int Reserved;
if(fread(&MagicNumber, 2, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
if(MagicNumber != 0x1203) error_exit("Wrong mapfile header!");
if(fread(&Reserved, 4, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
if(fread(ColorTable, 64*3, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
printf(" wit