1/*
2Copyright (c) 2013, Broadcom Europe Ltd
3Copyright (c) 2013, Tim Gover
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the copyright holder nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
21DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#include "tga.h"
30#include <string.h>
31
32#define TGA_WRITE(FP, F) \
33 if (fwrite((&F), sizeof(F), 1, (FP)) != 1) goto write_fail
34int write_tga(FILE *fp, int width, int height,
35 uint8_t *buffer, size_t buffer_size)
36{
37 struct tga_header header;
38 memset(&header, 0, sizeof(header));
39 header.image_type = tga_type_true_color;
40 header.image_info.width = width;
41 header.image_info.y_origin = height;
42 header.image_info.height = height;
43 header.image_info.bpp = 32;
44
45 TGA_WRITE(fp, header.id_length);
46 TGA_WRITE(fp, header.color_map_type);
47 TGA_WRITE(fp, header.image_type);
48 TGA_WRITE(fp, header.colormap_info.offset);
49 TGA_WRITE(fp, header.colormap_info.length);
50 TGA_WRITE(fp, header.colormap_info.bpp);
51 TGA_WRITE(fp, header.image_info.x_origin);
52 TGA_WRITE(fp, header.image_info.y_origin);
53 TGA_WRITE(fp, header.image_info.width);
54 TGA_WRITE(fp, header.image_info.height);
55 TGA_WRITE(fp, header.image_info.bpp);
56 TGA_WRITE(fp, header.image_info.descriptor);
57
58 if (fwrite(buffer, 1, buffer_size, fp) != buffer_size)
59 goto write_fail;
60
61 return 0;
62write_fail:
63 return -1;
64}
65
66#define TGA_READ(FP, F) if (fread((&F), sizeof(F), 1, (FP)) != 1) goto read_fail
67
68static int read_header(FILE *fp, struct tga_header *header) {
69 TGA_READ(fp, header->id_length);
70 TGA_READ(fp, header->color_map_type);
71 TGA_READ(fp, header->image_type);
72 TGA_READ(fp, header->colormap_info.offset);
73 TGA_READ(fp, header->colormap_info.length);
74 TGA_READ(fp, header->colormap_info.bpp);
75 TGA_READ(fp, header->image_info.x_origin);
76 TGA_READ(fp, header->image_info.y_origin);
77 TGA_READ(fp, header->image_info.width);
78 TGA_READ(fp, header->image_info.height);
79 TGA_READ(fp, header->image_info.bpp);
80 TGA_READ(fp, header->image_info.descriptor);
81
82 return 0;
83
84read_fail:
85 return -1;
86}
87
88unsigned char *load_tga(const char *filename, struct tga_header *header) {
89 unsigned char *image = NULL;
90 FILE *fp = fopen(filename, "r");
91 if (fp) {
92 if(read_header(fp, header) == 0) {
93 if (header->image_type == tga_type_true_color &&
94 (header->image_info.bpp == 24 ||
95 header->image_info.bpp == 32)) {
96 int buflen = header->image_info.width *
97 header->image_info.height * (header->image_info.bpp / 8);
98 image = malloc(buflen);
99 if (image) {
100 if (header->id_length)
101 fseek(fp, SEEK_CUR, header->id_length);
102
103 if (fread(image, 1, buflen, fp) != buflen) {
104 free(image);
105 image = NULL;
106 }
107 }
108 }
109 }
110 fclose(fp);
111 }
112 return image;
113}
114