Add Z-buffer, fixes issue with pyramidal studs
[render.git] / tga.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "tga.h"
4
5 struct tga *tga_read(const char *filename) {
6   FILE *stream = fopen(filename, "rb");
7   if (stream == NULL) {
8     perror(filename);
9     exit(EXIT_FAILURE);
10   }
11
12   struct tga *p = malloc(sizeof(struct tga));
13   if (p == NULL) {
14     perror("malloc()");
15     exit(EXIT_FAILURE);
16   }
17   if (fread(&p->head, sizeof(struct tga_head), 1, stream) != 1) {
18     perror("fread()");
19     exit(EXIT_FAILURE);
20   }
21
22   if (p->head.image_type != 2) {
23     fprintf(stderr, "image_type %d, should be 2\n", p->head.image_type);
24     exit(EXIT_FAILURE);
25   }
26
27   if (fseek(stream, p->head.id_length, SEEK_CUR)) {
28     perror("fseek()");
29     exit(EXIT_FAILURE);
30   }
31
32   p->data = array_new3(
33     p->head.image_height,
34     p->head.image_width,
35     p->head.image_depth / 8
36   );
37   if (fread(array_index0(p->data), p->data->stride[0], 1, stream) != 1) {
38     perror("fread()");
39     exit(EXIT_FAILURE);
40   }
41
42   fclose(stream);
43   return p;
44 }
45
46 void tga_free(struct tga *self) {
47   array_free(self->data);
48   free(self);
49 }