home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / compresn / dvpeg / src / jvmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-10  |  29.7 KB  |  864 lines

  1. /*
  2.  * jvmain.c
  3.  
  4.  still have to figure out Cirrus card
  5.  can't force ahead b type
  6.  what happens if you force a mode and don't know size??? default to 1024 - dangerous ??
  7.  const declaration on number_VGA_cards did not work !!!!
  8.  
  9.  *
  10.  *  first generation JPG viewer
  11.  *
  12.  * This file contains a trivial test user interface for the JPEG decompressor.
  13.  * It should work on any system with Unix- or MS-DOS-style command lines.
  14.  *
  15.  * Two different command line styles are permitted, depending on the
  16.  * compile-time switch TWO_FILE_COMMANDLINE:
  17.  *    djpeg [options]  inputfile outputfile
  18.  *    djpeg [options]  [inputfile]
  19.  * In the second style, output is always to standard output, which you'd
  20.  * normally redirect to a file or pipe to some other program.  Input is
  21.  * either from a named file or from standard input (typically redirected).
  22.  * The second style is convenient on Unix but is unhelpful on systems that
  23.  * don't support pipes.  Also, you MUST use the first style if your system
  24.  * doesn't do binary I/O to stdin/stdout.
  25.  */
  26.  
  27. #include "jinclude.h"
  28. #ifdef INCLUDES_ARE_ANSI
  29. #include <stdlib.h>        /* to declare exit() */
  30. #endif
  31. #ifdef NEED_SIGNAL_CATCHER
  32. #include <signal.h>        /* to declare signal() */
  33. #endif
  34.  
  35.  
  36. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  37. #define READ_BINARY    "r"
  38. #define WRITE_BINARY    "w"
  39. #else
  40. #define READ_BINARY    "rb"
  41. #define WRITE_BINARY    "wb"
  42. #endif
  43.  
  44. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  45. #define EXIT_FAILURE  1
  46. #endif
  47.  
  48.  
  49. #include "jversion.h"        /* for version message */
  50.  
  51.  
  52. /*
  53.  * PD version of getopt(3).
  54.  */
  55.  
  56. #include "egetopt.c"
  57.  
  58.  
  59. extern    unsigned char cirrus, everex, paradise, tseng, trident;
  60. extern    unsigned char t8900, ativga, aheada, aheadb;
  61. extern    unsigned char oaktech, video7, chipstech, tseng4, genoa;
  62. extern    unsigned char ncr, compaq, vesa;
  63. extern    unsigned char vga512, vga1024;
  64. extern    int maxx, maxy;
  65.  
  66. #define number_VGA_cards 16        /* the number of VGA cards supported */
  67.  
  68. /*        definations for reading keyboard */
  69.  
  70. #define arrow_left 0x4b
  71. #define arrow_right 0x4d
  72. #define arrow_up 0x48
  73. #define arrow_down 0x50
  74. #define page_up 0x49
  75. #define page_down 0x51
  76. #define escape 27
  77. #define home 71
  78. #define end 79
  79.  
  80.  
  81. /*
  82.  * This list defines the known output image formats
  83.  * (not all of which need be supported by a given version).
  84.  * You can change the default output format by defining DEFAULT_FMT;
  85.  * indeed, you had better do so if you undefine PPM_SUPPORTED.
  86.  */
  87.  
  88. typedef enum {
  89.     FMT_GIF,        /* GIF format */
  90.     FMT_PPM,        /* PPM/PGM (PBMPLUS formats) */
  91.     FMT_RLE,        /* RLE format */
  92.     FMT_TARGA,        /* Targa format */
  93.     FMT_TIFF        /* TIFF format */
  94. } IMAGE_FORMATS;
  95.  
  96.  
  97. struct mode {
  98.     int x_size;
  99.     int y_size;
  100.     unsigned char mode_number;};
  101.  
  102. struct video_card {
  103.     char name[15];
  104.     struct mode vid_mode[5];
  105.     } video_cards[number_VGA_cards] =            /* 16 was number_VGA_cards but compiler blew */
  106.     { {"ahead", {{320, 200, 0x13}, {640, 400, 0x60}, {640, 480, 0x61}, {800, 600, 0x62}, {0000, 000, 0x00}}},
  107.       {"ati", {{320, 200, 0x13}, {640, 400, 0x5c}, {640, 480, 0x5d}, {800, 600, 0x5e}, {1024, 768, 0x62}}},
  108.       {"chips & tech", {{320, 200, 0x13}, {640, 400, 0x78}, {640, 480, 0x79}, {800, 600, 0x7b}, {0, 0, 0x0}}},
  109.       {"everex", {{320, 200, 0x13}, {640, 400, 0x14}, {512, 480, 0x15}, {640, 480, 0x30}, {800, 600, 0x31}}},
  110.       {"oak-tek", {{320, 200, 0x13}, {640, 480, 0x53}, {800, 600, 0x54}, {0, 0, 0x0}, {0, 0, 0x0}}},
  111.       {"genoa", {{320, 200, 0x13}, {640, 400, 0x7e}, {640, 480, 0x5c}, {720, 512, 0x5d}, {800, 600, 0x5e}}},
  112.       {"NCR", {{320, 200, 0x13}, {640, 400, 0x5e}, {640, 480, 0x5f}, {800, 600, 0x5c}, {0, 0, 0x0}}},
  113.       {"paradise", {{320, 200, 0x13}, {640, 400, 0x5e}, {640, 480, 0x5f}, {0, 0, 0x0}, {0, 0, 0x0}}},
  114.       {"trident", {{320, 200, 0x13}, {640, 400, 0x5c}, {640, 480, 0x5d}, {800, 600, 0x5e}, {0, 0, 0x00}}},
  115.       {"trident 8900", {{320, 200, 0x13}, {640, 400, 0x5c}, {640, 480, 0x5d}, {800, 600, 0x5e}, {1024, 768, 0x62}}},
  116.       {"tseng", {{320, 200, 0x13}, {640, 350, 0x2d}, {640, 480, 0x2e}, {800, 600, 0x30}, {1024, 768, 0x38}}},
  117.       {"tseng 4000", {{320, 200, 0x13}, {640, 400, 0x2f}, {640, 480, 0x2e}, {800, 600, 0x30}, {1024, 768, 0x38}}},
  118.       {"video 7", {{320, 200, 0x13}, {640, 400, 0x66}, {640, 480, 0x67}, {800, 600, 0x69}, {1024, 768, 0x6a}}},
  119.       {"cirrus", {{320, 200, 0x13}, {640, 480, 0x2e}, {0, 0, 0x0}, {0, 0, 0x0}, {0, 0, 0x0}}},
  120.       {"compaq", {{320, 200, 0x13}, {640, 480, 0x2e}, {0, 0, 0x0}, {0, 0, 0x0}, {0, 0, 0x0}}},
  121.       {"vesa", {{320, 200, 0x13}, {640, 480, 0x00}, {0, 0, 0x0}, {0, 0, 0x0}, {0, 0, 0x0}}}
  122.     };
  123.  
  124.  
  125. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  126. #define DEFAULT_FMT    FMT_PPM
  127. #endif
  128.  
  129.  
  130. /* These static variables are needed by the error routines. */
  131. /*static jmp_buf setjmp_buffer;    /* for return to caller */
  132. static external_methods_ptr emethods; /* needed for access to message_parm */
  133.  
  134. /* buffer pointer for panning if needed/possible */
  135. /*static big_sarray_ptr channel;
  136. /*static cur_output_row;            /* current output row */
  137. static big_sarray_ptr raw_pic_ptr;        /* pointer to virt. pic. for panning */
  138.  
  139.  
  140. /* these are for the graphics interface */
  141.  
  142.     unsigned char palbuf[256][3];
  143.     int svga;
  144.     unsigned int color;
  145.     int video_mode_used;        /* mode number index for card */
  146.     int card_id;                /* card id # - put as enum */
  147.     int forced_video;            /* forced video mode flag */
  148.     int forced_mode;            /* forced mode # ie 1 to 255 */
  149.     int enable_pan;            /* enough memory for panning && needed?? */
  150.     int shrink;                    /* 1 = normal, 2 = 1/2, 3 = 1/3 ... */
  151.  
  152. /*
  153.  * Signal catcher to ensure that temporary files are removed before aborting.
  154.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  155.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  156.  */
  157.  
  158. #ifdef NEED_SIGNAL_CATCHER
  159.  
  160. GLOBAL void
  161. signal_catcher (int signum)
  162. {
  163.   emethods->trace_level = 0;    /* turn off trace output */
  164.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  165.   exit(EXIT_FAILURE);
  166. }
  167.  
  168. #endif
  169.  
  170.  
  171. LOCAL void
  172. usage (char * progname)
  173. /* complain about bad command line */
  174. {
  175.   fprintf(stderr, "usage: %s ", progname);
  176.   fprintf(stderr, "[-P] [-V #] [-M #] [-S #] [-b] [-h] [-D] [-d] [-m mem]");
  177.   fprintf(stderr, " inputfile\n");
  178.   fprintf(stderr, " 1) Ahead           2) ATI VGA         3) Chips & Tech\n");
  179.   fprintf(stderr, " 4) Everex          5) Oak Tech.       6) Genoa\n");
  180.   fprintf(stderr, " 7) NCR             8) Paradise        9) Trident\n");
  181.   fprintf(stderr, "10) Trident 8900   11) Tseng, Orchid, Genoa, Willow\n");
  182.   fprintf(stderr, "12) Tseng 4000     13) Video 7        14) Cirrus\n");
  183.   fprintf(stderr, "15) Compaq         16) Vesa\n");
  184.   exit(EXIT_FAILURE);
  185. }
  186.  
  187.  
  188. LOCAL void
  189. vid_usage (char * progname)
  190. /* complain about bad video selection */
  191. {
  192.   fprintf(stderr, "usage: %s ", progname);
  193.   fprintf(stderr, "video cards:\n");
  194.   fprintf(stderr, " 1) Ahead           2) ATI VGA         3) Chips & Tech\n");
  195.   fprintf(stderr, " 4) Everex          5) Oak Tech.       6) Genoa\n");
  196.   fprintf(stderr, " 7) NCR             8) Paradise        9) Trident\n");
  197.   fprintf(stderr, "10) Trident 8900   11) Tseng, Orchid, Genoa, Willow\n");
  198.   fprintf(stderr, "12) Tseng 4000     13) Video 7        14) Cirrus\n");
  199.   fprintf(stderr, "15) Compaq         16) Vesa\n");
  200.   exit(EXIT_FAILURE);
  201. }
  202.  
  203.  
  204. LOCAL void
  205. help_usage ()
  206. {
  207.     fprintf(stderr, "-P   to disable panning (saves memory)\n");
  208.     fprintf(stderr, "-M   to force the video mode requires the BIOS mode # in dec.\n");
  209.     fprintf(stderr, "-V # force a certain video cards\n");
  210.     fprintf(stderr, "-b   use cross block smoothing\n");
  211.     fprintf(stderr, "-g   use grayscale output\n");
  212.     fprintf(stderr, "-D   supress dithering in quantization\n");
  213.     fprintf(stderr, "-m # set maximum memory available\n");
  214.     fprintf(stderr, "-d   enable debugging\n");
  215.     fprintf(stderr, "-S # shrink image to 1/# size\n");
  216.     fprintf(stderr, "Viewing funcions:\n");
  217.     fprintf(stderr, "<Pg Up>  brighten         <Pg Dn>  darken\n");
  218.     fprintf(stderr, "<up arrow>  scroll up     <dwn arrow>  scroll down\n");
  219.     fprintf(stderr, "<+> zoom                  <-> shrink image\n");
  220.     exit(EXIT_FAILURE);
  221. }
  222.  
  223.  
  224. /* This routine is used for any and all trace, debug, or error printouts
  225.  * from the JPEG code.  The parameter is a printf format string; up to 8
  226.  * integer data values for the format string have been stored in the
  227.  * message_parm[] field of the external_methods struct.
  228.  */
  229.  
  230. METHODDEF void
  231. trace_message (const char *msgtext)
  232. {
  233.   fprintf(stderr, msgtext,
  234.       emethods->message_parm[0], emethods->message_parm[1],
  235.       emethods->message_parm[2], emethods->message_parm[3],
  236.       emethods->message_parm[4], emethods->message_parm[5],
  237.       emethods->message_parm[6], emethods->message_parm[7]);
  238.   fprintf(stderr, "\n");    /* there is no \n in the format string! */
  239. }
  240.  
  241. /*
  242.  * The error_exit() routine should not return to its caller.  The default
  243.  * routine calls exit(), but here we assume that we want to return to
  244.  * read_JPEG_data, which has set up a setjmp context for the purpose.
  245.  * You should make sure that the free_all method is called, either within
  246.  * error_exit or after the return to the outer-level routine.
  247.  */
  248.  
  249. METHODDEF void
  250. error_exit (const char *msgtext)
  251. {
  252.   txtmode();
  253.   trace_message(msgtext);    /* report the error message */
  254.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  255. /*    fclose(cinfo.input_file);*/
  256.   exit(EXIT_FAILURE);
  257. /*  longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  258. }
  259.  
  260.  
  261. /*
  262.  * To accept the image data from decompression, you must define four routines
  263.  * output_init, put_color_map, put_pixel_rows, and output_term.
  264.  *
  265.  * You must understand the distinction between full color output mode
  266.  * (N independent color components) and colormapped output mode (a single
  267.  * output component representing an index into a color map).  You should use
  268.  * colormapped mode to write to a colormapped display screen or output file.
  269.  * Colormapped mode is also useful for reducing grayscale output to a small
  270.  * number of gray levels: when using the 1-pass quantizer on grayscale data,
  271.  * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
  272.  * can regard the indexes are directly representing gray levels at reduced
  273.  * precision.  In any other case, you should not depend on the colormap
  274.  * entries having any particular order.
  275.  * To get colormapped output, set cinfo->quantize_colors to TRUE and set
  276.  * cinfo->desired_number_of_colors to the maximum number of entries in the
  277.  * colormap.  This can be done either in your main routine or in
  278.  * d_ui_method_selection.  For grayscale quantization, also set
  279.  * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
  280.  * (presently this is the default, but it may not be so in the future).
  281.  *
  282.  * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
  283.  * useful examples of what these routines should actually do, although each of
  284.  * them is encrusted with a lot of specialized code for its own file format.
  285.  */
  286.  
  287.  
  288. METHODDEF void
  289. output_init (decompress_info_ptr cinfo)
  290. /* This routine should do any setup required */
  291. {
  292. int i;
  293.  
  294.     if (forced_mode == 0){
  295.         for (i = 0; i < 5; i++)        /* set default to maximum resolution */
  296.             if (video_cards[card_id].vid_mode[i].mode_number != 0)
  297.                 video_mode_used = i;
  298.  
  299.         for (i = 4; i >= 0; i--)
  300.             if (video_cards[card_id].vid_mode[i].x_size >= cinfo->image_width
  301.                     && video_cards[card_id].vid_mode[i].y_size >= cinfo->image_height
  302.                     &&    video_cards[card_id].vid_mode[i].x_size != 0)
  303.                 video_mode_used = i;
  304.         }
  305.  
  306. /* now check if panning would be usefull - only iff card is detected and size know */
  307.  
  308.     if (video_cards[card_id].vid_mode[video_mode_used].y_size > cinfo->image_height
  309.          && video_cards[card_id].vid_mode[video_mode_used].x_size > cinfo->image_width)
  310.          enable_pan = 0;
  311.  
  312.     if (enable_pan)
  313.         raw_pic_ptr = (*cinfo->emethods->request_big_sarray)
  314.             (cinfo->image_width, cinfo->image_height, 1L);
  315.  
  316.     printf("size of image = %li, %li\n", cinfo->image_width, cinfo->image_height);
  317.     printf("quantizing down to 256 colors\n");
  318.  
  319.  
  320.  
  321.   /* This routine can initialize for output based on the data passed in cinfo.
  322.     * Useful fields include:
  323.     *    image_width, image_height    Pretty obvious, I hope.
  324.     *    data_precision            bits per pixel value; typically 8.
  325.     *    out_color_space            output colorspace previously requested
  326.     *    color_out_comps            number of color components in same
  327.     *    final_out_comps            number of components actually output
  328.     * final_out_comps is 1 if quantize_colors is true, else it is equal to
  329.     * color_out_comps.
  330.     *
  331.     * If you have requested color quantization, the colormap is NOT yet set.
  332.     * You may wish to defer output initialization until put_color_map is called.
  333.     */
  334. }
  335.  
  336.  
  337.  
  338. /*
  339.  * This routine is called if and only if you have set cinfo->quantize_colors
  340.  * to TRUE.  It is given the selected colormap and can complete any required
  341.  * initialization.  This call will occur after output_init and before any
  342.  * calls to put_pixel_rows.  Note that the colormap pointer is also placed
  343.  * in a cinfo field, whence it can be used by put_pixel_rows or output_term.
  344.  * num_colors will be less than or equal to desired_number_of_colors.
  345.  *
  346.  * The colormap data is supplied as a 2-D array of JSAMPLEs, indexed as
  347.  *        JSAMPLE colormap[component][indexvalue]
  348.  * where component runs from 0 to cinfo->color_out_comps-1, and indexvalue
  349.  * runs from 0 to num_colors-1.  Note that this is actually an array of
  350.  * pointers to arrays rather than a true 2D array, since C does not support
  351.  * variable-size multidimensional arrays.
  352.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  353.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  354.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  355.  * machine has only signed chars.
  356.  */
  357.  
  358. METHODDEF void
  359. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  360. /* Write the color map */
  361. {
  362. int i;
  363.  
  364.     for (i=0; i < num_colors; i++)
  365.         if (colormap != NULL && num_colors <= 256){
  366.             if (cinfo->out_color_space == CS_RGB) {
  367.               /* Normal case: RGB color map */
  368.               palbuf[i][0] = (GETJSAMPLE(colormap[0][i]) >> 2);
  369.               palbuf[i][1] = (GETJSAMPLE(colormap[1][i]) >> 2);
  370.               palbuf[i][2] = (GETJSAMPLE(colormap[2][i]) >> 2);
  371.             }
  372.         }
  373.         else printf("color map pointer = NULL or > 256 colors.\n");
  374.  
  375.     if (forced_mode == 0)
  376.         svgamode(video_cards[card_id].vid_mode[video_mode_used].mode_number,
  377.                 video_cards[card_id].vid_mode[video_mode_used].x_size);
  378.     else
  379.         svgamode(forced_mode, 1024);        /* assume x_size */
  380.  
  381.     setmany(palbuf,0,256);
  382. }
  383.  
  384.  
  385. /*
  386.  * This function is called repeatedly, with a few more rows of pixels supplied
  387.  * on each call.  With the current JPEG code, some multiple of 8 rows will be
  388.  * passed on each call except the last, but it is extremely bad form to depend
  389.  * on this.  You CAN assume num_rows > 0.
  390.  * The data is supplied in top-to-bottom row order (the standard order within
  391.  * a JPEG file).  If you cannot readily use the data in that order, you'll
  392.  * need an intermediate array to hold the image.  See jwrrle.c for an example
  393.  * of outputting data in bottom-to-top order.
  394.  *
  395.  * The data is supplied as a 3-D array of JSAMPLEs, indexed as
  396.  *        JSAMPLE pixel_data[component][row][column]
  397.  * where component runs from 0 to cinfo->final_out_comps-1, row runs from 0 to
  398.  * num_rows-1, and column runs from 0 to cinfo->image_width-1 (column 0 is
  399.  * left edge of image).  Note that this is actually an array of pointers to
  400.  * pointers to arrays rather than a true 3D array, since C does not support
  401.  * variable-size multidimensional arrays.
  402.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  403.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  404.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  405.  * machine has only signed chars.
  406.  *
  407.  * If quantize_colors is true, then there is only one component, and its values
  408.  * are indexes into the previously supplied colormap.  Otherwise the values
  409.  * are actual data in your selected output colorspace.
  410.  */
  411.  
  412.  
  413. METHODDEF void
  414. put_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
  415. /* Write some rows of output data */
  416. {
  417.   /* This example shows how you might write full-color RGB data (3 components)
  418.     * to an output file in which the data is stored 3 bytes per pixel.
  419.     */
  420. register FILE * outfile = cinfo->output_file;
  421. register JSAMPROW ptr0, output_row;
  422. static  long col;
  423. static  int row, gr_col;
  424. static  unsigned int gr_row = 0;  /* warning 1 shot system here -----------*/
  425. static  unsigned int read_row = 0;            /* more 1 shot */
  426. static    int col_cntr=1, row_cntr=1;                /* warning more 1 shot variables */
  427.  
  428. /* note ------ row is not going from 0 to max because it goes in sections ------*/
  429.  
  430. for (row = 0; row < num_rows; row++) {
  431.     ptr0 = pixel_data[0][row];
  432.     if (enable_pan) output_row = *((*cinfo->emethods->access_big_sarray)
  433.                             (raw_pic_ptr, read_row++, TRUE));
  434.     for (gr_col = col = 0; col < cinfo->image_width; col++){
  435.         if (enable_pan) *output_row++ = GETJSAMPLE(*ptr0);
  436.         if (gr_row < maxy && gr_col < maxx)
  437.             if (row_cntr == 1 && col_cntr == 1)
  438.                 point(gr_col++, gr_row, GETJSAMPLE(*ptr0));
  439.         ptr0++;
  440.         col_cntr = (col_cntr % shrink) + 1;
  441.         }
  442.     if (row_cntr == 1) gr_row++;
  443.     row_cntr = (row_cntr % shrink) + 1;
  444.     }
  445. }
  446.  
  447.  
  448.  
  449. METHODDEF void
  450. output_term (decompress_info_ptr cinfo)
  451. /* Finish up at the end of the output */
  452. {
  453. JSAMPROW output_row;        /* pseudo struct to hold 1 line for drawing */
  454.  
  455. static int
  456.         exit,                    /* exit flag ie on escape */
  457.         x_pos, y_pos,        /*    current upper left x, y position */
  458.         x_max, y_max,        /* maximum video x, y max (counting from 1 */
  459.         x_size, y_size,    /* picture size */
  460.         x, y,                    /* drawing counters */
  461.         old_color_scale,    /* old one to see if its changed */
  462.         color_scale,        /* factor to scale pallete up or down */
  463.         row, col,
  464.         y_delta, x_delta,
  465.         redraw,                /* cmd to force redraw */
  466.         cmd;
  467.  
  468.  
  469. static unsigned char    pallet[256][3];    /* duplicate pallete */
  470.  
  471. x_size = cinfo->image_width;
  472. y_size = cinfo->image_height;
  473. x_max = video_cards[card_id].vid_mode[video_mode_used].x_size;
  474. y_max = video_cards[card_id].vid_mode[video_mode_used].y_size;
  475. x_pos = y_pos = exit = color_scale = redraw = 0;
  476.  
  477. while(exit == 0){
  478.     old_color_scale = color_scale;
  479.     y_delta = x_delta = redraw = 0;
  480.     cmd = getch();
  481.     if (cmd == 0) cmd = getch();
  482.     switch (cmd){
  483.         case 45:            shrink++;    /* - key */
  484.                                 if (shrink > 4) shrink = 4;    /* limit to 1/4 size */
  485.                                 else redraw = 1;
  486.                                 break;
  487.         case 43:            shrink--;        /* + key */
  488.                                 if (shrink < 1) shrink = 1;    /* limit to 1:1 since can't zoom */
  489.                                 else redraw = 1;
  490.                                 break;
  491.         case page_up:        color_scale += 2;            /* turn up intensity */
  492.                                 break;
  493.         case page_down:    color_scale--;                /* turn down intensity */
  494.                                 break;
  495.         case arrow_up:        if (y_pos - 15 >= 0)
  496.                                     y_delta = -15;
  497.                                 else
  498.                                     y_delta = -y_pos;        /* move all the way to top */
  499.                                 break;
  500.         case arrow_down:    if (y_pos + 15 + y_max <= y_size)
  501.                                     y_delta = 15;
  502.                                 else
  503.                                     y_delta = y_max - (y_size - y_pos);
  504.                                 break;
  505.         case arrow_left:    if (x_pos - 15 >= 0)
  506.                                     x_delta = -15;
  507.                                 else
  508.                                     x_delta = -x_pos;
  509.                                 break;
  510.         case arrow_right:    if (x_pos + 15 + x_max <= x_size)
  511.                                     x_delta = 15;
  512.                                 else
  513.                                     x_delta = x_max - (x_size - x_pos);
  514.                                 break;
  515.         case escape:        exit = 1;
  516.                                 break;
  517. /*        default:                exit = 1;*/
  518.         }
  519.     if (exit == 0){
  520.         if (color_scale != old_color_scale){
  521. /*            color_scale %= 200;    /* mod to 100 - ie it will wrap !! */
  522.             if (color_scale > 100) color_scale = 100;
  523.             if (color_scale < -40) color_scale = -40;
  524.             for (x=0; x < 256; x++){
  525.                 pallet[x][0] = palbuf[x][0] * (1 + (float)color_scale / 40);
  526.                 pallet[x][1] = palbuf[x][1] * (1 + (float)color_scale / 40);
  527.                 pallet[x][2] =    palbuf[x][2] * (1 + (float)color_scale / 40);
  528.                 }
  529.             setmany(pallet, 0, 256);
  530.             }
  531.  
  532.         if (enable_pan && ( x_delta != 0 || y_delta != 0 || redraw)){
  533.             x_pos += x_delta;
  534.             y_pos += y_delta;
  535.             if (shrink == 1){
  536.                 for (y=0; y < y_max; y++){
  537.                     output_row = *((*cinfo->emethods->access_big_sarray)
  538.                             (raw_pic_ptr, y+y_pos, FALSE));
  539.                     for (x = 0; x < x_size; x++)
  540.                         if (x > x_pos)
  541.                             point(x-x_pos, y, *output_row++);
  542.                         else
  543.                             *output_row++;
  544.                     }
  545.                 }
  546.             else{        /* now do a shrunk picture */
  547.                 for (y=0; y < y_max; y++){
  548.                     row = y * shrink + y_pos;
  549.                     if (row < y_size){
  550.                         output_row = *((*cinfo->emethods->access_big_sarray)
  551.                                 (raw_pic_ptr, row, FALSE));
  552.                         for (x = 0; x < x_size; x++){
  553.                             col = x * shrink + x_pos;
  554.                             if (x > x_pos && col < x_size)
  555.                                 point(x-x_pos, y, *output_row++);
  556.                             else{
  557.                                 *output_row++;
  558.                                 if (x <= x_pos) point(x-x_pos, y, 0);        /* black ??  if shrouded */
  559.                                 }
  560.                             *output_row++;        /* since its shrunk at least 1 times */
  561.                             if (shrink >= 3) *output_row++;
  562.                             if (shrink == 4) *output_row++;
  563.                             }
  564.                         }
  565.                     }
  566.                 }
  567.             }
  568.         }
  569.   }
  570.   /* This termination routine may not need to do anything. */
  571.   /* Note that the JPEG code will only call it during successful exit; */
  572.   /* if you want it called during error exit, you gotta do that yourself. */
  573. }
  574.  
  575.  
  576.  
  577.  
  578. /*
  579.  * This routine gets control after the JPEG file header has been read;
  580.  * at this point the image size and colorspace are known.
  581.  * The routine must determine what output routines are to be used, and make
  582.  * any decompression parameter changes that are desirable.  For example,
  583.  * if it is found that the JPEG file is grayscale, you might want to do
  584.  * things differently than if it is color.  You can also delay setting
  585.  * quantize_colors and associated options until this point.
  586.  *
  587.  * j_d_defaults initializes out_color_space to CS_RGB.  If you want grayscale
  588.  * output you should set out_color_space to CS_GRAYSCALE.  Note that you can
  589.  * force grayscale output from a color JPEG file (though not vice versa).
  590.  */
  591.  
  592. METHODDEF void
  593. d_ui_method_selection (decompress_info_ptr cinfo)
  594. {
  595.   /* if grayscale input, force grayscale output; */
  596.   /* else leave the output colorspace as set by main routine. */
  597.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  598.      cinfo->out_color_space = CS_GRAYSCALE;
  599.  
  600.   /* select output routines */
  601.   cinfo->methods->output_init = output_init;
  602.   cinfo->methods->put_color_map = put_color_map;
  603.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  604.   cinfo->methods->output_term = output_term;
  605.  
  606.   /* select output file format */
  607.   /* Note: jselwxxx routine may make additional parameter changes,
  608.     * such as forcing color quantization if it's a colormapped format.
  609.     */
  610. /*  switch (requested_fmt) {
  611. #ifdef GIF_SUPPORTED
  612.   case FMT_GIF:
  613.      jselwgif(cinfo);
  614.      break;
  615. #endif
  616. #ifdef PPM_SUPPORTED
  617.   case FMT_PPM:
  618.      jselwppm(cinfo);
  619.      break;
  620. #endif
  621. #ifdef RLE_SUPPORTED
  622.   case FMT_RLE:
  623.      jselwrle(cinfo);
  624.      break;
  625. #endif
  626.   default:
  627.      ERREXIT(cinfo->emethods, "Unsupported output file format");
  628.      break;
  629.   }*/
  630. }
  631.  
  632.  
  633.  
  634.  
  635. /*
  636.  * The main program.
  637.  */
  638.  
  639. GLOBAL int
  640. main (int argc, char **argv)
  641. {
  642.   struct decompress_info_struct cinfo;
  643.   struct decompress_methods_struct dc_methods;
  644.   struct external_methods_struct e_methods;
  645.   int c;
  646.   int v_mod;
  647.  
  648.   /* Initialize the system-dependent method pointers. */
  649.   cinfo.methods = &dc_methods;
  650.   cinfo.emethods = &e_methods;
  651.   jselerror(&e_methods);    /* error/trace message routines */
  652.   jselmemmgr(&e_methods);    /* memory allocation routines */
  653.   dc_methods.d_ui_method_selection = d_ui_method_selection;
  654.  
  655.     forced_video = forced_mode = 0;            /* flag for forced video mode */
  656.     enable_pan = 1;    /* default to on */
  657.     shrink = 1;            /* default to normal size */
  658.  
  659.   /* Now OK to enable signal catcher. */
  660. #ifdef NEED_SIGNAL_CATCHER
  661.   emethods = &e_methods;
  662.   signal(SIGINT, signal_catcher);
  663. #ifdef SIGTERM            /* not all systems have SIGTERM */
  664.   signal(SIGTERM, signal_catcher);
  665. #endif
  666. #endif
  667.  
  668.   /* Set up default JPEG parameters. */
  669.   j_d_defaults(&cinfo, TRUE);
  670. /*  requested_fmt = DEFAULT_FMT;    /* set default output file format */
  671.  
  672. /* for viewer force quantization and 256 colors */
  673.     cinfo.desired_number_of_colors = 256;
  674.     cinfo.quantize_colors = TRUE;
  675.     cinfo.two_pass_quantize = TRUE;
  676.  
  677.   /* Scan command line options, adjust parameters */
  678.  
  679.   while ((c = egetopt(argc, argv, "S:PV:M:bgDm:dh")) != EOF)
  680.      switch (c) {
  681.      case 'S':                /* allow a shrink to 1/# of the image */
  682.         if (optarg == NULL){
  683.             help_usage();
  684.             usage(argv[0]);
  685.             }
  686.         if (sscanf(optarg, "%d", &shrink) < 1);
  687.         else shrink = 2;
  688.         if (shrink > 4 || shrink < 1){
  689.             help_usage();
  690.             usage(argv[0]);
  691.             }
  692.         break;
  693.      case 'P':            /* disable panning */
  694.         enable_pan = 0;
  695.         break;
  696.      case 'V':            /* force a video card and _assume_ card exists */
  697.         if (optarg == NULL)
  698.             usage(argv[0]);
  699.         if (sscanf(optarg, "%d", &v_mod) < 1)
  700.             vid_usage(argv[0]);
  701.         if (v_mod > number_VGA_cards || v_mod < 1)        /* check valid card # */
  702.             vid_usage(argv[0]);
  703.         forced_video = 1;
  704.         card_id = v_mod - 1;
  705.         break;
  706.      case 'M':            /* for a video mode */
  707.         if (optarg == NULL)
  708.             usage(argv[0]);
  709.         if (sscanf(optarg, "%d", &v_mod) < 1)
  710.             vid_usage(argv[0]);
  711.         if (v_mod > 255 || v_mod < 1)        /* check valid card # */
  712.             usage(argv[0]);
  713.         forced_mode = v_mod;
  714.         break;
  715.      case 'b':            /* Enable cross-block smoothing. */
  716.         cinfo.do_block_smoothing = TRUE;
  717.         break;
  718.      case 'g':            /* Force grayscale output. */
  719.         cinfo.out_color_space = CS_GRAYSCALE;
  720.         break;
  721.      case 'h':
  722.         help_usage();
  723.         break;
  724.      case 'D':            /* Suppress dithering in color quantization. */
  725.         cinfo.use_dithering = FALSE;
  726.         break;
  727.      case 'm':            /* Maximum memory in Kb (or Mb with 'm'). */
  728.         { long lval;
  729.     char ch = 'x';
  730.  
  731.     if (optarg == NULL)
  732.       usage(argv[0]);
  733.     if (sscanf(optarg, "%ld%c", &lval, &ch) < 1)
  734.       usage(argv[0]);
  735.     if (ch == 'm' || ch == 'M')
  736.       lval *= 1000L;
  737.     e_methods.max_memory_to_use = lval * 1000L;
  738.         }
  739.         break;
  740.      case 'd':            /* Debugging. */
  741.         e_methods.trace_level++;
  742.         break;
  743.      case '?':
  744.      default:
  745.         usage(argv[0]);
  746.         break;
  747.      }
  748.  
  749.   /* If -d appeared, print version identification */
  750.   if (e_methods.trace_level > 0)
  751.      fprintf(stderr, "Erics DVPEG viewer, thanks to:\n");
  752.      fprintf(stderr, "Independent JPEG Group, version %s\n%s\n",
  753.          JVERSION, JCOPYRIGHT);
  754.  
  755.  /* Select the input and output files */
  756.  /* not TWO_FILE_COMMANDLINE -- use Unix style */
  757.  
  758.   cinfo.input_file = stdin;    /* default input file */
  759.   cinfo.output_file = stdout;    /* always the output file */
  760.  
  761.     if (optind != argc-1) {
  762.         fprintf(stderr, "%s: need one input file\n", argv[0]);
  763.         usage(argv[0]);
  764.         }
  765.  
  766.     if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  767.         fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  768.         exit(EXIT_FAILURE);
  769.         }
  770.  
  771.  
  772.   /* Set up to read a JFIF or baseline-JPEG file. */
  773.   /* A smarter UI would inspect the first few bytes of the input file */
  774.   /* to determine its type. */
  775. #ifdef JFIF_SUPPORTED
  776.   jselrjfif(&cinfo);
  777. #else
  778.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  779. #endif
  780.  
  781. /*
  782.  * OK, here is the main function that actually causes everything to happen.
  783.  * We assume here that the JPEG filename is supplied by the caller of this
  784.  * routine, and that all decompression parameters can be default values.
  785.  * The routine returns 1 if successful, 0 if not.
  786.  */
  787.  
  788.   cinfo.output_file = NULL;    /* if no actual output file involved */
  789.  
  790.   /* Here we supply our own error handler; compare to use of standard error
  791.     * handler in the previous write_JPEG_file example.
  792.     */
  793.   emethods = &e_methods;    /* save struct addr for possible access */
  794.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  795.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  796.  
  797.  
  798.   /* If the decompressor requires full-image buffers (for two-pass color
  799.     * quantization or a noninterleaved JPEG file), it will create temporary
  800.     * files for anything that doesn't fit within the maximum-memory setting.
  801.     * You can change the default maximum-memory setting by changing
  802.     * e_methods.max_memory_to_use after jselmemmgr returns.
  803.     * On some systems you may also need to set up a signal handler to
  804.     * ensure that temporary files are deleted if the program is interrupted.
  805.     * (This is most important if you are on MS-DOS and use the jmemdos.c
  806.     * memory manager back end; it will try to grab extended memory for
  807.     * temp files, and that space will NOT be freed automatically.)
  808.     * See jcmain.c or jdmain.c for an example signal handler.
  809.     */
  810.  
  811.  
  812. /* now check the video display */
  813.  
  814.     if (forced_video){
  815.         forcevga(card_id+1);
  816.         svga = 1;
  817.         }
  818.     else{
  819.         svga=whichvga();
  820.  
  821.         if (aheada == 1 || aheadb == 1) card_id = 0;
  822.         if (ativga == 1) card_id = 1;
  823.         if (chipstech == 1) card_id = 2;
  824.         if (everex == 1) card_id = 3;
  825.         if (oaktech == 1) card_id = 4;
  826.         if (genoa == 1) card_id = 5;
  827.         if (ncr == 1) card_id = 6;
  828.         if (paradise == 1) card_id = 7;
  829.         if (trident == 1)
  830.         if (t8900 == 1) card_id = 9;
  831.             else card_id = 8;
  832.         if (tseng == 1)
  833.             if (tseng4 == 1) card_id = 11;
  834.             else card_id = 10;
  835.         if (video7 == 1) card_id = 12;
  836.         if (cirrus == 1) card_id = 13;
  837.         if (compaq == 1) card_id = 14;
  838.         if (vesa == 1) card_id = 15;
  839.     }
  840.  
  841.  
  842.  
  843.   /* At this point you can modify the default parameters set by j_d_defaults
  844.     * as needed; for example, you can request color quantization or force
  845.     * grayscale output.  See jdmain.c for examples of what you might change.
  846.     */
  847.  
  848.  
  849.   /* Here we go! */
  850.   if (svga)
  851.         jpeg_decompress(&cinfo);
  852.   else
  853.         printf("The video card is not a recognized 256 color card.\n");
  854.  
  855.   /* That's it, son.  Nothin' else to do, except close files. */
  856.   /* Here we assume only the input file need be closed. */
  857.   fclose(cinfo.input_file);
  858.  
  859.   txtmode();
  860.     printf("video card: %s\n",video_cards[card_id].name);
  861.  
  862.   return 1;            /* indicate success */
  863. }
  864.