2
* Copyright 2009 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: Bryan McQuade, Matthew Steele, Satyanarayana Manyam
19
#ifndef PAGESPEED_KERNEL_IMAGE_JPEG_OPTIMIZER_H_
20
#define PAGESPEED_KERNEL_IMAGE_JPEG_OPTIMIZER_H_
24
#include "pagespeed/kernel/base/basictypes.h"
25
#include "pagespeed/kernel/base/string.h"
26
#include "pagespeed/kernel/image/image_util.h"
27
#include "pagespeed/kernel/image/scanline_interface.h"
28
#include "pagespeed/kernel/image/scanline_status.h"
30
// DO NOT INCLUDE LIBJPEG HEADERS HERE. Doing so causes build errors
33
namespace net_instaweb {
39
namespace image_compression {
41
using net_instaweb::MessageHandler;
50
struct JpegLossyOptions {
51
JpegLossyOptions() : quality(85), num_scans(-1), color_sampling(YUV420) {}
52
// jpeg_quality - Can take values in the range [1,100].
53
// For web images, the preferred value for quality is 85.
54
// For smaller images like thumbnails, the preferred value for quality is 75.
55
// Setting it to values below 50 is generally not preferable.
58
// No. of progressive scan that needs to be included in the final output.
59
// -1 indicates to use all scans that are present.
62
// Color sampling that needs to be used while recompressing the image.
63
ColorSampling color_sampling;
66
struct JpegCompressionOptions {
67
JpegCompressionOptions()
68
: progressive(false), retain_color_profile(false),
69
retain_exif_data(false), lossy(false) {}
71
// Whether or not to produce a progressive JPEG. This parameter will only be
72
// applied for images with YCbCr colorspace, and it is ignored for other
76
// If set to 'true' any color profile information is retained.
77
bool retain_color_profile;
79
// If set to 'true' any exif information is retained.
80
bool retain_exif_data;
82
// Whether or not to use lossy compression.
85
// Lossy compression options. Only applicable if lossy (above) is set to true.
86
JpegLossyOptions lossy_options;
89
// Performs lossless optimization, that is, the output image will be
90
// pixel-for-pixel identical to the input image.
91
bool OptimizeJpeg(const GoogleString &original,
92
GoogleString *compressed,
93
MessageHandler* handler);
95
// Performs JPEG optimizations with the provided options.
96
bool OptimizeJpegWithOptions(const GoogleString &original,
97
GoogleString *compressed,
98
const JpegCompressionOptions &options,
99
MessageHandler* handler);
101
// User of this class must call this functions in the following sequence
103
// JpegScanlineWriter jpeg_writer;
105
// if (setjmp(env)) {
106
// jpeg_writer.AbortWrite();
109
// jpeg_writer.SetJmpBufEnv(&env);
110
// if (jpeg_writer.Init(width, height, format)) {
111
// jpeg_writer.SetJpegCompressParams(quality);
112
// jpeg_writer.InitializeWrite(out);
113
// while(has_lines_to_write) {
114
// writer.WriteNextScanline(next_scan_line);
116
// writer.FinalizeWrite()
119
class JpegScanlineWriter : public ScanlineWriterInterface {
121
explicit JpegScanlineWriter(MessageHandler* handler);
122
virtual ~JpegScanlineWriter();
124
// Set the environment for longjmp calls.
125
void SetJmpBufEnv(jmp_buf* env);
127
// This function is only called when jpeg library call longjmp for
128
// cleaning up the jpeg structs.
131
virtual ScanlineStatus InitWithStatus(const size_t width, const size_t height,
132
PixelFormat pixel_format);
133
// Set the compression options via 'params', which should be a
134
// JpegCompressionOptions*. Since writer only supports lossy
135
// encoding, it is an error to pass in a 'params' that has the lossy
136
// field set to false.
137
virtual ScanlineStatus InitializeWriteWithStatus(const void* params,
138
GoogleString *compressed);
139
virtual ScanlineStatus WriteNextScanlineWithStatus(
140
const void *scanline_bytes);
141
virtual ScanlineStatus FinalizeWriteWithStatus();
144
// Since writer only supports lossy encoding, it is an error to pass
145
// in a compression options that has lossy field set to false.
146
void SetJpegCompressParams(const JpegCompressionOptions& options);
148
// Opaque struct that is defined in the cc file and contains our
149
// JPEG-compressor-specific structures.
152
MessageHandler* message_handler_;
154
DISALLOW_COPY_AND_ASSIGN(JpegScanlineWriter);
157
} // namespace image_compression
159
} // namespace pagespeed
161
#endif // PAGESPEED_KERNEL_IMAGE_JPEG_OPTIMIZER_H_