Page Speed Optimization Libraries
1.7.30.4
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_ 00021 00022 #include <cstddef> 00023 00024 #include "net/instaweb/rewriter/image_types.pb.h" 00025 #include "net/instaweb/rewriter/cached_result.pb.h" 00026 #include "net/instaweb/rewriter/public/rewrite_options.h" 00027 #include "net/instaweb/util/public/basictypes.h" 00028 #include "net/instaweb/util/public/string.h" 00029 #include "net/instaweb/util/public/string_util.h" 00030 00031 namespace net_instaweb { 00032 class Histogram; 00033 class MessageHandler; 00034 class Timer; 00035 class Variable; 00036 struct ContentType; 00037 00038 class Image { 00039 public: 00051 00052 enum PreferredWebp { 00053 WEBP_NONE = 0, 00054 WEBP_LOSSY, 00055 WEBP_LOSSLESS 00056 }; 00057 00058 struct ConversionBySourceVariable { 00059 ConversionBySourceVariable() 00060 : timeout_count(NULL), 00061 success_ms(NULL), 00062 failure_ms(NULL) {} 00063 00064 Variable* timeout_count; 00065 Histogram* success_ms; 00066 Histogram* failure_ms; 00067 }; 00068 00069 struct ConversionVariables { 00070 enum VariableType { 00071 FROM_UNKNOWN_FORMAT = 0, 00072 FROM_GIF, 00073 FROM_PNG, 00074 FROM_JPEG, 00075 OPAQUE, 00076 NONOPAQUE, 00077 NUM_VARIABLE_TYPE 00078 }; 00079 ConversionBySourceVariable* Get(VariableType var_type) { 00080 if ((var_type < FROM_UNKNOWN_FORMAT) || 00081 (var_type >= NUM_VARIABLE_TYPE)) { 00082 return NULL; 00083 } 00084 return &(vars[var_type]); 00085 } 00086 00087 ConversionBySourceVariable vars[NUM_VARIABLE_TYPE]; 00088 }; 00089 00090 struct CompressionOptions { 00091 CompressionOptions() 00092 : preferred_webp(WEBP_NONE), 00093 allow_webp_alpha(false), 00094 webp_quality(RewriteOptions::kDefaultImageRecompressQuality), 00095 jpeg_quality(RewriteOptions::kDefaultImageRecompressQuality), 00096 progressive_jpeg_min_bytes( 00097 RewriteOptions::kDefaultProgressiveJpegMinBytes), 00098 progressive_jpeg(false), 00099 convert_gif_to_png(false), 00100 convert_png_to_jpeg(false), 00101 convert_jpeg_to_webp(false), 00102 recompress_jpeg(false), 00103 recompress_png(false), 00104 recompress_webp(false), 00105 retain_color_profile(false), 00106 retain_color_sampling(false), 00107 retain_exif_data(false), 00108 use_transparent_for_blank_image(false), 00109 jpeg_num_progressive_scans( 00110 RewriteOptions::kDefaultImageJpegNumProgressiveScans), 00111 webp_conversion_timeout_ms(-1), 00112 conversions_attempted(0), 00113 preserve_lossless(false), 00114 webp_conversion_variables(NULL) {} 00115 00118 PreferredWebp preferred_webp; 00119 bool allow_webp_alpha; 00120 int64 webp_quality; 00121 int64 jpeg_quality; 00122 int64 progressive_jpeg_min_bytes; 00123 bool progressive_jpeg; 00124 bool convert_gif_to_png; 00125 bool convert_png_to_jpeg; 00126 bool convert_jpeg_to_webp; 00127 bool recompress_jpeg; 00128 bool recompress_png; 00129 bool recompress_webp; 00130 bool retain_color_profile; 00131 bool retain_color_sampling; 00132 bool retain_exif_data; 00133 bool use_transparent_for_blank_image; 00134 int64 jpeg_num_progressive_scans; 00135 int64 webp_conversion_timeout_ms; 00136 00139 int conversions_attempted; 00140 bool preserve_lossless; 00141 00142 ConversionVariables* webp_conversion_variables; 00143 }; 00144 00145 virtual ~Image(); 00146 00148 static const ContentType* TypeToContentType(ImageType t); 00149 00158 virtual void Dimensions(ImageDim* natural_dim) = 0; 00159 00161 size_t input_size() const { 00162 return original_contents_.size(); 00163 } 00164 00166 size_t output_size() { 00167 size_t ret; 00168 if (output_valid_ || ComputeOutputContents()) { 00169 ret = output_contents_.size(); 00170 } else { 00171 ret = input_size(); 00172 } 00173 return ret; 00174 } 00175 00176 ImageType image_type() { 00177 if (image_type_ == IMAGE_UNKNOWN) { 00178 ComputeImageType(); 00179 } 00180 return image_type_; 00181 } 00182 00185 ResourceContext::LibWebpLevel MinimalWebpSupport() { 00186 if (!rewrite_attempted_) { 00187 ComputeOutputContents(); 00188 } 00189 return minimal_webp_support_; 00190 } 00191 00195 virtual bool ResizeTo(const ImageDim& new_dim) = 0; 00196 00200 virtual void SetTransformToLowRes() = 0; 00201 00204 const ContentType* content_type() { 00205 return TypeToContentType(image_type()); 00206 } 00207 00210 StringPiece Contents(); 00211 00214 virtual bool DrawImage(Image* image, int x, int y) = 0; 00215 00221 virtual bool EnsureLoaded(bool output_useful) = 0; 00222 00224 virtual const GoogleString& url() = 0; 00225 00226 protected: 00227 explicit Image(const StringPiece& original_contents); 00228 explicit Image(ImageType type); 00229 00231 virtual void ComputeImageType() = 0; 00232 virtual bool ComputeOutputContents() = 0; 00233 00235 virtual void SetResizedDimensions(const ImageDim& dim) = 0; 00236 00239 virtual bool ShouldConvertToProgressive(int64 quality) const = 0; 00240 00241 00242 ImageType image_type_; 00243 const StringPiece original_contents_; 00244 GoogleString output_contents_; 00245 bool output_valid_; 00246 bool rewrite_attempted_; 00247 ResourceContext::LibWebpLevel minimal_webp_support_; 00248 00249 private: 00250 friend class ImageTestingPeer; 00251 friend class ImageTest; 00252 00253 DISALLOW_COPY_AND_ASSIGN(Image); 00254 }; 00255 00266 Image* NewImage(const StringPiece& original_contents, 00267 const GoogleString& url, 00268 const StringPiece& file_prefix, 00269 Image::CompressionOptions* options, 00270 Timer* timer, 00271 MessageHandler* handler); 00272 00275 Image* BlankImageWithOptions(int width, int height, ImageType type, 00276 const StringPiece& tmp_dir, 00277 Timer* timer, 00278 MessageHandler* handler, 00279 Image::CompressionOptions* options); 00280 00281 } 00282 00283 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_