Page Speed Optimization Libraries  1.3.25.1
net/instaweb/rewriter/public/image.h
Go to the documentation of this file.
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/public/rewrite_options.h"
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/util/public/string.h"
00027 #include "net/instaweb/util/public/string_util.h"
00028 
00029 namespace net_instaweb {
00030 class ImageDim;
00031 class MessageHandler;
00032 class Timer;
00033 struct ContentType;
00034 
00035 class Image {
00036  public:
00048 
00049   enum Type {
00051     IMAGE_UNKNOWN = 0,
00052     IMAGE_JPEG,
00053     IMAGE_PNG,
00054     IMAGE_GIF,
00055     IMAGE_WEBP,
00056     IMAGE_WEBP_LOSSLESS_OR_ALPHA,  
00057 
00058   };
00059 
00060   enum PreferredWebp {
00061     WEBP_NONE = 0,
00062     WEBP_LOSSY,
00063     WEBP_LOSSLESS
00064   };
00065 
00066   struct CompressionOptions {
00067     CompressionOptions()
00068         : preferred_webp(WEBP_NONE),
00069           allow_webp_alpha(false),
00070           webp_quality(RewriteOptions::kDefaultImagesRecompressQuality),
00071           jpeg_quality(RewriteOptions::kDefaultImagesRecompressQuality),
00072           progressive_jpeg_min_bytes(
00073               RewriteOptions::kDefaultProgressiveJpegMinBytes),
00074           progressive_jpeg(false),
00075           convert_gif_to_png(false),
00076           convert_png_to_jpeg(false),
00077           convert_jpeg_to_webp(false),
00078           recompress_jpeg(false),
00079           recompress_png(false),
00080           recompress_webp(false),
00081           retain_color_profile(false),
00082           retain_color_sampling(false),
00083           retain_exif_data(false),
00084           jpeg_num_progressive_scans(
00085               RewriteOptions::kDefaultImageJpegNumProgressiveScans),
00086           webp_conversion_timeout_ms(-1),
00087           conversions_attempted(0),
00088           preserve_lossless(false) {}
00091     PreferredWebp preferred_webp;
00092     bool allow_webp_alpha;
00093     int64 webp_quality;
00094     int64 jpeg_quality;
00095     int64 progressive_jpeg_min_bytes;
00096     bool progressive_jpeg;
00097     bool convert_gif_to_png;
00098     bool convert_png_to_jpeg;
00099     bool convert_jpeg_to_webp;
00100     bool recompress_jpeg;
00101     bool recompress_png;
00102     bool recompress_webp;
00103     bool retain_color_profile;
00104     bool retain_color_sampling;
00105     bool retain_exif_data;
00106     int jpeg_num_progressive_scans;
00107     int64 webp_conversion_timeout_ms;
00108 
00111     int conversions_attempted;
00112     bool preserve_lossless;
00113   };
00114 
00115   virtual ~Image();
00116 
00118   static const ContentType* TypeToContentType(Type t);
00119 
00121   static const Type kImageTypeStart = IMAGE_UNKNOWN;
00122   static const Type kImageTypeEnd = IMAGE_WEBP_LOSSLESS_OR_ALPHA;
00123 
00132   virtual void Dimensions(ImageDim* natural_dim) = 0;
00133 
00135   size_t input_size() const {
00136     return original_contents_.size();
00137   }
00138 
00140   size_t output_size() {
00141     size_t ret;
00142     if (output_valid_ || ComputeOutputContents()) {
00143       ret = output_contents_.size();
00144     } else {
00145       ret = input_size();
00146     }
00147     return ret;
00148   }
00149 
00150   Type image_type() {
00151     if (image_type_ == IMAGE_UNKNOWN) {
00152       ComputeImageType();
00153     }
00154     return image_type_;
00155   }
00156 
00160   virtual bool ResizeTo(const ImageDim& new_dim) = 0;
00161 
00165   virtual void SetTransformToLowRes() = 0;
00166 
00169   const ContentType* content_type() {
00170     return TypeToContentType(image_type());
00171   }
00172 
00175   StringPiece Contents();
00176 
00179   virtual bool DrawImage(Image* image, int x, int y) = 0;
00180 
00186   virtual bool EnsureLoaded(bool output_useful) = 0;
00187 
00189   virtual const GoogleString& url() = 0;
00190 
00191  protected:
00192   explicit Image(const StringPiece& original_contents);
00193   explicit Image(Type type);
00194 
00196   virtual void ComputeImageType() = 0;
00197   virtual bool ComputeOutputContents() = 0;
00198 
00200   virtual void SetResizedDimensions(const ImageDim& dim) = 0;
00201 
00204   virtual bool ShouldConvertToProgressive(int64 quality) const = 0;
00205 
00206 
00207   Type image_type_; 
00208   const StringPiece original_contents_;
00209   GoogleString output_contents_; 
00210   bool output_valid_; 
00211   bool rewrite_attempted_; 
00212 
00213  private:
00214   friend class ImageTestingPeer;
00215   friend class ImageTest;
00216 
00217   DISALLOW_COPY_AND_ASSIGN(Image);
00218 };
00219 
00230 Image* NewImage(const StringPiece& original_contents,
00231                 const GoogleString& url,
00232                 const StringPiece& file_prefix,
00233                 Image::CompressionOptions* options,
00234                 Timer* timer,
00235                 MessageHandler* handler);
00236 
00239 Image* BlankImageWithOptions(int width, int height, Image::Type type,
00240                              const StringPiece& tmp_dir,
00241                              Timer* timer,
00242                              MessageHandler* handler,
00243                              Image::CompressionOptions* options);
00244 
00245 }  
00246 
00247 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines