home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / gpc / pixbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-15  |  12.5 KB  |  395 lines

  1. /* $XConsortium: pixbuf.c,v 5.1 91/02/16 10:07:50 rws Exp $ */
  2. /***********************************************************
  3. Copyright(c) 1989,1990, 1991 by Sun Microsystems, Inc. and the X Consortium at M.I.T.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Sun Microsystems,
  12. the X Consortium, and MIT not be used in advertising or publicity
  13. pertaining to distribution of the software without specific, written
  14. prior permission.
  15.  
  16. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
  18. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  19. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25.  
  26. /*--------------------------------------------------------------------*\
  27. |  Copyright (C) 1989,1990, 1991, National Computer Graphics Association
  28. |
  29. |  Permission is granted to any individual or institution to use, copy, or
  30. |  redistribute this software so long as it is not sold for profit, provided
  31. |  this copyright notice is retained.
  32. |
  33. |                         Developed for the
  34. |                National Computer Graphics Association
  35. |                         2722 Merrilee Drive
  36. |                         Fairfax, VA  22031
  37. |                           (703) 698-9600
  38. |
  39. |                                by
  40. |                 SimGraphics Engineering Corporation
  41. |                    1137 Huntington Drive  Unit A
  42. |                      South Pasadena, CA  91030
  43. |                           (213) 255-0900
  44. |---------------------------------------------------------------------
  45. |
  46. | Author        :    John M. Zulauf
  47. | File          :    pixbuf.c
  48. | Date          :    Thu Feb  8 17:50:14 PST 1990
  49. | Project       :    GPC
  50. | Description   :    Manager the pixel input buffer
  51. | Status        :    Version 1.0
  52. |
  53. | Revisions     :    
  54. |
  55. \*--------------------------------------------------------------------*/
  56.  
  57. /*--------------------------------------------------------------------*\
  58. |    Table of Contents
  59. |
  60. |    bif_initpixelbuffer(int, int, int, *unsigned char, *unsigned char, *unsigned char )
  61. |        :    Initialize the pixmap input buffer
  62. |    bif_fillpixelbuffer(BIF_INT)
  63. |        :    Recieve pixel value from the parser, add to list
  64. |    bif_endpixelbuffer(int, * Real_int_union)
  65. |        :    Finish the pixmap, error test, and pseudo map.
  66. |    keepPixel()
  67. |        :    Keep the currently stored pixel.
  68. |
  69. \*--------------------------------------------------------------------*/
  70.  
  71. /*--------------------------------------------------------------------*\
  72. |    Include Files
  73. \*--------------------------------------------------------------------*/
  74. #include <stdio.h>
  75. #include "biftypes.h"
  76. #include "bifmacro.h"
  77. #include "bifparse.h"
  78. #include "globals.h"
  79.  
  80. /*--------------------------------------------------------------------*\
  81. |    Local Defines
  82. \*--------------------------------------------------------------------*/
  83. #define RUN_LENGTH(buf,run,val)\
  84. {\
  85.     int MACi_index;\
  86.     if ( buf != NULL )\
  87.         for ( MACi_index = 0; MACi_index < run ; MACi_index++ )\
  88.             *(buf++) = val;\
  89. }
  90.  
  91. #define INIT_PIXEL_STORE(type,store)\
  92. {\
  93.     valType = type;\
  94.     limStore = store;\
  95.     if ( wk_info.color_mode == BIF_TRUE_COLOR )\
  96.         limKeep  = KEEP_RGB;\
  97.     else\
  98.         limKeep  = KEEP_RED;\
  99. }
  100.  
  101. #define STORE_RED    1
  102. #define STORE_RGB    3
  103.  
  104. #define KEEP_RED    1
  105. #define KEEP_RGB    3
  106.  
  107. #define PIXEL_VALUE    1
  108. #define SEG_HEADER    2
  109. #define BUFFER_FULL    3
  110.  
  111. static unsigned char    *redBase    = NULL;
  112. static unsigned char    *greenBase    = NULL;
  113. static unsigned char    *blueBase    = NULL;
  114. static unsigned char    *buffer[3]    = { NULL, NULL , NULL };
  115. static int        *colorIndex    = NULL;
  116. static int        *colorIndexBase    = NULL;
  117. static int        numRows;
  118. static int        numColumns;
  119. static int        mapType;
  120.  
  121. static int        mapSize;    /* Total number of pixels    */
  122. static int        overCount;    /* Did we get too many?      */
  123.  
  124. static int        intStore[3];
  125. static unsigned char    pixStore[3];
  126. static int        numKept;    /* The number pixels stored  */
  127. static int        valType;    /* Expect what next???       */
  128. static int        runLength;    /* Pixel repeat count         */
  129. static int        endOfSeg;    /* Where does the seg end?   */
  130. static int        limStore;    /* Store how many values?    */
  131. static int        limKeep;    /* Of these keep which ones? */
  132. static int        numStored;    /* The number of stored vals */
  133.  
  134. /*--------------------------------------------------------------------*\
  135. | Procedure     :    bif_initpixelbuffer(int, int, int,
  136. |                *unsigned char, *unsigned char,
  137. |                *unsigned char )
  138. |---------------------------------------------------------------------
  139. | Description   :    Initialize the pixmap input buffer
  140. |---------------------------------------------------------------------
  141. | Return        :    Error Code: (NI:)
  142. \*--------------------------------------------------------------------*/
  143. bif_initpixelbuffer(inNumRows, inNumColumns, inMapType,
  144.             inRed, inGreen, inBlue)
  145. int        inNumRows;
  146. int        inNumColumns;
  147. int        inMapType;
  148. unsigned char    *inRed;
  149. unsigned char    *inGreen;
  150. unsigned char    *inBlue;
  151.  
  152. {/* initPixBuf */
  153.     int retCode;
  154.  
  155.     /*------------------------------------------------------------*\
  156.     |    Save the size, type, and buffer locations
  157.     \*------------------------------------------------------------*/
  158.     numRows    = inNumRows;
  159.     numColumns = inNumColumns;
  160.     mapType    = inMapType;
  161.     redBase    = inRed;
  162.     greenBase  = inGreen;
  163.     blueBase   = inBlue;
  164.  
  165.     /*------------------------------------------------------------*\
  166.     |    We have to save the RED values as integers just in case
  167.     |    the user decides to do this as a pseudo color thang...
  168.     \*------------------------------------------------------------*/
  169.     if ( colorIndexBase != NULL )
  170.         free(colorIndexBase);
  171.     mapSize       = numRows * numColumns;
  172.     colorIndexBase = (int *)malloc(sizeof(int)*mapSize);
  173.  
  174.     if ( colorIndexBase == NULL )
  175.         ERROR("FATAL: Out of memory. (malloc failed)");
  176.  
  177.     /*------------------------------------------------------------*\
  178.     |    Set up some Useful additional information
  179.     \*------------------------------------------------------------*/
  180.     buffer[0]  = redBase;
  181.     buffer[1]  = greenBase;
  182.     buffer[2]  = blueBase;
  183.     colorIndex = colorIndexBase;
  184.  
  185.     numKept    = 0;
  186.     endOfSeg   = mapSize + 1;
  187.     numStored  = 0;
  188.     runLength  = 1;
  189.     overCount  = 0;
  190.  
  191.     switch ( mapType )
  192.     {
  193.     case PIXEL_VALUES:
  194.         INIT_PIXEL_STORE(PIXEL_VALUE,STORE_RGB);
  195.         break;
  196.     case INTENSITY_VALUES:
  197.         INIT_PIXEL_STORE(PIXEL_VALUE,STORE_RED);
  198.         break;
  199.     case PIXEL_VALUE_SEGMENTS:
  200.         INIT_PIXEL_STORE(SEG_HEADER,STORE_RGB);
  201.         break;
  202.     case INTENSITY_VALUE_SEGMENTS:
  203.         INIT_PIXEL_STORE(SEG_HEADER,STORE_RED);
  204.         break;
  205.     }
  206.  
  207.     retCode   = 0; /* We Be Okey Dokey */
  208.     return ( retCode );
  209.  
  210. }/* initPixBuf */
  211.  
  212.  
  213. /*--------------------------------------------------------------------*\
  214. | Procedure     :    bif_fillpixelbuffer(BIF_INT)
  215. |---------------------------------------------------------------------
  216. | Description    :    Recieve pixel value from the parser, add to list
  217. |---------------------------------------------------------------------
  218. | Return        :    Error Code (NI:)
  219. \*--------------------------------------------------------------------*/
  220. bif_fillpixelbuffer(pix)
  221. BIF_INT pix;
  222.  
  223. {/* bif_fillpixelbuffer */
  224.     switch (valType)
  225.     {
  226.     case PIXEL_VALUE:
  227.         intStore[numStored]   = (int)pix;
  228.         pixStore[numStored++] = (unsigned char)pix;
  229.         if ( numStored >= limStore )
  230.             storePixel();
  231.         break;
  232.     case SEG_HEADER:
  233.         if ( pix > 0 )
  234.         {/* Run length segment */
  235.             runLength = pix;
  236.             endOfSeg  = numKept + runLength;
  237.         }/* Run length segment */
  238.         else
  239.         {/* Pixel Values Segment */
  240.             runLength = 1;
  241.             endOfSeg  = numKept + -(pix);
  242.         }/* Pixel Values Segment */
  243.         valType = PIXEL_VALUE;
  244.         break;
  245.     case BUFFER_FULL:
  246.         overCount++;
  247.         break;
  248.     }
  249.         
  250. }/* bif_fillpixelbuffer */
  251.  
  252.  
  253. /*--------------------------------------------------------------------*\
  254. | Procedure     :    bif_endpixelbuffer(int, * Real_int_union)
  255. |---------------------------------------------------------------------
  256. | Description   :    Finish the pixmap, error test, and pseudo map.
  257. |---------------------------------------------------------------------
  258. | Return        :    Error Code (NI:)
  259. \*--------------------------------------------------------------------*/
  260. bif_endpixelbuffer(pseudoColorSize, pseudoColorMap)
  261. int pseudoColorSize;
  262. Real_int_union *pseudoColorMap;
  263.  
  264. {/* bif_endpixelbuffer */
  265.     int i, j, maxIndex, indx;
  266.     char buffy[255];
  267.  
  268.     /*------------------------------------------------------------*\
  269.     |    Make sure the buffer is full
  270.     \*------------------------------------------------------------*/
  271.     if ( valType != BUFFER_FULL )
  272.     {/* Oops, we didn't fill the buffer all the way */
  273.         /* Report! */
  274.         sprintf(buffy,
  275.             "In PIXEL_MAP3: Map size error (%d vs. %d).\n",
  276.             numKept, mapSize);
  277.         yyerror(buffy);
  278.  
  279.         /*----------------------------------------------------*\
  280.         |    Fill the rest of the map with BLACK (0)
  281.         \*----------------------------------------------------*/
  282.         intStore[0] = 0;
  283.         for( i = 0; i < limStore; i++ )
  284.             pixStore[i] = (unsigned char)0;
  285.         runLength = mapSize - numKept;
  286.         storePixel();
  287.  
  288.     }/* end Oops, we didn't fill the buffer all the way */
  289.     else if ( overCount > 0 )
  290.     {/* Too many pixels */
  291.         /* Report! */
  292.         sprintf(buffy,
  293.             "In PIXEL_MAP3: Map size error (%d vs. %d).\n",
  294.             mapSize+overCount, mapSize);
  295.         yyerror(buffy);
  296.     }/* end Too many pixels */
  297.  
  298.  
  299.     /*------------------------------------------------------------*\
  300.     |    If there is a pseudo color mapping (just) do it.
  301.     |    The save color index value remaps the RGB values
  302.     \*------------------------------------------------------------*/
  303.     if ( pseudoColorSize > 0 )
  304.     {/* if pseudo coloring should be done */
  305.         buffer[0]  = redBase;
  306.         buffer[1]  = greenBase;
  307.         buffer[2]  = blueBase;
  308.         colorIndex = colorIndexBase;
  309.         maxIndex = pseudoColorSize-1;
  310.         for ( i = 0;  i < mapSize; i++ )
  311.         {/* for all pixels */
  312.             /*--------------------------------------------*\
  313.             |    Error Checking
  314.             \*--------------------------------------------*/
  315.             *colorIndex = MAX_VAL(*colorIndex,0);
  316.             *colorIndex = MIN_VAL(*colorIndex,maxIndex);
  317.         
  318.             /*--------------------------------------------*\
  319.             |    Remap the rgb based on the color index
  320.             \*--------------------------------------------*/
  321.             indx = 3 * (int)*colorIndex;
  322.             for( j = 0; j < limKeep; j++ )
  323.                 *(buffer[j]++) = pseudoColorMap[indx+j].Int;
  324.             
  325.             /*--------------------------------------------*\
  326.             |    Next index please...
  327.             \*--------------------------------------------*/
  328.             colorIndex++;
  329.         }/* end for all pixels */
  330.  
  331.     }/* end if pseudo coloring should be done */
  332.  
  333.  
  334.     /*------------------------------------------------------------*\
  335.     |    Free the temp space.
  336.     \*------------------------------------------------------------*/
  337.     free((char *)colorIndexBase);
  338.     colorIndexBase = NULL;
  339.  
  340. #define GX4000
  341. #ifdef GX4000
  342.     /*------------------------------------------------------------*\
  343.     |    The GX4000 FORTRAN pixmap call can't handle 0's
  344.     \*------------------------------------------------------------*/
  345.     buffer[0]  = redBase;
  346.     buffer[1]  = greenBase;
  347.     buffer[2]  = blueBase;
  348.     for ( i = 0;  i < mapSize; i++ )
  349.     {
  350.         for( j = 0; j < limKeep; j++ )
  351.         {
  352.             if ( *buffer[j] == 0 )
  353.                 *buffer[j] = 1;
  354.             buffer[j]++;
  355.         }
  356.     }
  357. #endif /* GX4000 */
  358. }/* bif_endpixelbuffer */
  359.  
  360. /*--------------------------------------------------------------------*\
  361. | Procedure     :    keepPixel()
  362. |---------------------------------------------------------------------
  363. | Description   :    Keep the currently stored pixel.
  364. |---------------------------------------------------------------------
  365. | Return        :    None.
  366. \*--------------------------------------------------------------------*/
  367. storePixel()
  368.  
  369. {/* storePixel */
  370.     int i, repli;
  371.  
  372.     /*------------------------------------------------------------*\
  373.     |    Make sure the pixStore is full (by replication)
  374.     \*------------------------------------------------------------*/
  375.     repli = numStored - 1;
  376.     for( i = numStored; i < limKeep; i++ )
  377.         pixStore[i] = pixStore[repli];
  378.  
  379.     /*------------------------------------------------------------*\
  380.     |    Keep only the data we want too keep
  381.     \*------------------------------------------------------------*/
  382.     RUN_LENGTH(colorIndex,runLength,intStore[0]);
  383.     for( i = 0; i < limKeep; i++ )
  384.         RUN_LENGTH(buffer[i],runLength,pixStore[i]);
  385.  
  386.     numStored = 0;
  387.     numKept  += runLength;
  388.  
  389.     if ( numKept  >= mapSize )
  390.         valType = BUFFER_FULL;
  391.     else if ( numKept  >= endOfSeg )
  392.         valType = SEG_HEADER;
  393.  
  394. }/* storePixel */
  395.