-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_tga.c
executable file
·141 lines (123 loc) · 4.39 KB
/
save_tga.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2015-2019 Alaskan Emily, Transnat Games
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "image.h"
#include <stdio.h>
/* There's really no reason to not do RLE, if we do it properly.
* This breaks Corona loading images saved by AImg. But that's Corona's
* problem, not ours.
* Unlike TurboSphere/Sapphire, we actually support homogenous AND
* heterogenous blocks in AImg. This is why it's almost always better to
* save with RLE.
*/
static const uint8_t aimg_max_block_size = 0x7F;
/* Targa dimensions are 16-bit, and blocks do not cross scanlines. Thus x and y are 16-bit. */
static uint8_t aimg_determine_block_size(const struct AImg_Image *from,
uint8_t i, uint16_t x, uint16_t y, unsigned(*cmp)(uint32_t, uint32_t)){
if(i==0)
return aimg_determine_block_size(from, 1, x, y, cmp);
else if((unsigned)(x+i)>=from->w)
return i;
else if(i+1 >= aimg_max_block_size)
return aimg_max_block_size;
else{
if(cmp(*AImg_PixelConst(from, x+i-1, y), *AImg_PixelConst(from, x+i, y)))
return aimg_determine_block_size(from, i+1, x, y, cmp);
else
return i;
}
}
static unsigned aimg_pix_same(uint32_t a, uint32_t b){
return a==b;
}
static unsigned aimg_pix_diff(uint32_t a, uint32_t b){
return a!=b;
}
static const char aimg_tga_id[] = "aimg";
static const char aimg_tga_header[] = {
sizeof(aimg_tga_id), /* id length */
0, /* color map type */
10, /* data type (always RLE RGBA) */
0, 0, 0, 0, 0, /* color map data */
0, 0, 0, 0 /* x, y origin (shorts) */
/* Then write width, height as shorts, then bitsperpixel and "descriptor" as bytes. */
};
static void aimg_lo_hi_short(uint16_t in_, uint8_t out[2]){
uint8_t *in = (void *)(&in_);
out[0] = in[0];
out[1] = in[1];
}
#define TGA_WRITE_RGBA(RGBA_UINT_, FILE_)\
do{\
const uint32_t PIXEL_ = RGBA_UINT_;\
putc(AImg_RawToB(PIXEL_), FILE_);\
putc(AImg_RawToG(PIXEL_), FILE_);\
putc(AImg_RawToR(PIXEL_), FILE_);\
putc(AImg_RawToA(PIXEL_), FILE_);\
}while(0)
static void aimg_write_tga_pixels(const uint32_t *pixels, uint8_t remaining, FILE *file){
if(remaining){
TGA_WRITE_RGBA(*pixels, file);
aimg_write_tga_pixels(pixels+1, remaining-1, file);
}
}
unsigned AIMG_FASTCALL AImg_SaveTGA(const struct AImg_Image *from, const char *path){
FILE *file;
if(from->w > 0xFFFF || from->h > 0xFFFF){
puts("AIMG_LOADPNG_NFORMAT");
return AIMG_LOADPNG_NFORMAT;
}
if(!(file = fopen(path, "wb"))){
puts("AIMG_LOADPNG_NO_FILE");
return AIMG_LOADPNG_NO_FILE;
}
/* Write the header. */
fwrite(aimg_tga_header, sizeof(aimg_tga_header), 1, file);
{
uint8_t dimensions[4];
aimg_lo_hi_short(from->w, dimensions);
aimg_lo_hi_short(from->h, dimensions + 2);
fwrite(dimensions, 1, 4, file);
}
putc(32, file);
putc(0x20 | 0x09, file);
fwrite(aimg_tga_id, sizeof(aimg_tga_id), 1, file);
{
uint16_t x = 0, y = 0, run;
start_image:
if((run = aimg_determine_block_size(from, 0, x, y, aimg_pix_same))==1){
run = aimg_determine_block_size(from, 0, x, y, aimg_pix_diff);
putc(run-1, file);
aimg_write_tga_pixels(AImg_PixelConst(from, x, y), run, file);
}
else{
putc(0x80 | (run-1), file);
TGA_WRITE_RGBA(*AImg_PixelConst(from, x, y), file);
}
x+=run;
if(x>=from->w){
x = 0;
y++;
}
if(y<=from->h)
goto start_image;
}
fflush(file);
fclose(file);
return AIMG_LOADPNG_SUCCESS;
}