Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://coin3d.github.io
https://www.kongsberg.com/en/kogt/
simage_pic.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) Kongsberg Oil & Gas Technologies
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif /* HAVE_CONFIG_H */
20 
21 #ifdef SIMAGE_PIC_SUPPORT
22 
23 #include <simage_pic.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 
28 #define ERROR_NO_ERROR 0
29 #define ERROR_READING_HEADER 1
30 #define ERROR_READING_PALETTE 2
31 #define ERROR_MEMORY 3
32 #define ERROR_READ_ERROR 4
33 
34 static int picerror = ERROR_NO_ERROR;
35 
36 
37 int
38 simage_pic_error(char *buffer, int bufferlen)
39 {
40  switch (picerror) {
41  case ERROR_READING_HEADER:
42  strncpy(buffer, "PIC loader: Error reading header", bufferlen);
43  break;
44  case ERROR_READING_PALETTE:
45  strncpy(buffer, "PIC loader: Error reading palette", bufferlen);
46  break;
47  case ERROR_MEMORY:
48  strncpy(buffer, "PIC loader: Out of memory error", bufferlen);
49  break;
50  case ERROR_READ_ERROR:
51  strncpy(buffer, "PIC loader: Read error", bufferlen);
52  break;
53  }
54  return picerror;
55 }
56 
57 /* byte order workaround *sigh* */
58 
59 static int
60 readint16(FILE *fp, int * res)
61 {
62  unsigned char tmp = 0;
63  unsigned int tmp2;
64  if (fread(&tmp, 1, 1, fp) != 1) return 0;
65  *res = tmp;
66  if (fread(&tmp, 1, 1, fp) != 1) return 0;
67  tmp2 = tmp;
68  tmp2 <<= 8;
69  *res |= tmp2;
70  return 1;
71 }
72 
73 
74 int
75 simage_pic_identify(const char * ptr,
76  const unsigned char *header,
77  int headerlen)
78 {
79  static unsigned char piccmp[] = {0x19, 0x91};
80  if (headerlen < 2) return 0;
81  if (memcmp((const void*)header,
82  (const void*)piccmp, 2) == 0) return 1;
83  return 0;
84 }
85 
86 unsigned char *
87 simage_pic_load(const char *filename,
88  int *width_ret,
89  int *height_ret,
90  int *numComponents_ret)
91 {
92  int w, h, width, height, i, j, format;
93  unsigned char palette[256][3];
94  unsigned char * tmpbuf, * buffer, * ptr;
95 
96  FILE *fp = fopen(filename, "rb");
97  if (!fp) return NULL;
98 
99  picerror = ERROR_NO_ERROR;
100 
101  fseek(fp, 2, SEEK_SET);
102  if (!readint16(fp, &w)) {
103  picerror = ERROR_READING_HEADER;
104  fclose(fp);
105  return NULL;
106  }
107 
108  fseek(fp, 4, SEEK_SET);
109  if (!readint16(fp, &h)) {
110  picerror = ERROR_READING_HEADER;
111  fclose(fp);
112  return NULL;
113  }
114 
115  width = w;
116  height = h;
117 
118  if (width <= 0 || height <= 0) {
119  fclose(fp);
120  return NULL;
121  }
122  fseek(fp, 32, SEEK_SET);
123 
124  if (fread(&palette, 3, 256, fp) != 256) {
125  picerror = ERROR_READING_PALETTE;
126  }
127 
128  tmpbuf = (unsigned char *)malloc(width);
129  buffer = (unsigned char*) malloc((size_t)width*height*3);
130  if (tmpbuf == NULL || buffer == NULL) {
131  picerror = ERROR_MEMORY;
132  if (tmpbuf) free(tmpbuf);
133  if (buffer) free(buffer);
134  fclose(fp);
135  return NULL;
136  }
137  ptr = buffer;
138  for (i = 0; i < height; i++) {
139  if (fread(tmpbuf, 1, width, fp) != (size_t) width) {
140  picerror = ERROR_READ_ERROR;
141  fclose(fp);
142  if (tmpbuf) free(tmpbuf);
143  if (buffer) free(buffer);
144  buffer = NULL;
145  width = height = 0;
146  return NULL;
147  }
148  for (j = 0; j < width; j++) {
149  int idx = tmpbuf[j];
150  *ptr++ = palette[idx][0];
151  *ptr++ = palette[idx][1];
152  *ptr++ = palette[idx][2];
153  }
154  }
155  format = 3;
156  fclose(fp);
157 
158  *width_ret = width;
159  *height_ret = height;
160  *numComponents_ret = format;
161  return buffer;
162 }
163 
164 #endif /* SIMAGE_PIC_SUPPORT */
unsigned char * simage_pic_load(const char *filename, int *width, int *height, int *numComponents)
int simage_pic_identify(const char *filename, const unsigned char *header, int headerlen)
int simage_pic_error(char *buffer, int bufferlen)