home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************
- * Copyright (c) 1987 *
- * by CompuServe Inc, Columbus, Ohio. All Rights Reserved *
- ***********************************************************/
-
- /*
- * ABSTRACT:
- *
- * ENVIRONMENT: AmigaDOS
- *
- * AUTHOR: Steve Wilhite, CREATION DATE: 12-Mar-87
- *
- * REVISION HISTORY:
- *
- */
-
- #include <exec/types.h>
- #include <intuition/intuition.h>
- #include <stdio.h>
-
- /*
- * IMPORTS:
- */
-
- extern APTR OpenLibrary();
- extern APTR OpenScreen();
- extern APTR OpenWindow();
- extern APTR GetMsg();
- extern void LoadRGB4();
-
- /*
- * EXPORTS:
- */
-
- struct IntuitionBase *IntuitionBase;
- struct GfxBase *GfxBase;
-
- /*
- * PRIVATE:
- */
-
- static void Terminate();
-
- static UWORD colorTable[32];
- static struct ViewPort *vp;
- static struct RastPort *rp;
- static struct Screen *Screen;
- static struct Window *Window;
- static struct IntuiMessage *Msg;
- static UWORD Class, Code;
-
- static struct NewScreen NewScreen =
- {
- 0, 0, 0, 0, 0, 0, 1, 0, CUSTOMSCREEN, NULL, NULL, NULL, NULL
- };
-
- static struct NewWindow NewWindow =
- {
- 0, 0, 0, 0, 0, 1,
- CLOSEWINDOW,
- ACTIVATE | BORDERLESS | WINDOWCLOSE,
- NULL, NULL, NULL, NULL, NULL,
- 320, 200, 640, 400,
- CUSTOMSCREEN
- };
-
- #define buffer_size 4096
- #define INTUITION_REV 0
- #define GRAPHICS_REV 0
-
-
- static int
- input_file = 0,
- bytes_unread,
- palette[16],
- backdrop_width, backdrop_height,
- fill_color,
- left_edge, top_edge, right_edge, bottom_edge,
- image_width, image_height,
- bit_planes,
- have_color_map, interlaced_mode,
- row, col,
- old_color = -1,
- plane[4],
- screen_width = 320, screen_height = 200,
- base_row[4] = {0, 4, 2, 1 },
- row_disp[4] = {8, 8, 4, 2 },
- interlace_pass,
- num_colors;
-
- static char
- signature[] = "GIF87a";
-
- static unsigned char
- input_buffer[buffer_size],
- *input_ptr;
-
-
- static int read_byte()
- /*
- * Function:
- * Read the next byte from the input file.
- *
- * Returns:
- * 0 .. 255 the byte
- * -1 end of file
- * -4 read error
- */
- {
- if (bytes_unread == 0)
- {
- bytes_unread = Read(input_file, input_buffer, buffer_size);
-
- if (bytes_unread < 1)
- if (bytes_unread == 0)
- return -1;
- else
- return -4; /* disk read error */
-
- input_ptr = input_buffer;
- }
-
- bytes_unread--;
- return (int) *input_ptr++;
- }
-
- static int write_pixel(pixel)
- /*
- * Function:
- * Draw the specified pixel. Clip the image if necessary.
- *
- * Returns:
- * 0 (always)
- */
- int pixel;
- {
- if (pixel != fill_color && col < NewWindow.Width && row < NewWindow.Height)
- {
- SetAPen(rp, pixel);
- WritePixel(rp, col, row);
- }
-
- col++;
-
- if (col > right_edge)
- {
- col = left_edge;
-
- if (interlaced_mode)
- {
- row += row_disp[interlace_pass];
-
- if (row > bottom_edge)
- {
- interlace_pass++;
- row = top_edge + base_row[interlace_pass];
- }
- }
- else
- row++;
- }
-
- return 0;
- }
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int
- status,
- done,
- i, j,
- row, col,
- color_resolution,
- count, pixel,
- red, green, blue,
- color;
-
- char ch;
-
- /* Setup the libraries we will need. */
-
- IntuitionBase = (struct IntuitionBase *)
- OpenLibrary("intuition.library", INTUITION_REV);
-
- if (IntuitionBase == NULL)
- {
- puts("?cannot open intuition library\n");
- exit(FALSE);
- }
-
- GfxBase = (struct GfxBase *)
- OpenLibrary("graphics.library", GRAPHICS_REV);
-
- if (GfxBase == NULL)
- {
- puts("?cannot open graphics library\n");
- exit(FALSE);
- }
-
- if (argc < 2)
- {
- puts("usage: ShowGIF input-file\n");
- exit(1);
- }
-
- input_file = Open(argv[1], 1005);
-
- if (input_file == 0)
- {
- printf("? file %s not found\n", argv[1]);
- exit(1);
- }
-
- bytes_unread = 0;
-
- /* Read the signature sequence (should check it someday) */
-
- for (count = 0; count < 6; count++)
- read_byte();
-
- /* Read the virtual screen description */
-
- backdrop_width = read_byte();
- backdrop_width += 256*read_byte();
- backdrop_height = read_byte();
- backdrop_height += 256*read_byte();
- count = read_byte();
- bit_planes = (count & 7) + 1;
- num_colors = 1 << bit_planes;
- have_color_map = (count & 0x80) != 0;
- fill_color = read_byte();
- read_byte(); /* reserved */
-
- if (have_color_map)
- /*
- * Setup the default color map.
- */
- for (i = 0; i < num_colors; i++)
- {
- color = 0;
-
- red = read_byte();
- colorTable[i] = (red & 0xF0) << 4;
- green = read_byte();
- colorTable[i] |= (green & 0xF0);
- blue = read_byte();
- colorTable[i] |= (blue & 0xF0) >> 4;
- }
-
- /* Setup graphics mode depending on the size of the backdrop */
-
- if (backdrop_width <= 320)
- {
- NewScreen.Width = 320;
- NewScreen.Height = 200;
- NewScreen.Depth = bit_planes;
- }
- else if (backdrop_height <= 200)
- {
- NewScreen.Width = 640;
- NewScreen.Height = 200;
- NewScreen.Depth = 4;
- NewScreen.ViewModes = HIRES;
- }
- else
- {
- NewScreen.Width = 640;
- NewScreen.Height = 400;
- NewScreen.Depth = 4;
- NewScreen.ViewModes = HIRES | LACE;
- }
-
- NewWindow.Width = NewScreen.Width;
- NewWindow.Height = NewScreen.Height;
-
- if ((Screen = (struct Screen *) OpenScreen(&NewScreen)) == NULL)
- {
- puts("?cannot open new screen\n");
- exit(FALSE);
- }
-
- NewWindow.Screen = Screen;
-
- if ((Window = (struct Window *) OpenWindow(&NewWindow)) == NULL)
- {
- puts("?cannot open window\n");
- Terminate();
- }
-
- vp = &Screen->ViewPort;
- rp = Window->RPort;
-
- LoadRGB4(vp, colorTable, num_colors);
- SetDrMd(rp, JAM1);
- SetAPen(rp, 0);
- RectFill(rp, 0, 0, NewWindow.Width - 1, NewWindow.Height - 1);
- done = 0;
-
- while (!done)
- {
- ch = read_byte();
-
- switch (ch)
- {
- case ',':
- /*
- * Read the image description.
- */
- left_edge = read_byte();
- left_edge += 256*read_byte();
- top_edge = read_byte();
- top_edge += 256*read_byte();
- image_width = read_byte();
- image_width += 256*read_byte();
- image_height = read_byte();
- image_height += 256*read_byte();
- count = read_byte();
- bit_planes = (count & 0x07) + 1;
- interlaced_mode = (count & 0x40) != 0;
- have_color_map = (count & 0x80) != 0;
- num_colors = 1 << bit_planes;
- right_edge = left_edge + image_width - 1;
- bottom_edge = top_edge + image_height - 1;
-
- if (have_color_map)
- /*
- * Read the local color map but ignore it for now.
- */
- for (i = 0; i < num_colors; i++)
- {
- red = read_byte();
- green = read_byte();
- blue = read_byte();
- }
-
- row = top_edge;
- col = left_edge;
- interlace_pass = 0;
- status = Expand_Data(read_byte, write_pixel);
-
- /*
- * Wait until we get a close window message
- */
-
- for (;;)
- {
- WaitPort(Window->UserPort);
-
- while ((Msg = (struct IntuiMessage *) GetMsg(Window->UserPort)) != NULL)
- {
- Class = Msg->Class;
- Code = Msg->Code;
- ReplyMsg(Msg);
-
- if (Class == CLOSEWINDOW)
- Terminate();
- }
- }
-
- break;
-
- case ';': /* End of image-set */
-
- Terminate();
- break;
-
- default:
- printf("? unexpected byte %2x in GIF file\n", ch);
- Terminate();
- }
- }
- }
-
-
- static void Terminate()
- {
- if (input_file != 0)
- Close(input_file);
- if (Window != NULL)
- CloseWindow(Window);
- CloseScreen(Screen);
- OpenWorkBench();
- exit(TRUE);
- }
-
-