home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / formats / gif / gen_code / amiga / showgif.c < prev   
Encoding:
Text File  |  1989-03-02  |  7.1 KB  |  351 lines

  1. /***********************************************************
  2.  * Copyright (c) 1987                                      *
  3.  * by CompuServe Inc, Columbus, Ohio.  All Rights Reserved *
  4.  ***********************************************************/
  5.  
  6. /*
  7.  * ABSTRACT:
  8.  *
  9.  * ENVIRONMENT: AmigaDOS
  10.  *
  11.  * AUTHOR: Steve Wilhite, CREATION DATE: 12-Mar-87
  12.  *
  13.  * REVISION HISTORY:
  14.  *
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <intuition/intuition.h>
  19. #include <stdio.h>
  20.  
  21. /*
  22.  * IMPORTS:
  23.  */
  24.  
  25. extern APTR OpenLibrary();
  26. extern APTR OpenScreen();
  27. extern APTR OpenWindow();
  28. extern APTR GetMsg();
  29. extern void LoadRGB4();
  30.  
  31. /*
  32.  * EXPORTS:
  33.  */
  34.  
  35. struct IntuitionBase *IntuitionBase;
  36. struct GfxBase *GfxBase;
  37.  
  38. /*
  39.  * PRIVATE:
  40.  */
  41.  
  42. static void Terminate();
  43.  
  44. static UWORD colorTable[32];
  45. static struct ViewPort *vp;
  46. static struct RastPort *rp;
  47. static struct Screen *Screen;
  48. static struct Window *Window;
  49. static struct IntuiMessage *Msg;
  50. static UWORD Class, Code;
  51.  
  52. static struct NewScreen NewScreen =
  53.     {
  54.     0, 0, 0, 0, 0, 0, 1, 0, CUSTOMSCREEN, NULL, NULL, NULL, NULL
  55.     };
  56.  
  57. static struct NewWindow NewWindow =
  58.     {
  59.     0, 0, 0, 0, 0, 1,
  60.     CLOSEWINDOW,
  61.     ACTIVATE | BORDERLESS | WINDOWCLOSE,
  62.     NULL, NULL, NULL, NULL, NULL,
  63.     320, 200, 640, 400,
  64.     CUSTOMSCREEN
  65.     };
  66.  
  67. #define buffer_size    4096
  68. #define INTUITION_REV    0
  69. #define GRAPHICS_REV    0
  70.  
  71.  
  72. static int
  73.     input_file = 0,
  74.     bytes_unread,
  75.     palette[16],
  76.     backdrop_width, backdrop_height,
  77.     fill_color,
  78.     left_edge, top_edge, right_edge, bottom_edge,
  79.     image_width, image_height,
  80.     bit_planes,
  81.     have_color_map, interlaced_mode,
  82.     row, col,
  83.     old_color = -1,
  84.     plane[4],
  85.     screen_width = 320, screen_height = 200,
  86.     base_row[4] = {0, 4, 2, 1 },
  87.     row_disp[4] = {8, 8, 4, 2 },
  88.     interlace_pass,
  89.     num_colors;
  90.  
  91. static char
  92.     signature[] = "GIF87a";
  93.  
  94. static unsigned char
  95.     input_buffer[buffer_size],
  96.     *input_ptr;
  97.  
  98.  
  99. static int read_byte()
  100. /*
  101.  * Function:
  102.  *    Read the next byte from the input file.
  103.  *
  104.  * Returns:
  105.  *    0 .. 255    the byte
  106.  *    -1        end of file
  107.  *    -4        read error
  108.  GIFfy2+Ü8BJ¢÷⌠D ÿ╥xαD└─ PpàÇΓUöp@:å'É£≤πo╔;4H ! I╛éXα@ à@R<└D+H&%sΓ╩ƒ69╫└çÆUX¥    DHZ╤4¿"á@hå@Ѽó!S+
  109. ╙Æ┼!@    R$@Z )Çä£,╚?ƒÇU    |║SQ ⌠AV@ªèé∞ƒ╜╝c╓ⁿ!f╠Qö²├'Γ╔ï`▓IíT╟╓ƒÉt%06Çâ▒≡ü·      T?#üUüê@0EɪD     í─
  110. $xtlù]▄p(göC╚    ╖ÄIü`C·Å▌ÆHßû"ò#/á!vü @å&├┘?ⁿ8í╦î└ÇP┼ü└,£æ!N`%a¬╚Dô╧┴Aö?b(┴èt£üï4╛πîx╪ê#:╩>╥çEÉ?!q    "|╚âê┘q╙$ΦÑèV║≥,⌡ó! |α}L0±═~8QdÇHδ└┼ïo╞9gQî¿9;rZHT?╪Ç▌┌¿ÉU@¿äVÿAÇà`x≡B   !<`±*/
  111.     {
  112.     if (bytes_unread == 0)
  113.     {
  114.     bytes_unread = Read(input_file, input_buffer, buffer_size);
  115.  
  116.     if (bytes_unread < 1)
  117.         if (bytes_unread == 0)
  118.         return -1;
  119.         else
  120.         return -4;        /* disk read error */
  121.  
  122.     input_ptr = input_buffer;
  123.     }
  124.  
  125.     bytes_unread--;
  126.     return (int) *input_ptr++;
  127.     }
  128.  
  129. static int write_pixel(pixel)
  130. /*
  131.  * Function:
  132.  *    Draw the specified pixel. Clip the image if necessary.
  133.  *
  134.  * Returns:
  135.  *    0 (always)
  136.  */
  137.     int pixel;
  138.     {
  139.     if (pixel != fill_color && col < NewWindow.Width && row < NewWindow.Height)
  140.     {
  141.     SetAPen(rp, pixel);
  142.     WritePixel(rp, col, row);
  143.     }
  144.  
  145.     col++;
  146.  
  147.     if (col > right_edge)
  148.     {
  149.     col = left_edge;
  150.  
  151.     if (interlaced_mode)
  152.         {
  153.         row += row_disp[interlace_pass];
  154.  
  155.         if (row > bottom_edge)
  156.         {
  157.         interlace_pass++;
  158.         row = top_edge + base_row[interlace_pass];
  159.         }
  160.         }
  161.     else
  162.         row++;
  163.     }
  164.  
  165.     return 0;
  166.     }
  167.  
  168. main(argc, argv)
  169.     int argc;
  170.     char *argv[];
  171.     {
  172.     int
  173.     status,
  174.     done,
  175.     i, j,
  176.     row, col,
  177.     color_resolution,
  178.         count, pixel,
  179.         red, green, blue,
  180.     color;
  181.  
  182.     char ch;
  183.  
  184.     /* Setup the libraries we will need. */
  185.     
  186.     IntuitionBase = (struct IntuitionBase *)
  187.         OpenLibrary("intuition.library", INTUITION_REV);
  188.         
  189.     if (IntuitionBase == NULL)
  190.     {
  191.     puts("?cannot open intuition library\n");
  192.     exit(FALSE);
  193.     }
  194.  
  195.     GfxBase = (struct GfxBase *)
  196.     OpenLibrary("graphics.library", GRAPHICS_REV);
  197.  
  198.     if (GfxBase == NULL)
  199.     {
  200.     puts("?cannot open graphics library\n");
  201.         exit(FALSE);
  202.     }
  203.  
  204.     if (argc < 2)
  205.     {
  206.     puts("usage: ShowGIF input-file\n");
  207.     exit(1);
  208.     }
  209.  
  210.     input_file = Open(argv[1], 1005);
  211.  
  212.     if (input_file == 0)
  213.     {
  214.     printf("? file %s not found\n", argv[1]);
  215.     exit(1);
  216.     }
  217.  
  218.     bytes_unread = 0;
  219.  
  220.     /* Read the signature sequence (should check it someday) */
  221.  
  222.     for (count = 0; count < 6; count++)
  223.     read_byte();
  224.  
  225.     /* Read the virtual screen description */
  226.  
  227.     backdrop_width = read_byte();
  228.     backdrop_width += 256*read_byte();
  229.     backdrop_height = read_byte();
  230.     backdrop_height += 256*read_byte();
  231.     count = read_byte();
  232.     bit_planes = (count & 7) + 1;
  233.     num_colors = 1 << bit_planes;
  234.     have_color_map = (count & 0x80) != 0;
  235.     fill_color = read_byte();
  236.     read_byte();            /* reserved */
  237.  
  238.     if (have_color_map)
  239.     /*
  240.      * Setup the default color map.
  241.      */
  242.     for (i = 0; i < num_colors; i++)
  243.         {
  244.         color = 0;
  245.  
  246.         red = read_byte();
  247.         colorTable[i] = (red & 0xF0) << 4;
  248.         green = read_byte();
  249.         colorTable[i] |= (green & 0xF0);
  250.         blue = read_byte();
  251.         colorTable[i] |= (blue & 0xF0) >> 4;
  252.         }
  253.  
  254.     /* Setup graphics mode depending on the size of the backdrop */
  255.  
  256.     if (backdrop_width <= 320)
  257.     {
  258.     NewScreen.Width = 320;
  259.     NewScreen.Height = 200;
  260.     NewScreen.Depth = bit_planes;
  261.     }
  262.     else if (backdrop_height <= 200)
  263.     {
  264.     NewScreen.Width = 640;
  265.     NewScreen.Height = 200;
  266.     NewScreen.Depth = 4;
  267.     NewScreen.ViewModes = HIRES;
  268.     }
  269.     else
  270.     {
  271.     NewScreen.Width = 640;
  272.     NewScreen.Height = 400;
  273.     NewScreen.Depth = 4;
  274.     NewScreen.ViewModes = HIRES | LACE;
  275.     }
  276.  
  277.     NewWindow.Width = NewScreen.Width;
  278.     NewWindow.Height = NewScreen.Height;
  279.  
  280.     if ((Screen = (struct Screen *) OpenScreen(&NewScreen)) == NULL)
  281.     {
  282.         puts("?cannot open new screen\n");
  283.         exit(FALSE);
  284.     }
  285.     
  286.     NewWindow.Screen = Screen;
  287.     
  288.     if ((Window = (struct Window *) OpenWindow(&NewWindow)) == NULL)
  289.     {
  290.         puts("?cannot open window\n");
  291.     Terminate();
  292.     }
  293.     
  294.     vp = &Screen->ViewPort;
  295.     rp = Window->RPort;
  296.  
  297.     LoadRGB4(vp, colorTable, num_colors);
  298.     SetDrMd(rp, JAM1);
  299.     SetAPen(rp, 0);
  300.     RectFill(rp, 0, 0, NewWindow.Width - 1, NewWindow.Height - 1);
  301.     done = 0;
  302.  
  303.     while (!done)
  304.     {
  305.     ch = read_byte();
  306.  
  307.     switch (ch)
  308.         {
  309.         case ',':
  310.         /*
  311.          * Read the image description.
  312.          */
  313.         left_edge = read_byte();
  314.         left_edge += 256*read_byte();
  315.         top_edge = read_byte();
  316.         top_edge += 256*read_byte();
  317.         image_width = read_byte();
  318.         image_width += 256*read_byte();
  319.         image_height = read_byte();
  320.         image_height += 256*read_byte();
  321.         count = read_byte();
  322.         bit_planes = (count & 0x07) + 1;
  323.         interlaced_mode = (count & 0x40) != 0;
  324.         have_color_map = (count & 0x80) != 0;
  325.         num_colors = 1 << bit_planes;
  326.         right_edge = left_edge + image_width - 1;
  327.         bottom_edge = top_edge + image_height - 1;
  328.  
  329.         if (have_color_map)
  330.             /*
  331.              * Read the local color map but ignore it for now.
  332.              */
  333.             for (i = 0; i < num_colors; i++)
  334.             {
  335.             red = read_byte();
  336.             green = read_byte();
  337.             blue = read_byte();
  338.             }
  339.  
  340.         row = top_edge;
  341.         col = left_edge;
  342.         interlace_pass = 0;
  343.         status = Expand_Data(read_byte, write_pixel);
  344.  
  345.         /*
  346.          * Wait until we get a close window message
  347.          */
  348.  
  349.         for (;;)
  350.             {
  351.             WaitPort(Window->UserPort);
  352.  
  353.             while ((Msg = (struct IntuiMessage *) GetMsg(Window->User