1 | /*
|
---|
2 |
|
---|
3 | gbmiax.c - IBM Image Access eXecutive support
|
---|
4 |
|
---|
5 | Reads array as 8 bit greyscale.
|
---|
6 | Writes grey equivelent of passed in 8 bit colour data (no palette written).
|
---|
7 | Input options: width=# (default: 512)
|
---|
8 | Output options: r,g,b,k (default: k)
|
---|
9 |
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*...sincludes:0:*/
|
---|
13 | #include <stdio.h>
|
---|
14 | #include <ctype.h>
|
---|
15 | #include <stddef.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include "gbm.h"
|
---|
19 | #include "gbmhelp.h"
|
---|
20 |
|
---|
21 | /*...vgbm\46\h:0:*/
|
---|
22 | /*...vgbmhelp\46\h:0:*/
|
---|
23 | /*...e*/
|
---|
24 |
|
---|
25 | /*...suseful:0:*/
|
---|
26 | #define low_byte(w) ((byte) ((w)&0x00ff) )
|
---|
27 | #define high_byte(w) ((byte) (((w)&0xff00)>>8))
|
---|
28 | #define make_word(a,b) (((word)a) + (((word)b) << 8))
|
---|
29 | /*...e*/
|
---|
30 | /*...smake_output_palette:0:*/
|
---|
31 | #define SW4(a,b,c,d) ((a)*8+(b)*4+(c)*2+(d))
|
---|
32 |
|
---|
33 | static BOOLEAN make_output_palette(const GBMRGB gbmrgb[], byte grey[], const char *opt)
|
---|
34 | {
|
---|
35 | BOOLEAN k = ( gbm_find_word(opt, "k") != NULL );
|
---|
36 | BOOLEAN r = ( gbm_find_word(opt, "r") != NULL );
|
---|
37 | BOOLEAN g = ( gbm_find_word(opt, "g") != NULL );
|
---|
38 | BOOLEAN b = ( gbm_find_word(opt, "b") != NULL );
|
---|
39 | int i;
|
---|
40 |
|
---|
41 | switch ( SW4(k,r,g,b) )
|
---|
42 | {
|
---|
43 | case SW4(0,0,0,0):
|
---|
44 | /* Default is the same as "k" */
|
---|
45 | case SW4(1,0,0,0):
|
---|
46 | for ( i = 0; i < 0x100; i++ )
|
---|
47 | grey[i] = (byte) ( ((word) gbmrgb[i].r * 77U +
|
---|
48 | (word) gbmrgb[i].g * 150U +
|
---|
49 | (word) gbmrgb[i].b * 29U) >> 8 );
|
---|
50 | return TRUE;
|
---|
51 | case SW4(0,1,0,0):
|
---|
52 | for ( i = 0; i < 0x100; i++ )
|
---|
53 | grey[i] = gbmrgb[i].r;
|
---|
54 | return TRUE;
|
---|
55 | case SW4(0,0,1,0):
|
---|
56 | for ( i = 0; i < 0x100; i++ )
|
---|
57 | grey[i] = gbmrgb[i].g;
|
---|
58 | return TRUE;
|
---|
59 | case SW4(0,0,0,1):
|
---|
60 | for ( i = 0; i < 0x100; i++ )
|
---|
61 | grey[i] = gbmrgb[i].b;
|
---|
62 | return TRUE;
|
---|
63 | }
|
---|
64 | return FALSE;
|
---|
65 | }
|
---|
66 | /*...e*/
|
---|
67 |
|
---|
68 | static GBMFT iax_gbmft =
|
---|
69 | {
|
---|
70 | "IAX",
|
---|
71 | "IBM Image Access eXecutive",
|
---|
72 | "IAX",
|
---|
73 | GBM_FT_R8|
|
---|
74 | GBM_FT_W8,
|
---|
75 | };
|
---|
76 |
|
---|
77 | #define GBM_ERR_IAX_SIZE ((GBM_ERR) 1500)
|
---|
78 |
|
---|
79 | /*...siax_qft:0:*/
|
---|
80 | GBM_ERR iax_qft(GBMFT *gbmft)
|
---|
81 | {
|
---|
82 | *gbmft = iax_gbmft;
|
---|
83 | return GBM_ERR_OK;
|
---|
84 | }
|
---|
85 | /*...e*/
|
---|
86 | /*...siax_rhdr:0:*/
|
---|
87 | GBM_ERR iax_rhdr(const char *fn, int fd, GBM *gbm, const char *opt)
|
---|
88 | {
|
---|
89 | long length;
|
---|
90 | int w, h;
|
---|
91 | const char *width;
|
---|
92 |
|
---|
93 | fn=fn; fd=fd; /* Suppress 'unref arg' compiler warnings */
|
---|
94 |
|
---|
95 | length = gbm_file_lseek(fd, 0L, SEEK_END);
|
---|
96 | gbm_file_lseek(fd, 0L, SEEK_SET);
|
---|
97 |
|
---|
98 | if ( (width = gbm_find_word_prefix(opt, "width=")) != NULL )
|
---|
99 | sscanf(width + 6, "%d", &w);
|
---|
100 | else
|
---|
101 | w = 512;
|
---|
102 |
|
---|
103 | h = (int) (length / w);
|
---|
104 |
|
---|
105 | if ( w <= 0 || h <= 0 )
|
---|
106 | return GBM_ERR_BAD_SIZE;
|
---|
107 |
|
---|
108 | if ( w * h != length )
|
---|
109 | return GBM_ERR_IAX_SIZE;
|
---|
110 |
|
---|
111 | gbm->w = w;
|
---|
112 | gbm->h = h;
|
---|
113 | gbm->bpp = 8;
|
---|
114 |
|
---|
115 | return GBM_ERR_OK;
|
---|
116 | }
|
---|
117 | /*...e*/
|
---|
118 | /*...siax_rpal:0:*/
|
---|
119 | GBM_ERR iax_rpal(int fd, GBM *gbm, GBMRGB *gbmrgb)
|
---|
120 | {
|
---|
121 | int i;
|
---|
122 |
|
---|
123 | fd=fd; gbm=gbm; /* Suppress 'unref arg' compiler warnings */
|
---|
124 |
|
---|
125 | for ( i = 0; i < 0x100; i++ )
|
---|
126 | gbmrgb[i].r =
|
---|
127 | gbmrgb[i].g =
|
---|
128 | gbmrgb[i].b = (byte) i;
|
---|
129 |
|
---|
130 | return GBM_ERR_OK;
|
---|
131 | }
|
---|
132 | /*...e*/
|
---|
133 | /*...siax_rdata:0:*/
|
---|
134 | GBM_ERR iax_rdata(int fd, GBM *gbm, byte *data)
|
---|
135 | {
|
---|
136 | int i, stride;
|
---|
137 | byte *p;
|
---|
138 |
|
---|
139 | stride = ((gbm->w + 3) & ~3);
|
---|
140 | p = data + ((gbm->h - 1) * stride);
|
---|
141 | for ( i = gbm->h - 1; i >= 0; i-- )
|
---|
142 | {
|
---|
143 | gbm_file_read(fd, p, gbm->w);
|
---|
144 | p -= stride;
|
---|
145 | }
|
---|
146 | return GBM_ERR_OK;
|
---|
147 | }
|
---|
148 | /*...e*/
|
---|
149 | /*...siax_w:0:*/
|
---|
150 | GBM_ERR iax_w(const char *fn, int fd, const GBM *gbm, const GBMRGB *gbmrgb, const byte *data, const char *opt)
|
---|
151 | {
|
---|
152 | int i, j, stride;
|
---|
153 | byte grey[0x100];
|
---|
154 | const byte *p;
|
---|
155 | byte *linebuf;
|
---|
156 |
|
---|
157 | fn=fn; /* Suppress 'unref arg' compiler warning */
|
---|
158 |
|
---|
159 | if ( gbm->bpp != 8 )
|
---|
160 | return GBM_ERR_NOT_SUPP;
|
---|
161 |
|
---|
162 | if ( !make_output_palette(gbmrgb, grey, opt) )
|
---|
163 | return GBM_ERR_BAD_OPTION;
|
---|
164 |
|
---|
165 | if ( (linebuf = malloc((size_t) gbm->w)) == NULL )
|
---|
166 | return GBM_ERR_MEM;
|
---|
167 |
|
---|
168 | stride = ((gbm->w + 3) & ~3);
|
---|
169 | p = data + ((gbm->h - 1) * stride);
|
---|
170 | for ( i = gbm->h - 1; i >= 0; i-- )
|
---|
171 | {
|
---|
172 | for ( j = 0; j < gbm->w; j++ )
|
---|
173 | linebuf[j] = grey[p[j]];
|
---|
174 | gbm_file_write(fd, linebuf, gbm->w);
|
---|
175 | p -= stride;
|
---|
176 | }
|
---|
177 |
|
---|
178 | free(linebuf);
|
---|
179 |
|
---|
180 | return GBM_ERR_OK;
|
---|
181 | }
|
---|
182 | /*...e*/
|
---|
183 | /*...siax_err:0:*/
|
---|
184 | const char *iax_err(GBM_ERR rc)
|
---|
185 | {
|
---|
186 | switch ( (int) rc )
|
---|
187 | {
|
---|
188 | case GBM_ERR_IAX_SIZE:
|
---|
189 | return "file length is not a multiple of its width";
|
---|
190 | }
|
---|
191 | return NULL;
|
---|
192 | }
|
---|
193 | /*...e*/
|
---|