-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
executable file
·307 lines (268 loc) · 7.21 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* PNG2RLE , standalone cross-platform png to rle converter.
* Copyright (C) 2014 Alireza Forouzandeh Nezhad <[email protected]> <http://alirezafn.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include "png.h"
#define to565(r,g,b) \
((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
#define from565_r(x) ((((x) >> 11) & 0x1f) * 255 / 31)
#define from565_g(x) ((((x) >> 5) & 0x3f) * 255 / 63)
#define from565_b(x) (((x) & 0x1f) * 255 / 31)
typedef FILE file;
static long get_file_size(file *f) {
long current_offset=ftell(f);
fseek(f,0,SEEK_END);
long size=ftell(f);
fseek(f,current_offset,SEEK_SET);
return size;
}
static char *load_file_to_mem(char *path) {
file *f=fopen(path,"r");
long size=get_file_size(f);
char *mem=malloc((int)size);
fread(mem,1,size,f);
fclose(f);
return mem;
}
static void write_mem_to_file(char *path,char *mem,unsigned int size) {
file *f=fopen(path,"w");
fwrite(mem,1,size,f);
fclose(f);
return;
}
// log functions for later usage
static void log_info(char *m,...) {
va_list arg;
va_start(arg,m);
int s_size=strlen(m)+8;
char *s=malloc(s_size);
strcpy(s,"[INFO]\t");
strcat(s,m);
vfprintf(stdout,s,arg);
va_end(arg);
free(s);
}
static void log_warn(char *m,...) {
va_list arg;
va_start(arg,m);
int s_size=strlen(m)+8;
char *s=malloc(s_size);
strcpy(s,"[WARNING]\t");
strcat(s,m);
vfprintf(stdout,s,arg);
va_end(arg);
free(s);
}
static void log_err(char *m,...) {
va_list arg;
va_start(arg,m);
int s_size=strlen(m)+8;
char *s=malloc(s_size);
strcpy(s,"[ERROR]\t");
strcat(s,m);
vfprintf(stdout,s,arg);
va_end(arg);
free(s);
}
typedef struct {
void (*warn)(char *,...);
void (*err)(char *,...);
void (*info)(char *,...);
} log_ops;
static log_ops log = {
.info=log_info,
.err=log_err,
.info=log_info,
};
typedef struct {
char *start; // where memblk memory start
int size; // sizeof memblk memory
int offset; // current position
} memblk;
static memblk *memopen(void *mem,int size) {
memblk *m=malloc(sizeof(memblk));
m->start=mem;
m->size=size;
m->offset=0;
return m;
}
// simulate a POSIX read function but in memory instead of file
static int memread(memblk *f,void *buffer,int size) {
int read_len=size;
if(f->offset >= f->size) {
return 0;
}
if((size+f->offset) > f->size) {
read_len=f->size-(f->offset+size);
}
memcpy(buffer,f->start+f->offset,read_len);
f->offset+=read_len;
return read_len;
}
// simulate a POSIX close function but in memory instead of file
static void memclose(memblk *f) {
free(f);
}
// simulate a POSIX write function but in memory instead of file
// dynamicly expand memblk memory like when file size expands while writing
static int memwrite(memblk *f,void *buffer,int size) {
f->start=realloc(f->start,size+f->size);
if(!f->start) {
return -1;
}
f->size+=size;
memcpy(f->start+f->offset,buffer,size);
f->offset+=size;
return size;
}
// simulate a POSIX lseek function but in memory instead of file
static int memseek(memblk *f,unsigned int offset,int whence) {
if(whence == 0) {
f->offset=offset;
} else if(whence == 1) {
f->offset+=offset;
} else if(whence == 2) {
f->offset=f->size+offset;
// if offset is > 0 then we are trying to go out of range of
// f->start so set f->size to current offset to tell memwrite
// to expand the memory allocated
if(offset > 0) {
f->size=f->offset;
}
} else {
return -1;
}
}
// image pixel formats
typedef enum {
IMG_RGB888=0,
IMG_RGBA8888=1,
IMG_RGB565=2,
IMG_RLE565=3,
IMG_UNKOWN=4,
} img_format;
// a struct to hold image info
typedef struct {
unsigned char *mem; // ptr to where the image has been loaded
unsigned int height;
unsigned int width;
img_format format;
unsigned int error; // if there where an error keep it here
unsigned size;
} image;
static image *new_image(unsigned int width,unsigned int height,img_format format) {
image *i=(image *)malloc(sizeof(image));
i->height=height;
i->width=width;
i->format=format;
i->error=0;
if(format == IMG_RGBA8888) {
i->mem=malloc(width*height*4);
} else if(format == IMG_RGB888) {
i->mem=malloc(width*height*3);
} else if(format == IMG_RGB565) {
i->mem=malloc(width*height*2);
} else {
i->mem=0;
}
return i;
}
// read a png from file and write it in memory allocated image
static image *read_png_file_into_image(const char *path) {
image *i=(image *)malloc(sizeof(image));
i->error=lodepng_decode32_file(&i->mem, &i->width, &i->height, path);
i->format=IMG_RGBA8888;
return i;
}
// convert an image to rgb image
static image *convert_to_rgb(image *in) {
if(in->format!=IMG_RGBA8888) {
return 0;
}
int j;
image *o=new_image(in->width,in->height,IMG_RGB888);
for(j=0;j<in->width*in->height;j++) {
o->mem[j*3+0]=in->mem[j*4+0];
o->mem[j*3+1]=in->mem[j*4+1];
o->mem[j*3+2]=in->mem[j*4+2];
}
return o;
}
static memblk *memopen_image(image *i) {
int size=0;
if(i->format == IMG_RGBA8888) {
size=i->height*i->width*4;
} else if(i->format == IMG_RGB888) {
size=i->height*i->width*3;
} else if(i->format == IMG_RGB565) {
size=i->height*i->width*2;
} else {
return 0;
}
return memopen(i->mem,size);
}
static image *convert_to_rle(image *ini) {
// currently only rgb888 is supported !
if(ini->format!=IMG_RGB888) {
return 0;
}
memblk *m=memopen_image(ini);
memblk *outfd=memopen(malloc(0),0);
unsigned char in[3];
unsigned short last, color, count;
unsigned total = 0;
count = 0;
while(memread(m, in, 3) == 3) {
color = to565(in[0],in[1],in[2]);
if (count) {
if ((color == last) && (count != 65535)) {
count++;
continue;
} else {
memwrite(outfd, &count, 2);
memwrite(outfd, &last, 2);
total += count;
}
}
last = color;
count = 1;
}
if (count) {
memwrite(outfd, &count, 2);
memwrite(outfd, &last, 2);
total += count;
}
image *i=malloc(sizeof(image));
i->format=IMG_RLE565;
i->mem=outfd->start;
i->size=outfd->size;
return i;
}
static int write_image_to_file(char *path,image *i) {
write_mem_to_file(path,i->mem,i->size);
}
int main(int argc,char **argv) {
if(argc<3 || !strcmp(argv[1],"--help")) { printf("PNG to RLE converter\nusage: %s input_png output_rle\ncopyright 2014 Alireza7991 <[email protected]> <http://alirezafn.net>\n",argv[0]);
exit(0); }
write_image_to_file(argv[2],convert_to_rle(convert_to_rgb(read_png_file_into_image(argv[1]))));
exit(0);
}