home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / formats / gif / gen_code / amiga / buildgif.c next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  5.4 KB  |  207 lines

  1. /***********************************************************
  2.  * Copyright (c) 1987                                      *
  3.  * by CompuServe Inc, Columbus, Ohio.  All Rights Reserved *
  4.  ***********************************************************/
  5.  
  6. #include <stdio.h>
  7. #include "buildgif.h"
  8.  
  9. static short Write_Color_Map();
  10. static char GIF_signature[] = "GIF87a";
  11.  
  12.  
  13. short Create_GIF (read_pixel, write_byte, width, height, cr, interlaced,
  14.           bp, color_map)
  15. /*
  16.  * Function:
  17.  *    Create a 1-image GIF image-set.  The size of the screen will be the
  18.  *    same as the image.
  19.  *
  20.  * Inputs:
  21.  */
  22.     short (*read_pixel)();        /* ptr to caller's pixel reader */
  23.     short (*write_byte)();        /* ptr to caller's byte writer */
  24.     short width;            /* width of image in pixels */
  25.     short height;            /* height of image in pixels */
  26.     short cr;                /* bits of color resolution (1..8) */
  27.     short interlaced;            /* interlaced flag */
  28.     short bp;                /* bits per pixel (1..8) */
  29.     struct ColorEntry color_map[];    /* ptr to color map */
  30. /*
  31.  * Returns:
  32.  *    0 = OK, else error code
  33.  */
  34.     {
  35.     short status;        /* return status */
  36.  
  37.     status = Write_Screen_Desc (write_byte, width, height, cr, 0, bp, color_map);
  38.     if (status != 0) return status;
  39.  
  40.     status = Write_Image_Desc (write_byte, 0, 0, width, height, 0, interlaced, 0, NULL);
  41.     if (status != 0) return status;
  42.  
  43.     status = Compress_Data (bp, read_pixel, write_byte);
  44.     if (status != 0) return status;
  45.  
  46.     return (*write_byte)(';');
  47.     }
  48.  
  49. short Write_Screen_Desc (write_byte, width, height, cr, fill_color, bp, color_map)
  50. /*
  51.  * Function:
  52.  *    Write the GIF signature, the screen description, and the optional
  53.  *    color map.
  54.  *
  55.  * Inputs:
  56.  */
  57.     short (*write_byte)();        /* ptr to caller's byte writer */
  58.     short width;            /* width of the screen in pixels */
  59.     short height;            /* height of the screen in pixels */
  60.     short cr;                /* bits of color resolution (1..8) */
  61.     short fill_color;            /* pixel value used to fill the background */
  62.     short bp;                /* bits per pixel (1..8) */
  63.     struct ColorEntry color_map[];    /* ptr to the optional color map */
  64. /*
  65.  * Returns:
  66.  *    0 = OK, else error code
  67.  */
  68.     {
  69.     short status;
  70.     short i;
  71.  
  72.     for (i = 0; i < 6; i++)
  73.     if ((status = (*write_byte)(GIF_signature[i])) != 0) return status;
  74.  
  75.     status = (*write_byte)(width & 0xFF);
  76.     if (status != 0) return status;
  77.  
  78.     status = (*write_byte)(width >> 8);
  79.     if (status != 0) return status;
  80.  
  81.     status = (*write_byte)(height & 0xFF);
  82.     if (status != 0) return status;
  83.  
  84.     status = (*write_byte)(height >> 8);
  85.     if (status != 0) return status;
  86.  
  87.     if (color_map == NULL)
  88.     status = (*write_byte)(((cr - 1) & 0x07) << 4);
  89.     else
  90.     status = (*write_byte)(0x80 | ((cr - 1) << 4) | ((bp - 1) & 0x07));
  91.  
  92.     if (status != 0) return status;
  93.  
  94.     status = (*write_byte)(fill_color);
  95.     if (status != 0) return status;
  96.  
  97.     status = (*write_byte)(0);    /* reserved */
  98.     if (status != 0) return status;
  99.  
  100.     if (color_map != NULL)
  101.     return Write_Color_Map (write_byte, cr, bp, color_map);
  102.     else
  103.     return 0;
  104.     }
  105.  
  106. short Write_Image_Desc (write_byte, left_edge, top_edge, width, height,
  107.             cr, interlaced, bp, color_map)
  108. /*
  109.  * Function:
  110.  *    Write the image description and optional local color map.
  111.  *
  112.  * Inputs:
  113.  */
  114.     short (*write_byte)();        /* ptr to caller's byte writer */
  115.     short left_edge;            /* (left_edge, top_edge) is the upper left */
  116.     short top_edge;            /* position in pixels of the image */
  117.     short width;            /* width of the image in pixels */
  118.     short height;            /* height of the image in pixels */
  119.     short cr;                /* bits of color resolution (1..8) */
  120.     short interlaced;            /* 1 = interlace the image */
  121.     short bp;                /* bits per pixel for this image */
  122.     struct ColorEntry color_map[];    /* ptr to local color map */
  123. /*
  124.  * Returns:
  125.  *    0 = OK, else error code
  126.  */
  127.     {
  128.     short status;
  129.  
  130.     status = (*write_byte)(',');
  131.     if (status != 0) return status;
  132.  
  133.     status = (*write_byte)(left_edge & 0xFF);
  134.     if (status != 0) return status;
  135.  
  136.     status = (*write_byte)(left_edge >> 8);
  137.     if (status != 0) return status;
  138.  
  139.     status = (*write_byte)(top_edge & 0xFF);
  140.     if (status != 0) return status;
  141.  
  142.     status = (*write_byte)(top_edge >> 8);
  143.     if (status != 0) return status;
  144.  
  145.     status = (*write_byte)(width & 0xFF);
  146.     if (status != 0) return status;
  147.  
  148.     status = (*write_byte)(width >> 8);
  149.     if (status != 0) return status;
  150.  
  151.     status = (*write_byte)(height & 0xFF);
  152.     if (status != 0) return status;
  153.  
  154.     status = (*write_byte)(height >> 8);
  155.     if (status != 0) return status;
  156.  
  157.     if (color_map == NULL)
  158.     status = (*write_byte)(interlaced << 6);
  159.     else
  160.     status = (*write_byte)(0x80 | (interlaced << 6) | ((bp - 1) & 0x07));
  161.  
  162.     if (status != 0) return status;
  163.  
  164.     if (color_map != NULL)
  165.     Write_Color_Map (write_byte, cr, bp, color_map);
  166.     else
  167.     return 0;
  168.     }
  169.  
  170. static short Write_Color_Map (write_byte, cr, bp, color_map)
  171. /*
  172.  * Function:
  173.  *    Write the specified color map.
  174.  *
  175.  * Inputs:
  176.  *    (see above)
  177.  *
  178.  * Returns:
  179.  *    0 = OK, else error code
  180.  */
  181.     short (*write_byte)();
  182.     short cr;
  183.     short bp;
  184.     struct ColorEntry color_map[];
  185.     {
  186.     short num_colors;
  187.     short n, i;
  188.     short status;
  189.  
  190.     num_colors = 1 << bp;
  191.     n = (1 << cr) - 1;
  192.  
  193.     for (i = 0; i < num_colors; i++)
  194.     {
  195.     status = (*write_byte)(color_map[i].red*255/n);
  196.     if (status != 0) return status;
  197.  
  198.     status = (*write_byte)(color_map[i].green*255/n);
  199.     if (status != 0) return status;
  200.  
  201.     status = (*write_byte)(color_map[i].blue*255/n);
  202.     if (status != 0) return status;
  203.     }
  204.  
  205.     return 0;
  206.     }
  207.