-
Notifications
You must be signed in to change notification settings - Fork 7
/
tumble_jpeg.c
200 lines (164 loc) · 4.77 KB
/
tumble_jpeg.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
/*
* tumble: build a PDF file from image files
*
* Copyright 2003, 2017 Eric Smith <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*
* 2009-03-02 [JDB] Add support for overlay images.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h> /* strcasecmp() is a BSDism */
#include <jpeglib.h>
#include "semantics.h"
#include "tumble.h"
#include "bitblt.h"
#include "pdf.h"
#include "tumble_input.h"
static FILE *jpeg_f;
static struct jpeg_decompress_struct cinfo;
static struct jpeg_error_mgr jerr;
static bool match_jpeg_suffix (char *suffix)
{
return ((strcasecmp (suffix, ".jpg") == 0) ||
(strcasecmp (suffix, ".jpeg") == 0));
}
static bool close_jpeg_input_file (void)
{
return true;
}
static bool open_jpeg_input_file (FILE *f, char *name)
{
uint8_t buf [2];
size_t l;
l = fread (& buf [0], 1, sizeof (buf), f);
if (l != sizeof (buf))
return false;
rewind (f);
if ((buf [0] != 0xff) || (buf [1] != 0xd8))
return false;
cinfo.err = jpeg_std_error (& jerr);
jpeg_create_decompress (& cinfo);
jpeg_stdio_src (& cinfo, f);
jpeg_read_header (& cinfo, TRUE);
rewind (f);
jpeg_f = f;
return true;
}
static bool last_jpeg_input_page (void)
{
return true;
}
static bool get_jpeg_image_info (int image,
input_attributes_t input_attributes,
image_info_t *image_info)
{
double unit;
#ifdef DEBUG_JPEG
printf ("color space: %d\n", cinfo.jpeg_color_space);
printf ("components: %d\n", cinfo.num_components);
printf ("density unit: %d\n", cinfo.density_unit);
printf ("x density: %d\n", cinfo.X_density);
printf ("y density: %d\n", cinfo.Y_density);
printf ("width: %d\n", cinfo.image_width);
printf ("height: %d\n", cinfo.image_height);
#endif
switch (cinfo.jpeg_color_space)
{
case JCS_GRAYSCALE:
if (cinfo.num_components != 1)
{
fprintf (stderr, "JPEG grayscale image has %d components, should have 1\n",
cinfo.num_components);
return false;
}
image_info->color = 0;
break;
case JCS_RGB:
case JCS_YCbCr:
if (cinfo.num_components != 3)
{
fprintf (stderr, "JPEG RGB or YCbCr image has %d components, should have 3\n",
cinfo.num_components);
return false;
}
image_info->color = 1;
break;
default:
fprintf (stderr, "JPEG color space %d not supported\n", cinfo.jpeg_color_space);
return false;
}
image_info->width_samples = cinfo.image_width;
image_info->height_samples = cinfo.image_height;
if (cinfo.saw_JFIF_marker & cinfo.density_unit)
{
switch (cinfo.density_unit)
{
case 1: /* samples per inch */
unit = 1.0;
break;
case 2: /* samples per cm */
unit = 2.54;
break;
default:
fprintf (stderr, "JFIF density unit %d not supported\n", cinfo.density_unit);
return false;
}
image_info->width_points = ((image_info->width_samples * POINTS_PER_INCH) /
(cinfo.X_density * unit));
image_info->height_points = ((image_info->height_samples * POINTS_PER_INCH) /
(cinfo.Y_density * unit));
}
else
{
/* assume 300 DPI - not great, but what else can we do? */
image_info->width_points = (image_info->width_samples * POINTS_PER_INCH) / 300.0;
image_info->height_points = (image_info->height_samples * POINTS_PER_INCH) / 300.0;
}
return true;
}
static bool process_jpeg_image (int image, /* range 1 .. n */
input_attributes_t input_attributes,
image_info_t *image_info,
pdf_page_handle page,
output_attributes_t output_attributes)
{
pdf_write_jpeg_image (page,
output_attributes.position.x, output_attributes.position.y,
image_info->width_points,
image_info->height_points,
image_info->color,
image_info->width_samples,
image_info->height_samples,
input_attributes.transparency,
jpeg_f);
return true;
}
input_handler_t jpeg_handler =
{
match_jpeg_suffix,
open_jpeg_input_file,
close_jpeg_input_file,
last_jpeg_input_page,
get_jpeg_image_info,
process_jpeg_image
};
void init_jpeg_handler (void)
{
install_input_handler (& jpeg_handler);
}