Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
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
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_
21 
22 #include <cstddef>
23 
24 #include "net/instaweb/rewriter/cached_result.pb.h"
29 #include "pagespeed/kernel/http/image_types.pb.h"
31 
32 namespace net_instaweb {
33 class Histogram;
34 class MessageHandler;
35 class Timer;
36 class Variable;
37 struct ContentType;
38 
39 class Image {
40  public:
52 
55  : timeout_count(NULL),
56  success_ms(NULL),
57  failure_ms(NULL) {}
58 
62  };
63 
65  enum VariableType {
66  FROM_UNKNOWN_FORMAT = 0,
67  FROM_GIF,
68  FROM_PNG,
69  FROM_JPEG,
70  OPAQUE,
71  NONOPAQUE,
72  FROM_GIF_ANIMATED,
73  NUM_VARIABLE_TYPE
74  };
75  ConversionBySourceVariable* Get(VariableType var_type) {
76  if ((var_type < FROM_UNKNOWN_FORMAT) ||
77  (var_type >= NUM_VARIABLE_TYPE)) {
78  return NULL;
79  }
80  return &(vars[var_type]);
81  }
82 
83  ConversionBySourceVariable vars[NUM_VARIABLE_TYPE];
84  };
85 
88  : preferred_webp(pagespeed::image_compression::WEBP_NONE),
89  allow_webp_alpha(false),
90  allow_webp_animated(false),
91  webp_quality(RewriteOptions::kDefaultImageRecompressQuality),
92  webp_animated_quality(RewriteOptions::kDefaultImageRecompressQuality),
93  jpeg_quality(RewriteOptions::kDefaultImageRecompressQuality),
94  progressive_jpeg_min_bytes(
95  RewriteOptions::kDefaultProgressiveJpegMinBytes),
96  progressive_jpeg(false),
97  convert_gif_to_png(false),
98  convert_png_to_jpeg(false),
99  convert_jpeg_to_webp(false),
100  recompress_jpeg(false),
101  recompress_png(false),
102  recompress_webp(false),
103  retain_color_profile(false),
104  retain_color_sampling(false),
105  retain_exif_data(false),
106  use_transparent_for_blank_image(false),
107  jpeg_num_progressive_scans(
108  RewriteOptions::kDefaultImageJpegNumProgressiveScans),
109  webp_conversion_timeout_ms(-1),
111  preserve_lossless(false),
112  webp_conversion_variables(NULL) {}
113 
116  pagespeed::image_compression::PreferredLibwebpLevel preferred_webp;
117  bool allow_webp_alpha;
118  bool allow_webp_animated;
119  int64 webp_quality;
120  int64 webp_animated_quality;
121  int64 jpeg_quality;
122  int64 progressive_jpeg_min_bytes;
123  bool progressive_jpeg;
124  bool convert_gif_to_png;
125  bool convert_png_to_jpeg;
126  bool convert_jpeg_to_webp;
127  bool recompress_jpeg;
128  bool recompress_png;
129  bool recompress_webp;
130  bool retain_color_profile;
131  bool retain_color_sampling;
132  bool retain_exif_data;
133  bool use_transparent_for_blank_image;
134  int64 jpeg_num_progressive_scans;
135  int64 webp_conversion_timeout_ms;
136 
140  bool preserve_lossless;
141 
142  ConversionVariables* webp_conversion_variables;
143  };
144 
145  virtual ~Image();
146 
148  static const ContentType* TypeToContentType(ImageType t);
149 
158  virtual void Dimensions(ImageDim* natural_dim) = 0;
159 
161  size_t input_size() const {
162  return original_contents_.size();
163  }
164 
166  size_t output_size() {
167  size_t ret;
168  if (output_valid_ || ComputeOutputContents()) {
169  ret = output_contents_.size();
170  } else {
171  ret = input_size();
172  }
173  return ret;
174  }
175 
176  ImageType image_type() {
177  if (image_type_ == IMAGE_UNKNOWN) {
179  }
180  return image_type_;
181  }
182 
186  virtual bool ResizeTo(const ImageDim& new_dim) = 0;
187 
191  virtual void SetTransformToLowRes() = 0;
192 
196  return TypeToContentType(image_type());
197  }
198 
201  StringPiece Contents();
202 
205  virtual bool DrawImage(Image* image, int x, int y) = 0;
206 
212  virtual bool EnsureLoaded(bool output_useful) = 0;
213 
215  virtual const GoogleString& url() = 0;
216 
218  virtual const GoogleString& debug_message() = 0;
219 
221  virtual const GoogleString& resize_debug_message() = 0;
222 
224  virtual void SetDebugMessageUrl(const GoogleString& url) = 0;
225 
226  protected:
227  explicit Image(const StringPiece& original_contents);
228  explicit Image(ImageType type);
229 
231  virtual void ComputeImageType() = 0;
232  virtual bool ComputeOutputContents() = 0;
233 
235  virtual void SetResizedDimensions(const ImageDim& dim) = 0;
236 
239  virtual bool ShouldConvertToProgressive(int64 quality) const = 0;
240 
241 
242  ImageType image_type_;
243  const StringPiece original_contents_;
247 
248  private:
249  friend class ImageTestingPeer;
250  friend class ImageTest;
251 
252 
253 };
254 
265 Image* NewImage(const StringPiece& original_contents,
266  const GoogleString& url,
267  const StringPiece& file_prefix,
268  Image::CompressionOptions* options,
269  Timer* timer,
270  MessageHandler* handler);
271 
274 Image* BlankImageWithOptions(int width, int height, ImageType type,
275  const StringPiece& tmp_dir,
276  Timer* timer,
277  MessageHandler* handler,
278  Image::CompressionOptions* options);
279 
280 }
281 
282 #endif
virtual void ComputeImageType()=0
Internal helpers.
Image * NewImage(const StringPiece &original_contents, const GoogleString &url, const StringPiece &file_prefix, Image::CompressionOptions *options, Timer *timer, MessageHandler *handler)
pagespeed::image_compression::PreferredLibwebpLevel preferred_webp
Definition: image.h:116
virtual void SetDebugMessageUrl(const GoogleString &url)=0
Sets the URL to be printed in debug messages.
Histogram * failure_ms
Failed (and non-timed-out) conversion duration.
Definition: image.h:61
GoogleString output_contents_
Lazily filled.
Definition: image.h:244
Definition: statistics.h:43
size_t output_size()
Returns the size of output image in bytes.
Definition: image.h:166
bool rewrite_attempted_
Indicates if we tried rewriting for this.
Definition: image.h:246
size_t input_size() const
Returns the size of original input in bytes.
Definition: image.h:161
virtual const GoogleString & debug_message()=0
Returns the debug message.
Variable * timeout_count
of timed-out conversions.
Definition: image.h:59
ImageType image_type_
Lazily initialized, initially IMAGE_UNKNOWN.
Definition: image.h:242
Histogram * success_ms
Successful conversion duration.
Definition: image.h:60
StringPiece Contents()
virtual void Dimensions(ImageDim *natural_dim)=0
const ContentType * content_type()
Definition: image.h:195
virtual const GoogleString & url()=0
Returns the image URL.
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual void SetResizedDimensions(const ImageDim &dim)=0
Inject desired resized dimensions directly for testing.
virtual bool EnsureLoaded(bool output_useful)=0
virtual bool ResizeTo(const ImageDim &new_dim)=0
Image * BlankImageWithOptions(int width, int height, ImageType type, const StringPiece &tmp_dir, Timer *timer, MessageHandler *handler, Image::CompressionOptions *options)
static const ContentType * TypeToContentType(ImageType t)
static method to convert image type to content type.
Definition: content_type.h:31
int conversions_attempted
Definition: image.h:139
Definition: statistics.h:138
virtual bool ShouldConvertToProgressive(int64 quality) const =0
Definition: image_testing_peer.h:29
Definition: message_handler.h:39
bool output_valid_
Indicates output_contents_ now correct.
Definition: image.h:245
Definition: image.h:39
virtual const GoogleString & resize_debug_message()=0
Returns the resized image debug message.
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
virtual bool DrawImage(Image *image, int x, int y)=0
virtual void SetTransformToLowRes()=0