home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / giflib11 / util / gif2iris.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-03  |  8.8 KB  |  283 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber            UNIX Ver 0.1,    Jul. 1989    *
  5. ******************************************************************************
  6. * Program to display GIF file under X11 window system.                 *
  7. * Options:                                     *
  8. * -f : force the process to be in foreground.                     *
  9. * -p PosX PosY : defines the position where to put the image.             *
  10. * -h : on line help.                                 *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 13 mar 90 - Version 1.0 by Gershon Elber.                     *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <graphics.h>
  18. #include <stdlib.h>
  19. #include <alloc.h>
  20. #include <io.h>
  21. #include <dos.h>
  22. #include <bios.h>
  23. #endif /* __MSDOS__ */
  24.  
  25. #include "gl.h"
  26. #include "device.h"
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <fcntl.h>
  32. #include "gif_lib.h"
  33. #include "getarg.h"
  34.  
  35. #define PROGRAM_NAME    "Gif2Iris"
  36.  
  37. #define ABS(x)        ((x) > 0 ? (x) : (-(x)))
  38.  
  39. #ifdef __MSDOS__
  40. extern unsigned int
  41.     _stklen = 16384;                 /* Increase default stack size. */
  42. #endif /* __MSDOS__ */
  43. #ifdef SYSV
  44. static char *VersionStr =
  45.         "Gif library module,\t\tGershon Elber\n\
  46.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  47. static char
  48.     *CtrlStr = "Gif2Iris f%- p%-PosX|PosY!d!d h%- GifFile!*s";
  49. #else
  50. static char
  51.     *VersionStr =
  52.     PROGRAM_NAME
  53.     GIF_LIB_VERSION
  54.     "    Gershon Elber,    "
  55.     __DATE__ ",   " __TIME__ "\n"
  56.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  57. static char
  58.     *CtrlStr =
  59.     PROGRAM_NAME
  60.     " f%- p%-PosX|PosY!d!d h%- GifFile!*s";
  61. #endif /* SYSV */
  62.  
  63. /* Make some variables global, so we could access them faster: */
  64. static int
  65.     PosFlag = FALSE,
  66.     HelpFlag = FALSE,
  67.     ForeGroundFlag = FALSE,
  68.     ColorMapSize = 0,
  69.     BackGround = 0,
  70.     IrisPosX = 0,
  71.     IrisPosY = 0,
  72.     InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
  73.     InterlacedJumps[] = { 8, 8, 4, 2 };    /* be read - offsets and jumps... */
  74. static GifColorType
  75.     *ColorMap;
  76.  
  77. static void Screen2Iris(GifRowType *ScreenBuffer,
  78.             int ScreenWidth, int ScreenHeight);
  79.  
  80. /******************************************************************************
  81. * Interpret the command line and scan the given GIF file.              *
  82. ******************************************************************************/
  83. void main(int argc, char **argv)
  84. {
  85.     int    i, j, Error, NumFiles, ImageNum = 0, Size, Row, Col, Width, Height,
  86.         ExtCode, Count;
  87.     GifRecordType RecordType;
  88.     GifByteType *Extension;
  89.     char **FileName = NULL;
  90.     GifRowType *ScreenBuffer;
  91.     GifFileType *GifFile;
  92.  
  93.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  94.         &ForeGroundFlag,
  95.         &PosFlag, &IrisPosX, &IrisPosY,
  96.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  97.         (NumFiles > 1 && !HelpFlag)) {
  98.     if (Error)
  99.         GAPrintErrMsg(Error);
  100.     else if (NumFiles > 1)
  101.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  102.     GAPrintHowTo(CtrlStr);
  103.     exit(1);
  104.     }
  105.  
  106.     if (HelpFlag) {
  107.     fprintf(stderr, VersionStr);
  108.     GAPrintHowTo(CtrlStr);
  109.     exit(0);
  110.     }
  111.  
  112.     if (NumFiles == 1) {
  113.     if ((GifFile = DGifOpenFileName(*FileName)) == NULL) {
  114.         PrintGifError();
  115.         exit(-1);
  116.     }
  117.     }
  118.     else {
  119.     /* Use the stdin instead: */
  120.  
  121. #ifdef __MSDOS__
  122.     setmode(0, O_BINARY);
  123. #endif /* __MSDOS__ */
  124.     if ((GifFile = DGifOpenFileHandle(0)) == NULL) {
  125.         PrintGifError();
  126.         exit(-1);
  127.     }
  128.     }
  129.  
  130.     /* Allocate the screen as vector of column of rows. We cannt allocate    */
  131.     /* the all screen at once, as this broken minded CPU can allocate up to  */
  132.     /* 64k at a time and our image can be bigger than that:             */
  133.     /* Note this screen is device independent - its the screen as defined by */
  134.     /* the GIF file parameters itself.                         */
  135.     if ((ScreenBuffer = (GifRowType *)
  136.     malloc(GifFile -> SHeight * sizeof(GifRowType *))) == NULL)
  137.         GIF_EXIT("Failed to allocate memory required, aborted.");
  138.  
  139.     Size = GifFile -> SWidth * sizeof(GifPixelType);/* Size in bytes one row.*/
  140.     if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) /* First row. */
  141.     GIF_EXIT("Failed to allocate memory required, aborted.");
  142.  
  143.     for (i = 0; i < GifFile -> SWidth; i++)  /* Set its color to BackGround. */
  144.     ScreenBuffer[0][i] = GifFile -> SBackGroundColor;
  145.     for (i = 1; i < GifFile -> SHeight; i++) {
  146.     /* Allocate the other rows, and set their color to background too: */
  147.     if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL)
  148.         GIF_EXIT("Failed to allocate memory required, aborted.");
  149.  
  150.     memcpy(ScreenBuffer[i], ScreenBuffer[0], Size);
  151.     }
  152.  
  153.     /* Scan the content of the GIF file and load the image(s) in: */
  154.     do {
  155.     if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
  156.         PrintGifError();
  157.         exit(-1);
  158.     }
  159.     switch (RecordType) {
  160.         case IMAGE_DESC_RECORD_TYPE:
  161.         if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
  162.             PrintGifError();
  163.             exit(-1);
  164.         }
  165.         Row = GifFile -> ITop; /* Image Position relative to Screen. */
  166.         Col = GifFile -> ILeft;
  167.         Width = GifFile -> IWidth;
  168.         Height = GifFile -> IHeight;
  169.         fprintf(stderr, "\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  170.             PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
  171.         if (GifFile -> ILeft + GifFile -> IWidth > GifFile -> SWidth ||
  172.            GifFile -> ITop + GifFile -> IHeight > GifFile -> SHeight) {
  173.             fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n");
  174.             exit(-2);
  175.         }
  176.         if (GifFile -> IInterlace) {
  177.             /* Need to perform 4 passes on the images: */
  178.             for (Count = i = 0; i < 4; i++)
  179.             for (j = Row + InterlacedOffset[i]; j < Row + Height;
  180.                          j += InterlacedJumps[i]) {
  181.                 fprintf(stderr, "\b\b\b\b%-4d", Count++);
  182.                 if (DGifGetLine(GifFile, &ScreenBuffer[j][Col],
  183.                 Width) == GIF_ERROR) {
  184.                 PrintGifError();
  185.                 exit(-1);
  186.                 }
  187.             }
  188.         }
  189.         else {
  190.             for (i = 0; i < Height; i++) {
  191.             fprintf(stderr, "\b\b\b\b%-4d", i);
  192.             if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],
  193.                 Width) == GIF_ERROR) {
  194.                 PrintGifError();
  195.                 exit(-1);
  196.             }
  197.             }
  198.         }
  199.         break;
  200.         case EXTENSION_RECORD_TYPE:
  201.         /* Skip any extension blocks in file: */
  202.         if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
  203.             PrintGifError();
  204.             exit(-1);
  205.         }
  206.         while (Extension != NULL) {
  207.             if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
  208.             PrintGifError();
  209.             exit(-1);
  210.             }
  211.         }
  212.         break;
  213.         case TERMINATE_RECORD_TYPE:
  214.         break;
  215.         default:            /* Should be traps by DGifGetRecordType. */
  216.         break;
  217.     }
  218.     }
  219.     while (RecordType != TERMINATE_RECORD_TYPE);
  220.  
  221.     /* Lets display it - set the global variables required and do it: */
  222.     BackGround = GifFile -> SBackGroundColor;
  223.     ColorMap = (GifFile -> IColorMap ? GifFile -> IColorMap :
  224.                        GifFile -> SColorMap);
  225.     ColorMapSize = 1 << (GifFile -> IColorMap ? GifFile -> IBitsPerPixel :
  226.                                 GifFile -> SBitsPerPixel);
  227.     fprintf(stderr, "\n");
  228.     Screen2Iris(ScreenBuffer, GifFile -> SWidth, GifFile -> SHeight);
  229.  
  230.     if (DGifCloseFile(GifFile) == GIF_ERROR) {
  231.     PrintGifError();
  232.     exit(-1);
  233.     }
  234. }
  235.  
  236. /******************************************************************************
  237. * The real display routine.                              *
  238. ******************************************************************************/
  239. static void Screen2Iris(GifRowType *ScreenBuffer,
  240.             int ScreenWidth, int ScreenHeight)
  241. {
  242.     short Val;
  243.     int i, j;
  244.     unsigned long *IrisScreenBuffer, *PBuffer;
  245.  
  246.     if (ScreenWidth > XMAXSCREEN + 1 ||
  247.     ScreenHeight > YMAXSCREEN + 1)
  248.     GIF_EXIT("Input image is too big.");
  249.  
  250.     if (PosFlag)
  251.     prefposition(IrisPosX, IrisPosX + ScreenWidth - 1,
  252.              IrisPosY, IrisPosY + ScreenHeight - 1);
  253.     else
  254.     prefsize(ScreenWidth, ScreenHeight);
  255.     if (ForeGroundFlag)
  256.     foreground();
  257.  
  258.     winopen(PROGRAM_NAME);
  259.     RGBmode();
  260.     gconfig();
  261.     if ((IrisScreenBuffer = (unsigned long *)
  262.      malloc(ScreenWidth * ScreenHeight * sizeof(unsigned long))) == NULL)
  263.     GIF_EXIT("Failed to allocate memory required, aborted.");
  264.  
  265.     PBuffer = IrisScreenBuffer;
  266.     for (i = ScreenHeight - 1; i >= 0; i--)
  267.     for (j = 0; j < ScreenWidth; j++)
  268.         *PBuffer++ = ColorMap[ScreenBuffer[i][j]].Red +
  269.                  (ColorMap[ScreenBuffer[i][j]].Green << 8) +
  270.                  (ColorMap[ScreenBuffer[i][j]].Blue << 16);
  271.  
  272.     reshapeviewport();
  273.     lrectwrite(0, 0, ScreenWidth - 1, ScreenHeight - 1, IrisScreenBuffer);
  274.  
  275.     while (TRUE) {
  276.     if (qread(&Val) == REDRAW) {
  277.         reshapeviewport();
  278.         lrectwrite(0, 0, ScreenWidth - 1, ScreenHeight - 1,
  279.                                                IrisScreenBuffer);
  280.     }
  281.     }
  282. }
  283.