aboutsummaryrefslogtreecommitdiffstats
path: root/progs/util/imagesgi.cpp
blob: f5128aabec0bc35fefca66bf8597b5cdecbe24fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/******************************************************************************
** Filename       : imageSgi.cpp
**                  UNCLASSIFIED
**
** Description    : Utility to read SGI image format files.  This code was
**                  originally a SGI image loading utility provided with the
**                  Mesa 3D library @ http://www.mesa3d.org by Brain Paul.
**                  This has been extended to read all SGI image formats
**                  (e.g. INT, INTA, RGB, RGBA).
**
** Revision History:
**   Date       Name   Description
**   06/07/99   BRC    Initial Release
**
** Note:
**
** The SGI Image Data (if not RLE)
**
**   If the image is stored verbatim (without RLE), then image data directly
**   follows the 512 byte header.  The data for each scanline of the first
**   channel is written first.  If the image has more than 1 channel, all
**   the data for the first channel is written, followed by the remaining
**   channels.  If the BPC value is 1, then each scanline is written as XSIZE
**   bytes.  If the BPC value is 2, then each scanline is written as XSIZE
**   shorts.  These shorts are stored in the byte order described above.
**
******************************************************************************/
#define __IMAGESGI_CPP

#include "imagesgi.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>

struct sImageSgiRaw
{
   struct sImageSgiHeader header;
   unsigned char *chan0;
   unsigned char *chan1;
   unsigned char *chan2;
   unsigned char *chan3;
   unsigned int *rowStart;
   int *rowSize;
};

// Static routines
static struct sImageSgiRaw *ImageSgiRawOpen(char const * const fileName);
static void ImageSgiRawClose(struct sImageSgiRaw *raw);
static void ImageSgiRawGetRow(struct sImageSgiRaw *raw, unsigned char *buf,
   int y, int z);
static void ImageSgiRawGetData(struct sImageSgiRaw *raw, struct sImageSgi 
*final);
static void *SwitchEndian16(void *value);
static void *SwitchEndian32(void *value);

// Static variables
FILE *mFp = NULL;
unsigned char *mChanTmp = NULL;


/*****************************************************************************/
struct sImageSgi *ImageSgiOpen(char const * const fileName)
{
   struct sImageSgiRaw *raw = NULL;
   struct sImageSgi *final = NULL;

   raw = ImageSgiRawOpen(fileName);
   final = new struct sImageSgi;

   assert(final);
   if(final)
   {
      final->header = raw->header;
      final->data = NULL;
      ImageSgiRawGetData(raw, final);
      ImageSgiRawClose(raw);
   }

   return final;
} // ImageSgiRawOpen


/*****************************************************************************/
void ImageSgiClose(struct sImageSgi *image)
{

   if(image)
   {
      if(image->data)
         delete[] image->data;
      image->data = NULL;
      delete image;
   }
   image = NULL;

   return;
} // ImageSgiClose


/*****************************************************************************/
static struct sImageSgiRaw *ImageSgiRawOpen(char const * const fileName)
{
   struct sImageSgiRaw *raw = NULL;
   int x;
   int i;
   bool swapFlag = false;
   union
   {
      int testWord;
      char testByte[4];
   } endianTest;
   endianTest.testWord = 1;

   // Determine endianess of platform.
   if(endianTest.testByte[0] == 1)
      swapFlag = true;
   else
      swapFlag = false;

   raw = new struct sImageSgiRaw;

   assert(raw);
   if(raw)
   {
      raw->chan0 = NULL;
      raw->chan1 = NULL;
      raw->chan2 = NULL;
      raw->chan3 = NULL;
      raw->rowStart = NULL;
      raw->rowSize = NULL;
      mFp = fopen(fileName, "rb");
      assert(mFp);

      fread(&raw->header, sizeof(struct sImageSgiHeader), 1, mFp);
      if(swapFlag == true)
      {
         SwitchEndian16(&raw->header.magic);
         SwitchEndian16(&raw->header.type);
         SwitchEndian16(&raw->header.dim);
         SwitchEndian16(&raw->header.xsize);
         SwitchEndian16(&raw->header.ysize);
         SwitchEndian16(&raw->header.zsize);
      }

      mChanTmp = new unsigned char[raw->header.xsize * raw->header.ysize];
      assert(mChanTmp);
      switch(raw->header.zsize)
      {
      case 4:
         raw->chan3 = new unsigned char[raw->header.xsize * 
raw->header.ysize];
         assert(raw->chan3);
      case 3:
         raw->chan2 = new unsigned char[raw->header.xsize * 
raw->header.ysize];
         assert(raw->chan2);
      case 2:
         raw->chan1 = new unsigned char[raw->header.xsize * 
raw->header.ysize];
         assert(raw->chan1);
      case 1:
         raw->chan0 = new unsigned char[raw->header.xsize * 
raw->header.ysize];
         assert(raw->chan0);
      }

      if(raw->header.type == IMAGE_SGI_TYPE_RLE)
      {
         x = raw->header.ysize * raw->header.zsize * sizeof(unsigned int);
         raw->rowStart = new unsigned int[x];
         raw->rowSize = new int[x];

         fseek(mFp, sizeof(struct sImageSgiHeader), SEEK_SET);
         fread(raw->rowStart, 1, x, mFp);
         fread(raw->rowSize, 1, x, mFp);

         if(swapFlag == true)
         {
            for(i=0; i<x/sizeof(unsigned int); i++)
               SwitchEndian32(&raw->rowStart[i]);
            for(i=0; i<x/sizeof(int); i++)
               SwitchEndian32(&raw->rowSize[i]);
         }

      }

   }

   return raw;
} // ImageSgiRawOpen


/*****************************************************************************/
static void ImageSgiRawClose(struct sImageSgiRaw *raw)
{

   fclose(mFp);
   mFp = NULL;

   if(mChanTmp)
      delete[] mChanTmp;
   mChanTmp = NULL;

   if(raw->chan0)
      delete[] raw->chan0;
   raw->chan0 = NULL;

   if(raw->chan1)
      delete[] raw->chan1;
   raw->chan1 = NULL;

   if(raw->chan2)
      delete[] raw->chan2;
   raw->chan2 = NULL;

   if(raw->chan3)
      delete[] raw->chan3;
   raw->chan3 = NULL;

   if(raw)
      delete raw;
   raw = NULL;

   return;
} // ImageSgiRawClose


/*****************************************************************************/
static void ImageSgiRawGetRow(struct sImageSgiRaw *raw, unsigned char *buf,
   int y, int z)
{
   unsigned char *iPtr = NULL;
   unsigned char *oPtr = NULL;
   unsigned char pixel;
   int count;

   if((raw->header.type & 0xFF00) == 0x0100)
   {
      fseek(mFp, raw->rowStart[y+z*raw->header.ysize], SEEK_SET);
      fread(mChanTmp, 1, (unsigned int)raw->rowSize[y+z*raw->header.ysize], 
mFp);
      iPtr = mChanTmp;
      oPtr = buf;
      while(1)
      {
         pixel = *iPtr++;
         count = (int)(pixel & 0x7F);
         if(!count)
         {
            return;
         }
         if (pixel & 0x80)
         {
            while (count--)
            {
               *oPtr++ = *iPtr++;
            }
         }
         else
         {
            pixel = *iPtr++;
            while (count--)
            {
               *oPtr++ = pixel;
            }
         }
      }
   }
   else
   {
      fseek(mFp,
         sizeof(struct sImageSgiHeader)+(y*raw->header.xsize) +
            (z*raw->header.xsize*raw->header.ysize),
         SEEK_SET);
      fread(buf, 1, raw->header.xsize, mFp);
   }

   return;
} // ImageSgiRawGetRow


/*****************************************************************************/
static void ImageSgiRawGetData(struct sImageSgiRaw *raw, struct sImageSgi 
*final)
{
   unsigned char *ptr = NULL;
   int i, j;

   final->data =
      new unsigned 
char[raw->header.xsize*raw->header.ysize*raw->header.zsize];
   assert(final->data);

   ptr = final->data;
   for(i=0; i<raw->header.ysize; i++)
   {
      switch(raw->header.zsize)
      {
      case 1:
         ImageSgiRawGetRow(raw, raw->chan0, i, 0);
         for(j=0; j<raw->header.xsize; j++)
            *(ptr++) = raw->chan0[j];
         break;
      case 2:
         ImageSgiRawGetRow(raw, raw->chan0, i, 0);
         ImageSgiRawGetRow(raw, raw->chan1, i, 1);
         for(j=0; j<raw->header.xsize; j++)
         {
            *(ptr++) = raw->chan0[j];
            *(ptr++) = raw->chan1[j];
         }
         break;
      case 3:
         ImageSgiRawGetRow(raw, raw->chan0, i, 0);
         ImageSgiRawGetRow(raw, raw->chan1, i, 1);
         ImageSgiRawGetRow(raw, raw->chan2, i, 2);
         for(j=0; j<raw->header.xsize; j++)
         {
            *(ptr++) = raw->chan0[j];
            *(ptr++) = raw->chan1[j];
            *(ptr++) = raw->chan2[j];
         }
         break;
      case 4:
         ImageSgiRawGetRow(raw, raw->chan0, i, 0);
         ImageSgiRawGetRow(raw, raw->chan1, i, 1);
         ImageSgiRawGetRow(raw, raw->chan2, i, 2);
         ImageSgiRawGetRow(raw, raw->chan3, i, 3);
         for(j=0; j<raw->header.xsize; j++)
         {
            *(ptr++) = raw->chan0[j];
            *(ptr++) = raw->chan1[j];
            *(ptr++) = raw->chan2[j];
            *(ptr++) = raw->chan3[j];
         }
         break;
      }
   }

   return;
} // ImageSgiRawGetData


/*****************************************************************************/
static void *SwitchEndian16(void *value)
{
   short value16 = *(short *) value;
   value16 = ((value16 & 0xff00) >> 8L) +
             ((value16 & 0x00ff) << 8L);
   *(short *)value = value16;
   return value;
} // SwitchEndian16


/*****************************************************************************/
static void *SwitchEndian32(void *value)
{
   int value32 = *(int *) value;
   value32 = ((value32 & 0xff000000) >> 24L) +
             ((value32 & 0x00ff0000) >> 8)   +
             ((value32 & 0x0000ff00) << 8)   +
             ((value32 & 0x000000ff) << 24L);
   *(int *)value = value32;
   return value;
} // SwitchEndian32