home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
MM1
/
GRAPHICS
/
jpegshow.lzh
/
JPEGSHOW
/
jpegshow.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-09
|
5KB
|
215 lines
#include <stdio.h>
#include <modes.h>
typedef long size_t;
#include "jpeglib.h"
#include <setjmp.h>
#include "getargs.h"
#define TABSIZE (sizeof(Argtab)/sizeof(ARG))
#define SCREEN_WIDTH 384
#define SCREEN_HEIGHT 480
#define SCREEN_TYPE 9
int actwin, signum, pauselen = -1;
int iflag, xflag, yflag, scale = 1, sflag, vflag, wflag;
extern errno;
unsigned char *scraddr, *ptr;
struct jpeg_decompress_struct cinfo;
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
ARG Argtab[] =
{{'i', BOOLEAN, &iflag, "display header info only"},
{'p', INTEGER, &pauselen, "pause in secs (0 = no pause, -1= wait for user"},
{'s', INTEGER, &scale, "scale factor, must be 1, 2, 4 or 8"},
{'v', BOOLEAN, &vflag, "verbose"},
{'x', INTEGER, &xflag, "x offset in percent"},
{'y', INTEGER, &yflag, "y offset in percent"},
{'1', BOOLEAN, &sflag, "single pass (fast)"}};
typedef struct my_error_mgr *my_error_ptr;
unsigned char palette[3][256];
gotsignl(int signum)
{
if (signum != 2 && signum != 3)
return;
jpeg_destroy_decompress(&cinfo);
Select(0);
exit(signum);
}
void wait4user()
{
char c;
if (pauselen > 0)
sleep(pauselen);
else if (pauselen < 0) {
Bell(actwin);
do {
read(actwin, &c, 1);
} while (c != 13);
}
}
METHODDEF void my_error_exit(j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
GLOBAL int read_JPEG_file(char *filename)
{
struct my_error_mgr jerr;
FILE *infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
int i;
if ((infile = fopen(filename, "r")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return 0;
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
Select(0);
fclose(infile);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
cinfo.quantize_colors = TRUE;
cinfo.scale_num = 1;
cinfo.scale_denom = scale;
if (sflag)
cinfo.two_pass_quantize = FALSE;
jpeg_start_decompress(&cinfo);
if (iflag || vflag) {
fprintf(stderr, "output width x height = %d x %d\n",
cinfo.output_width, cinfo.output_height);
fprintf(stderr, "actual number of colors = %d\n", cinfo.actual_number_of_colors);
}
if (iflag) {
jpeg_destroy_decompress(&cinfo);
return 0;
}
xflag = xflag * cinfo.output_width / 100;
yflag = yflag * cinfo.output_height / 100;
for (i = 0; i < 256; i++)
Palette(actwin, i,
cinfo.colormap[0][i], cinfo.colormap[1][i], cinfo.colormap[2][i]);
row_stride = cinfo.output_width;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) & cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
put_scanline_someplace(buffer[0], row_stride);
}
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 1;
}
main(int argc, char **argv)
{
argc = getargs(argc, argv, Argtab, TABSIZE);
if (scale != 1 && scale != 2 && scale != 4 && scale != 8) {
fprintf(stderr, "scale = %d, must be 1, 2, 4 or 8 - ignored\n", scale);
scale = 1;
}
if ( xflag < 0 || xflag >= 100 ) {
fprintf(stderr, "x offset = %d, must be 0 - 100 %% - ignored\n", xflag);
xflag = 0;
}
if ( yflag < 0 || yflag >= 100 ) {
fprintf(stderr, "x offset = %d, must be 0 - 100 %% - ignored\n", yflag);
yflag = 0;
}
if (!iflag) {
actwin = open("/w", S_IWRITE | S_IREAD);
if (actwin == EOF)
exit(_errmsg(errno, "cannot open '/w'\n"));
if (DWSet(actwin, SCREEN_TYPE, 0, 0, SCREEN_WIDTH / 8, SCREEN_HEIGHT / 8,
0, 0, 0) == EOF)
exit(_errmsg(errno, "cannot DWSet\n"));
CurOff(actwin);
ScaleSw(actwin, 0);
Select(actwin);
scraddr = (unsigned char *) _gs_scadd(actwin);
if (scraddr == NULL)
exit(_errmsg(errno, "cannot _gs_scadd\n"));
} else
sflag = 1;
intercept(gotsignl);
while (--argc > 0) {
ptr = scraddr;
if ( !iflag )
Clear(actwin);
if ( read_JPEG_file(*++argv) == 0 )
continue;
wait4user();
}
Select(0);
}
put_scanline_someplace(unsigned char *a, int r_s)
{
register int i, count;
register unsigned char *dest, *src, *next;
r_s -= xflag;
if (cinfo.output_scanline < yflag ||
cinfo.output_scanline - yflag > SCREEN_HEIGHT / 2 || r_s < 0)
return;
count = (r_s < SCREEN_WIDTH) ? r_s : SCREEN_WIDTH;
i = count;
dest = ptr;
next = dest + SCREEN_WIDTH;
src = a + xflag;
while (i--)
*dest++ = *next++ = *src++;
ptr += SCREEN_WIDTH * 2;
}