home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / siggraphCD / lib / libtk / image.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.6 KB  |  244 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <stdlib.h> 
  39. #include <string.h>
  40. #include "tk.h"
  41. #include "private.h"
  42.  
  43. /******************************************************************************/
  44.  
  45. typedef struct _rawImageRec {
  46.     unsigned short imagic;
  47.     unsigned short type;
  48.     unsigned short dim;
  49.     unsigned short sizeX, sizeY, sizeZ;
  50.     unsigned long min, max;
  51.     unsigned long wasteBytes;
  52.     char name[80];
  53.     unsigned long colorMap;
  54.     FILE *file;
  55.     unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  56.     unsigned long rleEnd;
  57.     GLuint *rowStart;
  58.     GLint *rowSize;
  59. } rawImageRec;
  60.  
  61. /******************************************************************************/
  62.  
  63. static void ConvertShort(unsigned short *array, long length)
  64. {
  65.     unsigned long b1, b2;
  66.     unsigned char *ptr;
  67.  
  68.     ptr = (unsigned char *)array;
  69.     while (length--) {
  70.     b1 = *ptr++;
  71.     b2 = *ptr++;
  72.     *array++ = (b1 << 8) | (b2);
  73.     }
  74. }
  75.  
  76. static void ConvertLong(GLuint *array, long length)
  77. {
  78.     unsigned long b1, b2, b3, b4;
  79.     unsigned char *ptr;
  80.  
  81.     ptr = (unsigned char *)array;
  82.     while (length--) {
  83.     b1 = *ptr++;
  84.     b2 = *ptr++;
  85.     b3 = *ptr++;
  86.     b4 = *ptr++;
  87.     *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
  88.     }
  89. }
  90.  
  91. static rawImageRec *RawImageOpen(char *fileName)
  92. {
  93.     union {
  94.     int testWord;
  95.     char testByte[4];
  96.     } endianTest;
  97.     rawImageRec *raw;
  98.     GLenum swapFlag;
  99.     int x;
  100.  
  101.     endianTest.testWord = 1;
  102.     if (endianTest.testByte[0] == 1) {
  103.     swapFlag = GL_TRUE;
  104.     } else {
  105.     swapFlag = GL_FALSE;
  106.     }
  107.  
  108.     raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  109.     if (raw == NULL) {
  110.     fprintf(stderr, "Out of memory!\n");
  111.     tkQuit();
  112.     }
  113.     if ((raw->file = fopen(fileName, "rb")) == NULL) {
  114.     perror(fileName);
  115.     tkQuit();
  116.     }
  117.  
  118.     fread(raw, 1, 12, raw->file);
  119.  
  120.     if (swapFlag) {
  121.     ConvertShort(&raw->imagic, 6);
  122.     }
  123.  
  124.     raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  125.     raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  126.     raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  127.     raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  128.     if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  129.     raw->tmpB == NULL) {
  130.     fprintf(stderr, "Out of memory!\n");
  131.     tkQuit();
  132.     }
  133.  
  134.     if ((raw->type & 0xFF00) == 0x0100) {
  135.     x = raw->sizeY * raw->sizeZ * sizeof(GLuint);
  136.     raw->rowStart = (GLuint *)malloc(x);
  137.     raw->rowSize = (GLint *)malloc(x);
  138.     if (raw->rowStart == NULL || raw->rowSize == NULL) {
  139.         fprintf(stderr, "Out of memory!\n");
  140.         tkQuit();
  141.     }
  142.     raw->rleEnd = 512 + (2 * x);
  143.     fseek(raw->file, 512, SEEK_SET);
  144.     fread(raw->rowStart, 1, x, raw->file);
  145.     fread(raw->rowSize, 1, x, raw->file);
  146.     if (swapFlag) {
  147.         ConvertLong(raw->rowStart, x/sizeof(GLuint));
  148.         ConvertLong((GLuint *)raw->rowSize, x/sizeof(GLint));
  149.     }
  150.     }
  151.     return raw;
  152. }
  153.  
  154. static void RawImageClose(rawImageRec *raw)
  155. {
  156.  
  157.     fclose(raw->file);
  158.     free(raw->tmp);
  159.     free(raw->tmpR);
  160.     free(raw->tmpG);
  161.     free(raw->tmpB);
  162.     free(raw);
  163. }
  164.  
  165. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  166. {
  167.     unsigned char *iPtr, *oPtr, pixel;
  168.     int count;
  169.  
  170.     if ((raw->type & 0xFF00) == 0x0100) {
  171.     fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
  172.     fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY],
  173.           raw->file);
  174.  
  175.     iPtr = raw->tmp;
  176.     oPtr = buf;
  177.     while (1) {
  178.         pixel = *iPtr++;
  179.         count = (int)(pixel & 0x7F);
  180.         if (!count) {
  181.         return;
  182.         }
  183.         if (pixel & 0x80) {
  184.         while (count--) {
  185.             *oPtr++ = *iPtr++;
  186.         }
  187.         } else {
  188.         pixel = *iPtr++;
  189.         while (count--) {
  190.             *oPtr++ = pixel;
  191.         }
  192.         }
  193.     }
  194.     } else {
  195.     fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  196.           SEEK_SET);
  197.     fread(buf, 1, raw->sizeX, raw->file);
  198.     }
  199. }
  200.  
  201. static void RawImageGetData(rawImageRec *raw, TK_RGBImageRec *final)
  202. {
  203.     unsigned char *ptr;
  204.     int i, j;
  205.  
  206.     final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  207.     if (final->data == NULL) {
  208.     fprintf(stderr, "Out of memory!\n");
  209.     tkQuit();
  210.     }
  211.  
  212.     ptr = final->data;
  213.     for (i = 0; i < raw->sizeY; i++) {
  214.     RawImageGetRow(raw, raw->tmpR, i, 0);
  215.     RawImageGetRow(raw, raw->tmpG, i, 1);
  216.     RawImageGetRow(raw, raw->tmpB, i, 2);
  217.     for (j = 0; j < raw->sizeX; j++) {
  218.         *ptr++ = *(raw->tmpR + j);
  219.         *ptr++ = *(raw->tmpG + j);
  220.         *ptr++ = *(raw->tmpB + j);
  221.     }
  222.     }
  223. }
  224.  
  225. TK_RGBImageRec *tkRGBImageLoad(char *fileName)
  226. {
  227.     rawImageRec *raw;
  228.     TK_RGBImageRec *final;
  229.  
  230.     raw = RawImageOpen(fileName);
  231.     final = (TK_RGBImageRec *)malloc(sizeof(TK_RGBImageRec));
  232.     if (final == NULL) {
  233.     fprintf(stderr, "Out of memory!\n");
  234.     tkQuit();
  235.     }
  236.     final->sizeX = raw->sizeX;
  237.     final->sizeY = raw->sizeY;
  238.     RawImageGetData(raw, final);
  239.     RawImageClose(raw);
  240.     return final;
  241. }
  242.  
  243. /******************************************************************************/
  244.