File: loadlum.c

package info (click to toggle)
glut 3.6-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,084 kB
  • ctags: 15,234
  • sloc: ansic: 131,032; makefile: 2,129; ada: 2,012; yacc: 473; fortran: 290; lex: 131; sed: 49; csh: 38
file content (182 lines) | stat: -rw-r--r-- 4,045 bytes parent folder | download | duplicates (5)
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

/* texture.c - by David Blythe, SGI */

/* load_luminace is a simplistic routine for reading an SGI .bw image file. */

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

#include "loadlum.h"

typedef struct _ImageRec {
  unsigned short imagic;
  unsigned short type;
  unsigned short dim;
  unsigned short xsize, ysize, zsize;
  unsigned int min, max;
  unsigned int wasteBytes;
  char name[80];
  unsigned long colorMap;
  FILE *file;
  unsigned char *tmp;
  unsigned long rleEnd;
  unsigned int *rowStart;
  int *rowSize;
} ImageRec;

static void
ConvertShort(unsigned short *array, unsigned int length)
{
  unsigned short b1, b2;
  unsigned char *ptr;

  ptr = (unsigned char *) array;
  while (length--) {
    b1 = *ptr++;
    b2 = *ptr++;
    *array++ = (b1 << 8) | (b2);
  }
}

static void
ConvertUint(unsigned *array, unsigned int length)
{
  unsigned int b1, b2, b3, b4;
  unsigned char *ptr;

  ptr = (unsigned char *) array;
  while (length--) {
    b1 = *ptr++;
    b2 = *ptr++;
    b3 = *ptr++;
    b4 = *ptr++;
    *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
  }
}

static ImageRec *
ImageOpen(char *fileName)
{
  union {
    int testWord;
    char testByte[4];
  } endianTest;
  ImageRec *image;
  int swapFlag, x;

  endianTest.testWord = 1;
  if (endianTest.testByte[0] == 1)
    swapFlag = 1;
  else
    swapFlag = 0;

  image = (ImageRec *) malloc(sizeof(ImageRec));
  if (image == NULL) {
    fprintf(stderr, "Out of memory!\n");
    exit(1);
  }
  if ((image->file = fopen(fileName, "rb")) == NULL) {
    perror(fileName);
    exit(1);
  }
  fread(image, 1, 12, image->file);

  if (swapFlag)
    ConvertShort(&image->imagic, 6);
  image->tmp = (unsigned char *) malloc(image->xsize * 256);
  if (image->tmp == NULL) {
    fprintf(stderr, "Out of memory!\n");
    exit(1);
  }
  if ((image->type & 0xFF00) == 0x0100) {
    x = image->ysize * image->zsize * (int) sizeof(unsigned);
    image->rowStart = (unsigned *) malloc(x);
    image->rowSize = (int *) malloc(x);
    if (image->rowStart == NULL || image->rowSize == NULL) {
      fprintf(stderr, "Out of memory!\n");
      exit(1);
    }
    image->rleEnd = 512 + (2 * x);
    fseek(image->file, 512, SEEK_SET);
    fread(image->rowStart, 1, x, image->file);
    fread(image->rowSize, 1, x, image->file);
    if (swapFlag) {
      ConvertUint(image->rowStart, x / (int) sizeof(unsigned));
      ConvertUint((unsigned *) image->rowSize, x / (int) sizeof(int));
    }
  }
  return image;
}

static void
ImageClose(ImageRec * image)
{
  fclose(image->file);
  free(image->tmp);
  free(image);
}

static void
ImageGetRow(ImageRec * image, unsigned char *buf, int y, int z)
{
  unsigned char *iPtr, *oPtr, pixel;
  int count;

  if ((image->type & 0xFF00) == 0x0100) {
    fseek(image->file, (long) image->rowStart[y + z * image->ysize], SEEK_SET);
    fread(image->tmp, 1, (unsigned int) image->rowSize[y + z * image->ysize],
      image->file);
    iPtr = image->tmp;
    oPtr = buf;
    for (;;) {
      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(image->file, 512 + (y * image->xsize) + (z * image->xsize * image->ysize),
      SEEK_SET);
    fread(buf, 1, image->xsize, image->file);
  }
}

unsigned char *
load_luminance(char *name, int *width, int *height, int *components)
{
  unsigned char *base, *lptr;
  ImageRec *image;
  int y;

  image = ImageOpen(name);

  if (!image)
    return NULL;
  if (image->zsize != 1)
    return NULL;

  *width = image->xsize;
  *height = image->ysize;
  *components = image->zsize;

  base = (unsigned char *)
    malloc(image->xsize * image->ysize * sizeof(unsigned char));
  if (!base)
    return NULL;
  lptr = base;
  for (y = 0; y < image->ysize; y++) {
    ImageGetRow(image, lptr, y, 0);
    lptr += image->xsize;
  }
  ImageClose(image);
  return base;
}