-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwsq2png.c
123 lines (97 loc) · 2.25 KB
/
wsq2png.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <png.h>
#include "wsq.h"
int debug;
static int write_grey_8bit_png(FILE *fl, void *image, int width, int height)
{
unsigned char *pixels = image;
png_structp png;
png_infop info;
int row;
png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png)
return -__LINE__;
info = png_create_info_struct(png);
if (!info)
return -__LINE__;
if (setjmp(png_jmpbuf(png))) {
png_destroy_write_struct(&png, &info);
return -__LINE__;
}
png_init_io(png, fl);
png_set_IHDR(png, info, width, height,
8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png, info);
for (row = 0; row < height; row++)
png_write_row(png, pixels + (width * row));
png_write_end(png, NULL);
png_destroy_write_struct(&png, &info);
return 0;
}
static void usage(const char *comm)
{
fprintf(stderr, "Usage: %s [-h] <in.wsq> <out.png>\n", comm);
fprintf(stderr, "where:\n");
fprintf(stderr, "\t-h\tusage syntax (this message)\n");
fprintf(stderr, "\t\tuse - instead of file name to use stdin/stdout\n");
}
int main(int argc, char *argv[])
{
int opt;
FILE *in = NULL;
FILE *out = NULL;
int err;
int width, height, depth, ppi, lossy;
unsigned char *image;
while ((opt = getopt(argc, argv, "h")) != -1) {
switch (opt) {
default:
usage(argv[0]);
return 1;
}
}
if (argc - optind != 2) {
usage(argv[0]);
return 1;
}
if (strcmp(argv[optind], "-") == 0)
in = stdin;
else
in = fopen(argv[optind], "rb");
if (!in) {
perror(argv[optind]);
return 1;
}
if (strcmp(argv[optind + 1], "-") == 0)
out = stdout;
else
out = fopen(argv[optind + 1], "wb");
if (!out) {
perror(argv[optind + 1]);
return 1;
}
err = wsq_decode_file(&image, &width, &height, &depth, &ppi,
&lossy, in);
if (err) {
fprintf(stderr, "Failed to compress image! (%d)\n", err);
return 1;
}
if (depth != 8)
fprintf(stderr, "Warning: expected 8-bit image, got %d...\n",
depth);
err = write_grey_8bit_png(out, image, width, height);
if (err) {
fprintf(stderr, "Failed to read PNG image! (%d)\n", err);
return 1;
}
if (in != stdin)
fclose(in);
if (out != stdout)
fclose(out);
free(image);
return 0;
}