Page Speed Optimization Libraries
1.7.30.1
|
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::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 00186 virtual bool ResizeTo(const ImageDim& new_dim) = 0; 00187 00191 virtual void SetTransformToLowRes() = 0; 00192 00195 const ContentType* content_type() { 00196 return TypeToContentType(image_type()); 00197 } 00198 00201 StringPiece Contents(); 00202 00205 virtual bool DrawImage(Image* image, int x, int y) = 0; 00206 00212 virtual bool EnsureLoaded(bool output_useful) = 0; 00213 00215 virtual const GoogleString& url() = 0; 00216 00217 protected: 00218 explicit Image(const StringPiece& original_contents); 00219 explicit Image(ImageType type); 00220 00222 virtual void ComputeImageType() = 0; 00223 virtual bool ComputeOutputContents() = 0; 00224 00226 virtual void SetResizedDimensions(const ImageDim& dim) = 0; 00227 00230 virtual bool ShouldConvertToProgressive(int64 quality) const = 0; 00231 00232 00233 ImageType image_type_; 00234 const StringPiece original_contents_; 00235 GoogleString output_contents_; 00236 bool output_valid_; 00237 bool rewrite_attempted_; 00238 00239 private: 00240 friend class ImageTestingPeer; 00241 friend class ImageTest; 00242 00243 DISALLOW_COPY_AND_ASSIGN(Image); 00244 }; 00245 00256 Image* NewImage(const StringPiece& original_contents, 00257 const GoogleString& url, 00258 const StringPiece& file_prefix, 00259 Image::CompressionOptions* options, 00260 Timer* timer, 00261 MessageHandler* handler); 00262 00265 Image* BlankImageWithOptions(int width, int height, ImageType type, 00266 const StringPiece& tmp_dir, 00267 Timer* timer, 00268 MessageHandler* handler, 00269 Image::CompressionOptions* options); 00270 00271 } 00272 00273 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_