forked from nohal/libbsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsbmem2png.c
161 lines (139 loc) · 3.91 KB
/
bsbmem2png.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
/*
* bsbmem2png.c - Convert a bsb raster image loaded into a memory buffer to a Portable Network Graphics (png).
* See http://www.libpng.org for details of the PNG format and how
* to use libpng.
*
* Copyright (C) 2004 Stefan Petersen <spetm@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: bsb2png.c,v 1.7 2007/02/05 17:08:18 mikrom Exp $
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <bsb.h>
#include <png.h>
static void copy_bsb_to_png(BSBImage *image, png_structp png_ptr)
{
int row, bp, pp;
uint8_t *bsb_row, *png_row;
bsb_row = (uint8_t *)malloc(image->width * sizeof(uint8_t));
png_row = (uint8_t *)malloc(image->width * sizeof(uint8_t) * image->depth);
/* Copy row by row */
for (row = 0; row < image->height; row++)
{
bsb_seek_to_row(image, row);
bsb_read_row(image, bsb_row);
for (bp = 0, pp = 0; bp < image->width; bp++)
{
uint8_t i = bsb_row[bp];
png_row[pp++] = image->red[i];
png_row[pp++] = image->green[i];
png_row[pp++] = image->blue[i];
}
png_write_row(png_ptr, png_row);
}
free(bsb_row);
free(png_row);
} /* copy_bsb_to_png */
int main(int argc, char *argv[])
{
BSBImage image;
FILE *bsb_fd;
FILE *png_fd;
png_structp png_ptr;
png_infop info_ptr;
png_text text[2];
if (argc != 3) {
fprintf(stderr, "Usage:\n\tbsb2png input.kap output.png\n");
exit(1);
}
/* read from file argv[1] into memory buffer */
struct stat statbuf;
int s = stat(argv[1], &statbuf);
if(s != 0)
{
perror("stat");
exit(1);
}
off_t size = statbuf.st_size;
char *bsb_buf = (char*) malloc(size);
if(!bsb_buf)
{
perror("malloc");
exit(1);
}
bsb_fd = fopen(argv[1], "r");
if(!bsb_fd)
{
perror("fopen bsb");
exit(1);
}
char *b = bsb_buf;
size_t n = 0;
while( (n = fread(b, 1, 256, bsb_fd)) > 0)
b += n;
fclose(bsb_fd);
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "png_ptr == NULL\n");
exit(1);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "info_ptr == NULL\n");
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
exit(1);
}
if ((png_fd = fopen(argv[2], "wb")) == NULL) {
perror("fopen");
exit(1);
}
png_init_io(png_ptr, png_fd);
/* assert(size >= 0); */
bsb_fd = fmemopen(bsb_buf, (size_t)size, "r");
if(!bsb_fd)
{
perror("fmemopen");
exit(1);
}
if (! bsb_open_fp(bsb_fd, &image)) {
exit(1);
}
png_set_IHDR(png_ptr, info_ptr, image.width, image.height, 8,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
/* Some text to go with the png image */
text[0].key = "Title";
text[0].text = argv[2];
text[0].compression = PNG_TEXT_COMPRESSION_NONE;
text[1].key = "Generator";
text[1].text = "bsb2png";
text[1].compression = PNG_TEXT_COMPRESSION_NONE;
png_set_text(png_ptr, info_ptr, text, 2);
/* Write header data */
png_write_info(png_ptr, info_ptr);
/* Copy the image in itself */
copy_bsb_to_png(&image, png_ptr);
png_write_end(png_ptr, NULL);
fclose(png_fd);
png_destroy_write_struct(&png_ptr, &info_ptr);
bsb_close(&image);
return 0;
}