Page Speed Optimization Libraries  1.9.32.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/image_types.pb.h"
25 #include "net/instaweb/rewriter/cached_result.pb.h"
30 
31 namespace net_instaweb {
32 class Histogram;
33 class MessageHandler;
34 class Timer;
35 class Variable;
36 struct ContentType;
37 
38 class Image {
39  public:
51 
53  WEBP_NONE = 0,
54  WEBP_LOSSY,
55  WEBP_LOSSLESS
56  };
57 
60  : timeout_count(NULL),
61  success_ms(NULL),
62  failure_ms(NULL) {}
63 
64  Variable* timeout_count;
65  Histogram* success_ms;
66  Histogram* failure_ms;
67  };
68 
70  enum VariableType {
71  FROM_UNKNOWN_FORMAT = 0,
72  FROM_GIF,
73  FROM_PNG,
74  FROM_JPEG,
75  OPAQUE,
76  NONOPAQUE,
77  NUM_VARIABLE_TYPE
78  };
79  ConversionBySourceVariable* Get(VariableType var_type) {
80  if ((var_type < FROM_UNKNOWN_FORMAT) ||
81  (var_type >= NUM_VARIABLE_TYPE)) {
82  return NULL;
83  }
84  return &(vars[var_type]);
85  }
86 
87  ConversionBySourceVariable vars[NUM_VARIABLE_TYPE];
88  };
89 
92  : preferred_webp(WEBP_NONE),
93  allow_webp_alpha(false),
94  webp_quality(RewriteOptions::kDefaultImageRecompressQuality),
95  jpeg_quality(RewriteOptions::kDefaultImageRecompressQuality),
96  progressive_jpeg_min_bytes(
97  RewriteOptions::kDefaultProgressiveJpegMinBytes),
98  progressive_jpeg(false),
99  convert_gif_to_png(false),
100  convert_png_to_jpeg(false),
101  convert_jpeg_to_webp(false),
102  recompress_jpeg(false),
103  recompress_png(false),
104  recompress_webp(false),
105  retain_color_profile(false),
106  retain_color_sampling(false),
107  retain_exif_data(false),
108  use_transparent_for_blank_image(false),
109  jpeg_num_progressive_scans(
110  RewriteOptions::kDefaultImageJpegNumProgressiveScans),
111  webp_conversion_timeout_ms(-1),
113  preserve_lossless(false),
114  webp_conversion_variables(NULL) {}
115 
119  bool allow_webp_alpha;
120  int64 webp_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 
185  ResourceContext::LibWebpLevel MinimalWebpSupport() {
186  if (!rewrite_attempted_) {
187  ComputeOutputContents();
188  }
189  return minimal_webp_support_;
190  }
191 
195  virtual bool ResizeTo(const ImageDim& new_dim) = 0;
196 
200  virtual void SetTransformToLowRes() = 0;
201 
204  const ContentType* content_type() {
205  return TypeToContentType(image_type());
206  }
207 
210  StringPiece Contents();
211 
214  virtual bool DrawImage(Image* image, int x, int y) = 0;
215 
221  virtual bool EnsureLoaded(bool output_useful) = 0;
222 
224  virtual const GoogleString& url() = 0;
225 
227  virtual const GoogleString& debug_message() = 0;
228 
230  virtual const GoogleString& resize_debug_message() = 0;
231 
232  protected:
233  explicit Image(const StringPiece& original_contents);
234  explicit Image(ImageType type);
235 
237  virtual void ComputeImageType() = 0;
238  virtual bool ComputeOutputContents() = 0;
239 
241  virtual void SetResizedDimensions(const ImageDim& dim) = 0;
242 
245  virtual bool ShouldConvertToProgressive(int64 quality) const = 0;
246 
247 
248  ImageType image_type_;
249  const StringPiece original_contents_;
250  GoogleString output_contents_;
253  ResourceContext::LibWebpLevel minimal_webp_support_;
254 
255  private:
256  friend class ImageTestingPeer;
257  friend class ImageTest;
258 
259  DISALLOW_COPY_AND_ASSIGN(Image);
260 };
261 
272 Image* NewImage(const StringPiece& original_contents,
273  const GoogleString& url,
274  const StringPiece& file_prefix,
275  Image::CompressionOptions* options,
276  Timer* timer,
277  MessageHandler* handler);
278 
281 Image* BlankImageWithOptions(int width, int height, ImageType type,
282  const StringPiece& tmp_dir,
283  Timer* timer,
284  MessageHandler* handler,
285  Image::CompressionOptions* options);
286 
287 }
288 
289 #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)
Histogram * failure_ms
Failed (and non-timed-out) conversion duration.
Definition: image.h:66
GoogleString output_contents_
Lazily filled.
Definition: image.h:250
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:252
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:64
ImageType image_type_
Lazily initialized, initially IMAGE_UNKNOWN.
Definition: image.h:248
Histogram * success_ms
Successful conversion duration.
Definition: image.h:65
StringPiece Contents()
virtual void Dimensions(ImageDim *natural_dim)=0
const ContentType * content_type()
Definition: image.h:204
virtual const GoogleString & url()=0
Returns the image URL.
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.
int conversions_attempted
Definition: image.h:139
virtual bool ShouldConvertToProgressive(int64 quality) const =0
PreferredWebp
Definition: image.h:52
Definition: image_testing_peer.h:29
ResourceContext::LibWebpLevel MinimalWebpSupport()
Definition: image.h:185
bool output_valid_
Indicates output_contents_ now correct.
Definition: image.h:251
Definition: image.h:38
PreferredWebp preferred_webp
Definition: image.h:118
virtual const GoogleString & resize_debug_message()=0
Returns the resized image debug message.
virtual bool DrawImage(Image *image, int x, int y)=0
virtual void SetTransformToLowRes()=0