2
* Copyright 2011 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: Satyanarayana Manyam
19
#ifndef PAGESPEED_KERNEL_IMAGE_IMAGE_CONVERTER_H_
20
#define PAGESPEED_KERNEL_IMAGE_IMAGE_CONVERTER_H_
22
#include "pagespeed/kernel/base/basictypes.h"
23
#include "pagespeed/kernel/base/string.h"
24
#include "pagespeed/kernel/image/jpeg_optimizer.h"
25
#include "pagespeed/kernel/image/scanline_status.h"
26
#include "pagespeed/kernel/image/webp_optimizer.h"
28
namespace net_instaweb {
34
namespace image_compression {
36
using net_instaweb::MessageHandler;
38
class MultipleFrameReader;
39
class MultipleFrameWriter;
40
class PngReaderInterface;
41
class ScanlineReaderInterface;
42
class ScanlineWriterInterface;
44
class ImageConverter {
53
// Converts image one line at a time, between different image
54
// formats. Both 'reader' and 'writer' must be non-NULL.
55
static ScanlineStatus ConvertImageWithStatus(
56
ScanlineReaderInterface* reader,
57
ScanlineWriterInterface* writer);
59
inline static bool ConvertImage(ScanlineReaderInterface* reader,
60
ScanlineWriterInterface* writer) {
61
return ConvertImageWithStatus(reader, writer).Success();
64
// Converts image frame by frame, and then line by line within each
65
// frame, between different image formats. Both 'reader' and
66
// 'writer' must be non-NULL.
67
static ScanlineStatus ConvertMultipleFrameImage(MultipleFrameReader* reader,
68
MultipleFrameWriter* writer);
70
static bool ConvertPngToJpeg(
71
const PngReaderInterface& png_struct_reader,
72
const GoogleString& in,
73
const JpegCompressionOptions& options,
75
MessageHandler* handler);
77
// Reads the PNG encoded in 'in' with 'png_struct_reader', encodes
78
// it in WebP format using the options in 'config', and writes the
79
// resulting WebP in 'out'. Note that if config.alpha_quality==0,
80
// this function will fail when attempting to convert an image with
81
// transparent pixels. Returns is_opaque set to true iff the 'in'
83
static bool ConvertPngToWebp(
84
const PngReaderInterface& png_struct_reader,
85
const GoogleString& in,
86
const WebpConfiguration& config,
89
MessageHandler* handler);
91
// Reads the PNG encoded in 'in' with 'png_struct_reader', encodes
92
// it in WebP format using the options in 'config', and writes the
93
// resulting WebP in 'out'. Note that if config.alpha_quality==0,
94
// this function will fail when attempting to convert an image with
95
// transparent pixels. Returns is_opaque set to true iff the 'in'
96
// image was opaque. On entry, '*webp_writer' must be NULL; on exit,
97
// it contains the webp writer that was used to write the WebP, and
98
// the caller is responsible for deleting it. Most clients will
99
// prefer to use the other form ConvertPngToWebp.
100
static bool ConvertPngToWebp(
101
const PngReaderInterface& png_struct_reader,
102
const GoogleString& in,
103
const WebpConfiguration& config,
106
ScanlineWriterInterface** webp_writer,
107
MessageHandler* handler);
109
// Optimizes the given png image, also converts to jpeg and take the
110
// the one that has smaller size and set the output. Returns false
111
// if both of them fails.
112
static bool OptimizePngOrConvertToJpeg(
113
const PngReaderInterface& png_struct_reader,
114
const GoogleString& in,
115
const JpegCompressionOptions& options,
118
MessageHandler* handler);
120
// Populates 'out' with a version of the input image 'in' resulting
121
// in the smallest size, and returns the corresponding
122
// ImageType. The image formats that are candidates for the output
123
// image are: lossless WebP, optimized PNG, custom JPEG (if
124
// jpeg_options != NULL), and custom WebP (if webp_config !=
125
// NULL). To compensate for the loss in quality in the custom JPEG
126
// and WebP (which are presumably lossy), these two formats must be
127
// substantially smaller than the optimized PNG and the lossless
128
// WebP in order to be chosen. In the case where none of these image
129
// formats could be generated or the original image turns out to be
130
// the smallest, copies the original image to 'out' and returns
132
static ImageType GetSmallestOfPngJpegWebp(
133
// TODO(bmcquade): should be a ScanlineReaderInterface.
134
const PngReaderInterface& png_struct_reader,
135
const GoogleString& in,
136
const JpegCompressionOptions* jpeg_options,
137
const WebpConfiguration* webp_config,
139
MessageHandler* handler);
145
DISALLOW_COPY_AND_ASSIGN(ImageConverter);
148
} // namespace image_compression
150
} // namespace pagespeed
152
#endif // PAGESPEED_KERNEL_IMAGE_IMAGE_CONVERTER_H_