MCPcopy Create free account
hub / github.com/NetHack/NetHack / depack_img

Function depack_img

outdated/win/gem/load_img.c:163–291  ·  view source on GitHub ↗

Loads & depacks IMG (0 if succeded, else error). */ Bitplanes are one after another in address IMG_HEADER.addr. */

Source from the content-addressed store, hash-verified

161/* Loads & depacks IMG (0 if succeded, else error). */
162/* Bitplanes are one after another in address IMG_HEADER.addr. */
163int
164depack_img(char *name, IMG_header *pic)
165{
166 int b, line, plane, width, word_aligned, opcode, patt_len, pal_size,
167 byte_repeat, patt_repeat, scan_repeat, error = FALSE;
168 char *pattern, *to, *endline, *puffer, sol_pat;
169 long size;
170 FILE *fp;
171
172 if ((fp = fopen(name, "rb")) == NULL)
173 return (ERR_FILE);
174
175 setvbuf(fp, NULL, _IOLBF, BUFSIZ);
176
177 /* read header info (bw & ximg) into image structure */
178 fread((char *) &(pic->version), 2, 8 + 3, fp);
179
180 /* only 2-256 color imgs */
181 if (pic->planes < 1 || pic->planes > 8) {
182 error = ERR_COLOR;
183 goto end_depack;
184 }
185
186 /* if XIMG, read info */
187 if (pic->magic == XIMG && pic->paltype == 0) {
188 pal_size = (1 << pic->planes) * 3 * 2;
189 if ((pic->palette = (short *) calloc(1, pal_size))) {
190 fread((char *) pic->palette, 1, pal_size, fp);
191 }
192 } else {
193 pic->palette = NULL;
194 }
195
196 /* width in bytes word aliged */
197 word_aligned = (pic->img_w + 15) >> 4;
198 word_aligned <<= 1;
199
200 /* width byte aligned */
201 width = (pic->img_w + 7) >> 3;
202
203 /* allocate memory for the picture */
204 free(pic->addr);
205 size = (long) ((long) word_aligned * (long) pic->img_h
206 * (long) pic->planes); /*MAR*/
207
208 /* check for header validity & malloc long... */
209 if (pic->length > 7 && pic->planes < 33 && pic->img_w > 0
210 && pic->img_h > 0) {
211 if (!(pic->addr = (char *) calloc(1, size))) {
212 error = ERR_ALLOC;
213 goto end_depack;
214 }
215 } else {
216 error = ERR_HEADER;
217 goto end_depack;
218 }
219
220 patt_len = pic->pat_len;

Callers 3

mar_gem_initFunction · 0.85
mar_ask_nameFunction · 0.85
mar_display_nhwindowFunction · 0.85

Calls 4

freadFunction · 0.85
fseekFunction · 0.85
fgetcFunction · 0.85
fcloseFunction · 0.85

Tested by

no test coverage detected