Skip to content

Commit 452ae23

Browse files
keever50xiaoxiang781216
authored andcommitted
graphics/jpgresizetool: Add libjpeg resize tool
Add a libjpeg based JPEG resize tool. Simple resizer that can resize JPEGs using "jpgresize input.jpg output.jpg scale_denom(1,2,4,8) quality%". Tries to use little memory by scanning per line. Signed-off-by: Kevin Witteveen (MartiniMarter) <[email protected]>
1 parent 4ffed5d commit 452ae23

File tree

5 files changed

+303
-0
lines changed

5 files changed

+303
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# ##############################################################################
2+
# apps/graphics/jpgresizetool/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
if(CONFIG_GRAPHICS_JPGRESIZETOOL)
24+
nuttx_add_application(
25+
NAME
26+
jpgresize
27+
STACKSIZE
28+
4096
29+
MODULE
30+
${CONFIG_GRAPHICS_JPGRESIZETOOL}
31+
SRCS
32+
resize.c)
33+
endif()

graphics/jpgresizetool/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config GRAPHICS_JPGRESIZETOOL
7+
bool "JPGResizeTool"
8+
default n
9+
depends on libjpeg
10+
---help---
11+
Libjpeg JPEG resizer

graphics/jpgresizetool/Make.defs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/graphics/jpgresizetool/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
ifneq ($(CONFIG_GRAPHICS_JPGRESIZETOOL),)
24+
CONFIGURED_APPS += $(APPDIR)/graphics/jpgresizetool
25+
endif

graphics/jpgresizetool/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
############################################################################
2+
# apps/graphics/jpgresizetool/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(APPDIR)/Make.defs
24+
25+
MAINSRC = resize.c
26+
27+
# built-in application info
28+
29+
PROGNAME = jpgresize
30+
PRIORITY = SCHED_PRIORITY_DEFAULT
31+
STACKSIZE = 4096
32+
MODULE = $(CONFIG_GRAPHICS_JPGRESIZETOOL)
33+
34+
include $(APPDIR)/Application.mk

graphics/jpgresizetool/resize.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/****************************************************************************
2+
* apps/graphics/jpgresizetool/resize.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <jpeglib.h>
30+
31+
/****************************************************************************
32+
* Definitions
33+
****************************************************************************/
34+
35+
/* Every N line, report resizing progress */
36+
#define JPEGRESIZE_PROGRESS_INTERVAL 40
37+
38+
/****************************************************************************
39+
* Public Functions
40+
****************************************************************************/
41+
42+
/* Usage: jpgresize input.jpg output.jpg scale_denom quality%
43+
* scale_denom: 1, 2, 4, 8. Shrinks image by scale_denom times.
44+
* quality: 1% to 100%. 20% heavy compression, 80% nearly original.
45+
* thumbnail example:
46+
* jpgresize /sd/IMAGES/000000a1.jpg /sd/TEMP/THUMBS/000000a1.jpg 8 20
47+
*/
48+
49+
int main(int argc, char *argv[])
50+
{
51+
struct jpeg_compress_struct cinfo_out;
52+
struct jpeg_decompress_struct cinfo;
53+
struct jpeg_error_mgr jerr_out;
54+
struct jpeg_error_mgr jerr;
55+
const char *output_filename;
56+
const char *input_filename;
57+
uint8_t togglechar;
58+
JSAMPARRAY buffer;
59+
FILE *outfile;
60+
FILE *infile;
61+
int scale_denom;
62+
int pixel_size;
63+
int quality;
64+
int height;
65+
int width;
66+
67+
/* Take arguments */
68+
69+
if (argc != 5)
70+
{
71+
fprintf(stderr,
72+
"Usage: %s input.jpg output.jpg scale_denom(1, 2, 4, 8) quality%%\n",
73+
argv[0]);
74+
return -1;
75+
}
76+
77+
scale_denom = atoi(argv[3]);
78+
if (scale_denom != 1 && scale_denom != 2 && scale_denom != 4 &&
79+
scale_denom != 8 && scale_denom != 16)
80+
{
81+
fprintf(stderr, "scale_denom must be 1, 2, 4 or 8\n");
82+
return -1;
83+
}
84+
85+
input_filename = argv[1];
86+
output_filename = argv[2];
87+
quality = atoi(argv[4]);
88+
89+
/* Opening input */
90+
91+
infile = fopen(input_filename, "rb");
92+
if (!infile)
93+
{
94+
perror("fopen input");
95+
return -1;
96+
}
97+
98+
/* Configuring decompressor */
99+
100+
cinfo.err = jpeg_std_error(&jerr);
101+
jpeg_create_decompress(&cinfo);
102+
jpeg_stdio_src(&cinfo, infile);
103+
jpeg_read_header(&cinfo, TRUE);
104+
105+
cinfo.scale_num = 1;
106+
cinfo.scale_denom = scale_denom;
107+
jpeg_start_decompress(&cinfo);
108+
109+
width = cinfo.output_width;
110+
height = cinfo.output_height;
111+
pixel_size = cinfo.output_components;
112+
113+
/* Opening output */
114+
115+
outfile = fopen(output_filename, "wb");
116+
if (!outfile)
117+
{
118+
perror("fopen output");
119+
jpeg_destroy_decompress(&cinfo);
120+
fclose(infile);
121+
return -1;
122+
}
123+
124+
/* Configuring compressor */
125+
126+
cinfo_out.err = jpeg_std_error(&jerr_out);
127+
jpeg_create_compress(&cinfo_out);
128+
jpeg_stdio_dest(&cinfo_out, outfile);
129+
130+
cinfo_out.image_width = width;
131+
cinfo_out.image_height = height;
132+
cinfo_out.input_components = pixel_size;
133+
cinfo_out.in_color_space = cinfo.out_color_space;
134+
jpeg_set_defaults(&cinfo_out);
135+
jpeg_set_quality(&cinfo_out, quality, TRUE);
136+
jpeg_start_compress(&cinfo_out, TRUE);
137+
138+
/* Allocating memory by using JPEG their own allocator. Do not use malloc */
139+
140+
printf("Allocating... \r");
141+
buffer = (*cinfo.mem->alloc_sarray)
142+
((j_common_ptr)&cinfo, JPOOL_IMAGE, width * pixel_size, 1);
143+
144+
/* Start the job */
145+
146+
printf("Resizing... \n");
147+
togglechar = 0;
148+
while (cinfo.output_scanline < cinfo.output_height)
149+
{
150+
/* Pretty loading bar... */
151+
152+
if (cinfo.output_scanline % JPEGRESIZE_PROGRESS_INTERVAL == 1)
153+
{
154+
printf("\r[%u%%]",
155+
(unsigned)(((float)cinfo.output_scanline
156+
/ cinfo.output_height)
157+
* 100));
158+
togglechar = !togglechar;
159+
}
160+
161+
if (togglechar)
162+
{
163+
printf("-");
164+
}
165+
else
166+
{
167+
printf("#");
168+
}
169+
170+
fflush(stdout);
171+
172+
/* Actual work */
173+
174+
jpeg_read_scanlines(&cinfo, buffer, 1);
175+
jpeg_write_scanlines(&cinfo_out, buffer, 1);
176+
}
177+
178+
/* Erase progress bar */
179+
180+
printf("\r ");
181+
for (size_t i = 0; i < JPEGRESIZE_PROGRESS_INTERVAL; i++)
182+
{
183+
printf(" ");
184+
}
185+
186+
/* All done */
187+
188+
printf("\rDone\n");
189+
190+
jpeg_finish_compress(&cinfo_out);
191+
jpeg_destroy_compress(&cinfo_out);
192+
fclose(outfile);
193+
194+
jpeg_finish_decompress(&cinfo);
195+
jpeg_destroy_decompress(&cinfo);
196+
fclose(infile);
197+
198+
printf("Resized JPEG written to %s\n", output_filename);
199+
return 0;
200+
}

0 commit comments

Comments
 (0)