Page Speed Optimization Libraries
1.5.27.2
|
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/public/rewrite_options.h" 00026 #include "net/instaweb/util/public/basictypes.h" 00027 #include "net/instaweb/util/public/string.h" 00028 #include "net/instaweb/util/public/string_util.h" 00029 00030 namespace net_instaweb { 00031 class Histogram; 00032 class ImageDim; 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::kDefaultImagesRecompressQuality), 00095 jpeg_quality(RewriteOptions::kDefaultImagesRecompressQuality), 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 jpeg_num_progressive_scans( 00109 RewriteOptions::kDefaultImageJpegNumProgressiveScans), 00110 webp_conversion_timeout_ms(-1), 00111 conversions_attempted(0), 00112 preserve_lossless(false), 00113 webp_conversion_variables(NULL) {} 00114 00117 PreferredWebp preferred_webp; 00118 bool allow_webp_alpha; 00119 int64 webp_quality; 00120 int64 jpeg_quality; 00121 int64 progressive_jpeg_min_bytes; 00122 bool progressive_jpeg; 00123 bool convert_gif_to_png; 00124 bool convert_png_to_jpeg; 00125 bool convert_jpeg_to_webp; 00126 bool recompress_jpeg; 00127 bool recompress_png; 00128 bool recompress_webp; 00129 bool retain_color_profile; 00130 bool retain_color_sampling; 00131 bool retain_exif_data; 00132 int64 jpeg_num_progressive_scans; 00133 int64 webp_conversion_timeout_ms; 00134 00137 int conversions_attempted; 00138 bool preserve_lossless; 00139 00140 ConversionVariables* webp_conversion_variables; 00141 }; 00142 00143 virtual ~Image(); 00144 00146 static const ContentType* TypeToContentType(ImageType t); 00147 00156 virtual void Dimensions(ImageDim* natural_dim) = 0; 00157 00159 size_t input_size() const { 00160 return original_contents_.size(); 00161 } 00162 00164 size_t output_size() { 00165 size_t ret; 00166 if (output_valid_ || ComputeOutputContents()) { 00167 ret = output_contents_.size(); 00168 } else { 00169 ret = input_size(); 00170 } 00171 return ret; 00172 } 00173 00174 ImageType image_type() { 00175 if (image_type_ == IMAGE_UNKNOWN) { 00176 ComputeImageType(); 00177 } 00178 return image_type_; 00179 } 00180 00184 virtual bool ResizeTo(const ImageDim& new_dim) = 0; 00185 00189 virtual void SetTransformToLowRes() = 0; 00190 00193 const ContentType* content_type() { 00194 return TypeToContentType(image_type()); 00195 } 00196 00199 StringPiece Contents(); 00200 00203 virtual bool DrawImage(Image* image, int x, int y) = 0; 00204 00210 virtual bool EnsureLoaded(bool output_useful) = 0; 00211 00213 virtual const GoogleString& url() = 0; 00214 00215 protected: 00216 explicit Image(const StringPiece& original_contents); 00217 explicit Image(ImageType type); 00218 00220 virtual void ComputeImageType() = 0; 00221 virtual bool ComputeOutputContents() = 0; 00222 00224 virtual void SetResizedDimensions(const ImageDim& dim) = 0; 00225 00228 virtual bool ShouldConvertToProgressive(int64 quality) const = 0; 00229 00230 00231 ImageType image_type_; 00232 const StringPiece original_contents_; 00233 GoogleString output_contents_; 00234 bool output_valid_; 00235 bool rewrite_attempted_; 00236 00237 private: 00238 friend class ImageTestingPeer; 00239 friend class ImageTest; 00240 00241 DISALLOW_COPY_AND_ASSIGN(Image); 00242 }; 00243 00254 Image* NewImage(const StringPiece& original_contents, 00255 const GoogleString& url, 00256 const StringPiece& file_prefix, 00257 Image::CompressionOptions* options, 00258 Timer* timer, 00259 MessageHandler* handler); 00260 00263 Image* BlankImageWithOptions(int width, int height, ImageType type, 00264 const StringPiece& tmp_dir, 00265 Timer* timer, 00266 MessageHandler* handler, 00267 Image::CompressionOptions* options); 00268 00269 } 00270 00271 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_