home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / FORMATS / SHOW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-26  |  7.7 KB  |  329 lines

  1. // This program is graphics file format shower.
  2. // We can show you formats :
  3. //    TGA (TARGA)
  4. //      GIF (CompuServe)
  5. //    PCX (Zsoft)
  6. //    BMP (DIB) (Microsoft Windows bitmap)
  7. // MONO & 16 colors pictures are displaied in standart VGA 16 colors video mode
  8. // 256 & Gray - in VGA and SVGA 256 colors modes
  9. //
  10. //        you SVGA must have VESA BIOS extention !!!
  11. //
  12. // To display True Color pictures we use color quantization and
  13. //    show them in SVGA 256 colors modes
  14. //
  15. // Written by Podvoysky E. & Kiselev J. CZ 1994.
  16.  
  17. #pragma -ml
  18.  
  19. #include <conio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <dir.h>
  23. #include <mem.h>
  24. #include <dos.h>
  25.  
  26. #include "graph.h"
  27. #include "formats.h"
  28. #include "formconv.h"
  29. #include "palette.h"
  30. #include "mapper.h"
  31. #include "mapper2.h"
  32. #include "quant.h"
  33. #include "keymouse.h"
  34.  
  35. VGApalette pal;
  36. BGRpalette BGRpal;
  37.  
  38. graph_file_reader *grf_r;
  39. image_keeper *keeper = NULL;
  40.  
  41. void init_keeper(image_type im_type, BOOL force ) {
  42.  
  43.     if  (force || (getmaxx()+1 < grf_r -> width) ||
  44.         (getmaxy()+1 < grf_r -> height)) {
  45.  
  46.         if (!EXM_initialized)  manager_initialize();
  47.         keeper = new    image_keeper(UNKNOWN,
  48.             im_type,
  49.             grf_r -> width,
  50.             grf_r -> height,
  51.             grf_r -> palette,
  52.             grf_r -> top_to_bottom);
  53.         if (keeper == NULL) return;
  54.         if (! keeper -> file_ok) {
  55.             delete keeper;
  56.             keeper = NULL;
  57.         }
  58. /*        if (keeper -> where == DISK) {
  59.             delete keeper;
  60.             keeper = NULL;
  61.         }
  62. */    }
  63. }
  64.  
  65. void show_picture(int x0, int y0) {
  66.     int i, y, width;
  67.     BYTE *tmp;
  68.  
  69.     width = min(keeper->width, getmaxx()+1);
  70.     for  (i = getmaxy(), y = y0 + getmaxy(); i >= 0; i--, y--) {
  71.         tmp = keeper->give_line(y);
  72.         putrow( (rowptr) (tmp+x0), 0, i, width);
  73.     }
  74. }
  75.  
  76. #define MOUSE_SENSITIVITY 3
  77. #define KEY_STEP 3
  78. void scroll_picture() {
  79.     int x0 = 0, y0 = 0,  xold = 0, yold = 0;
  80.     int maxx0, maxy0;
  81.     int step;
  82.  
  83.     WORD key;
  84.     int d_mousex, d_mousey;
  85.     BOOL  fin = FALSE, shft;
  86.  
  87.     initmouse_easy();
  88.     setmouserange(0,0,getmaxx(),getmaxy());
  89.  
  90.     if (kbhit() && (readkeyword() == kwEsc)) return;
  91.     clearkey();
  92.  
  93.     if (getmaxx() < 350) step = KEY_STEP; else step = 2*KEY_STEP;
  94.     maxx0 = keeper->width-1 - getmaxx();
  95.     maxy0 = keeper->height-1 - getmaxy();
  96.  
  97.     do { // while (!fin)
  98.          setmousepos( getmaxx()/2, getmaxy()/2);
  99.          d_mousex = d_mousey = 0;
  100.  
  101.          while ((!kbhit()) && (d_mousex == 0) && (d_mousey == 0)) {
  102.             d_mousex = (mousex - getmaxx()/2)/MOUSE_SENSITIVITY;
  103.             d_mousey = (mousey - getmaxy()/2)/MOUSE_SENSITIVITY;
  104.             }
  105.  
  106.  
  107.          if (kbhit()) {
  108.         key = readkeyword();
  109.         shft = keyboardstatus() & 3; // was shift pressed?
  110.  
  111.         switch (key) { // when shift key is unimportant
  112.             case kwHome:
  113.             case kwShiftHome:   x0 = 0; break;
  114.  
  115.             case kwEnd:
  116.             case kwShiftEnd: x0 = maxx0; break;
  117.  
  118.             case kwPgUp:
  119.             case kwShiftPgUp:   y0 -= getmaxy()/2; break;
  120.  
  121.  
  122.             case kwPgDn:
  123.             case kwShiftPgDn:  y0 += getmaxy()/2; break;
  124.         }
  125.  
  126.         if (shft) switch (key) {
  127.             case kwLeft:
  128.             case kwShiftLeft: x0 -= getmaxx()/2; break; // NumLock may cause kwShiftLeft
  129.  
  130.             case kwRight:
  131.             case kwShiftRight: x0 += getmaxx()/2; break;
  132.  
  133.             case kwUp:
  134.             case kwShiftUp:  y0 -= 5*step;   break;
  135.  
  136.             case kwDown:
  137.             case kwShiftDown: y0 += 5*step;  break;
  138.         }
  139.         else switch (key) {
  140.             case kwLeft:
  141.             case kwShiftLeft: x0 -= step; break; // NumLock may cause kwShiftLeft
  142.  
  143.             case kwRight:
  144.             case kwShiftRight: x0 += step; break;
  145.  
  146.             case kwUp:
  147.             case kwShiftUp:  y0 -= step;   break;
  148.  
  149.             case kwDown:
  150.             case kwShiftDown: y0 += step;  break;
  151.  
  152.         }
  153.  
  154.         switch (key) {
  155.             case kwEsc:
  156.             case kwAltX:
  157.             case kwF4:
  158.             case kwF10:
  159.             case kwEnter:
  160.             case kwSpace:
  161.             case kwCtrlBreak:      fin = TRUE;
  162.         }
  163.          }
  164.          else { // not kbhit, mouse moved
  165.              x0 += d_mousex*step;
  166.              y0 += d_mousey*step;
  167.          }
  168.  
  169.          if (!fin) {
  170.                      x0 = min(x0, maxx0);
  171.              y0 = min(y0, maxy0);
  172.              x0 = max(x0, 0);
  173.              y0 = max(y0, 0);
  174.  
  175.              if ((xold - x0) || (yold - y0)) show_picture(x0,y0);
  176.              xold = x0; yold = y0;
  177.          }
  178.     }
  179.     while (!fin);
  180.  
  181. }
  182.  
  183. void main(int argc, char *argv[]) {
  184.     int i,j;
  185.     int inc_line,currline, cols;
  186.     BYTE *dest, *tmp , *dest2;
  187.     // color_selector *selector;
  188.     // color_mapper *mapper;
  189.     color_reductor *reductor = NULL;
  190.  
  191.     if(argc < 2) {
  192.     printf("usage : shower graphfile \n");
  193.     printf("for example :\n");
  194.     printf("    SHOWER WORLD.TGA \n");
  195.     printf(" supported formats: TGA,GIF,PCX,BMP (DIB) \n");
  196.     exit(0);
  197.     }
  198.  
  199.     minheap = 110000L;
  200.     grf_r = open_image_file(argv[1]);
  201.     if (grf_r==NULL) {printf("%s\n",format_file_error_report()); exit(0);}
  202.  
  203.     if(grf_r->top_to_bottom) {currline = 0;inc_line = 1;}
  204.     else {currline = grf_r->height-1;inc_line =-1;}
  205.  
  206.     dest = new BYTE [grf_r->width+1];
  207.  
  208.     if (graphinit() < 0) {printf("error graphics initializing."); exit(0);}
  209.  
  210.     switch(grf_r->source_type) {
  211.     case MONOIMG:
  212.         for(i=grf_r->height-1;i>=0;i--) {
  213.             grf_r->get_next_line();
  214.             if(grf_r->source_ext_type == MONOGIF) {
  215.                 for(j=0;j<grf_r->width;j++) {
  216.                 if(grf_r->source_line[j] != 0) dest[j] = WHITE;
  217.                 else dest[j] = BLACK;
  218.                 }
  219.             }
  220.             else mono2row(grf_r->source_line,dest,WHITE,grf_r->width);
  221.             // black & white
  222.             putrow((rowptr)dest,0,currline,grf_r->width);
  223.             currline += inc_line;
  224.             if(kbhit() && (readkeyword() == kwEsc)) goto finish;
  225.                 goto finish;
  226.         }
  227.         break;
  228.     case COLOR16IMG:
  229.         switchmode(VGA16);
  230.         /*
  231.         if(grf_r->height > 480 || grf_r->width > 640)
  232.                 switchmode(SVGA800x600x16);
  233.                 */
  234.         init_keeper(COLOR16IMG, FALSE);
  235.  
  236.         for(i=0;i<16;i++) {
  237.             pal[i].r = grf_r->palette[i].r/4;
  238.             pal[i].g = grf_r->palette[i].g/4;
  239.             pal[i].b = grf_r->palette[i].b/4;
  240.         }
  241.         setVGApalette(pal);
  242.         for(i = grf_r->height; i > 0 ; i--) {
  243.             grf_r->get_next_line();
  244.             if(grf_r->source_ext_type != COLOR16_1PIXEL) {
  245.                 if(grf_r->source_ext_type == COLOR16_2PIXEL)
  246.                     col16A2col16B(grf_r->source_line,dest,grf_r->width);
  247.                 else col16plane2col16B(grf_r->source_line,dest,grf_r->width);
  248.             // 16 colors
  249.                 tmp = dest;
  250.                 putrow((rowptr)dest,0,currline,grf_r ->width);
  251.             }
  252.             else  tmp =  grf_r->source_line;
  253.  
  254.             putrow((rowptr) tmp, 0,currline,grf_r ->width);
  255.             currline += inc_line;
  256.  
  257.             if (keeper) keeper -> store_line(tmp);
  258.             if(kbhit() && (readkeyword() == kwEsc)) goto finish;
  259.  
  260.         }
  261.         break;
  262.     case GRAY256IMG:
  263.     case COLOR256IMG:
  264.         choosevideomode(grf_r);
  265.         // switchmode(VGA256);
  266.         init_keeper(COLOR256IMG, FALSE);
  267.  
  268.  
  269.         setpalette_by_BGR(grf_r->palette);
  270.         for(i = grf_r->height; i > 0; i--) {
  271.             grf_r->get_next_line();
  272.             // 256 colors
  273.             putrow((rowptr)grf_r->source_line,0,currline,grf_r->width);
  274.             currline += inc_line;
  275.  
  276.             if (keeper) keeper -> store_line(grf_r->source_line);
  277.             if(kbhit() && (readkeyword() == kwEsc)) goto finish;
  278.         }
  279.         break;
  280.  
  281.     case TRUECOLORIMG:
  282.  
  283.          choosevideomode(grf_r);
  284.         // switchmode(VGA256);
  285.         if (!EXM_initialized)  manager_initialize();
  286.  
  287.         stand_palette(pal);
  288.  
  289.         init_keeper(COLOR256IMG, FALSE);
  290.         reductor = new     color_reductor (grf_r, TRUE,  255);
  291.  
  292.         if (!reductor->initialized) {
  293.             printf("\a\a Not enough memory!");
  294.             goto finish;
  295.             }
  296.  
  297.  
  298.         if (reductor-> pass1() < 0 ) break;
  299.         setpalette_by_BGR(reductor->my_colormap);
  300.  
  301.         for( i = 0; i < grf_r->height; i++) {
  302.             reductor->process_line(currline,dest);
  303.             putrow((rowptr)dest,0,currline,grf_r-> width);
  304.             currline += inc_line;
  305.             if (keeper) keeper -> store_line(dest);
  306.  
  307.             if(kbhit() && (readkeyword() == kwEsc)) {
  308.                 goto finish;
  309.             }
  310.         }
  311.         delete reductor;
  312.         break;
  313.  
  314.  
  315.     }
  316.  
  317.     if (keeper) scroll_picture();
  318.     else delay_key1(120000);
  319.  
  320. finish:    if (keeper) delete keeper;
  321.     if (EXM_initialized)  manager_finish(TRUE);
  322.  
  323.     delete dest;
  324.     delete grf_r;
  325.     closegraph();
  326.     printf("Image shower. By ^Z 1994.\n");
  327. }
  328.  
  329.