Page Speed Optimization Libraries  1.2.24.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 struct ContentType;
00033 
00034 class Image {
00035  public:
00047 
00048   enum Type {
00050     IMAGE_UNKNOWN = 0,
00051     IMAGE_JPEG,
00052     IMAGE_PNG,
00053     IMAGE_GIF,
00054     IMAGE_WEBP,  
00055   };
00056 
00057   struct CompressionOptions {
00058     CompressionOptions()
00059         : webp_preferred(false),
00060           webp_quality(RewriteOptions::kDefaultImagesRecompressQuality),
00061           jpeg_quality(RewriteOptions::kDefaultImagesRecompressQuality),
00062           progressive_jpeg(false),
00063           convert_png_to_jpeg(false),
00064           convert_gif_to_png(false),
00065           recompress_jpeg(false),
00066           recompress_png(false),
00067           recompress_webp(false),
00068           retain_color_profile(false),
00069           retain_color_sampling(false),
00070           retain_exif_data(false),
00071           jpeg_num_progressive_scans(
00072               RewriteOptions::kDefaultImageJpegNumProgressiveScans) {}
00073     bool webp_preferred;
00074     int64 webp_quality;
00075     int64 jpeg_quality;
00076     bool progressive_jpeg;
00077     bool convert_png_to_jpeg;
00078     bool convert_gif_to_png;
00079     bool recompress_jpeg;
00080     bool recompress_png;
00081     bool recompress_webp;
00082     bool retain_color_profile;
00083     bool retain_color_sampling;
00084     bool retain_exif_data;
00085     int jpeg_num_progressive_scans;
00086   };
00087 
00088   virtual ~Image();
00089 
00091   static const ContentType* TypeToContentType(Type t);
00092 
00094   static const Type kImageTypeStart = IMAGE_UNKNOWN;
00095   static const Type kImageTypeEnd = IMAGE_WEBP;
00096 
00105   virtual void Dimensions(ImageDim* natural_dim) = 0;
00106 
00108   size_t input_size() const {
00109     return original_contents_.size();
00110   }
00111 
00113   size_t output_size() {
00114     size_t ret;
00115     if (output_valid_ || ComputeOutputContents()) {
00116       ret = output_contents_.size();
00117     } else {
00118       ret = input_size();
00119     }
00120     return ret;
00121   }
00122 
00123   Type image_type() {
00124     if (image_type_ == IMAGE_UNKNOWN) {
00125       ComputeImageType();
00126     }
00127     return image_type_;
00128   }
00129 
00133   virtual bool ResizeTo(const ImageDim& new_dim) = 0;
00134 
00138   virtual void SetTransformToLowRes() = 0;
00139 
00142   const ContentType* content_type() {
00143     return TypeToContentType(image_type());
00144   }
00145 
00148   StringPiece Contents();
00149 
00152   virtual bool DrawImage(Image* image, int x, int y) = 0;
00153 
00159   virtual bool EnsureLoaded(bool output_useful) = 0;
00160 
00161  protected:
00162   explicit Image(const StringPiece& original_contents);
00163   explicit Image(Type type);
00164 
00166   virtual void ComputeImageType() = 0;
00167   virtual bool ComputeOutputContents() = 0;
00168 
00169   Type image_type_; 
00170   const StringPiece original_contents_;
00171   GoogleString output_contents_; 
00172   bool output_valid_; 
00173 
00174  private:
00175   friend class ImageTest;
00176 
00177   DISALLOW_COPY_AND_ASSIGN(Image);
00178 };
00179 
00193 Image* NewImage(const StringPiece& original_contents,
00194                 const GoogleString& url,
00195                 const StringPiece& file_prefix,
00196                 Image::CompressionOptions* options,
00197                 MessageHandler* handler);
00198 
00201 Image* BlankImageWithOptions(int width, int height, Image::Type type,
00202                              const StringPiece& tmp_dir,
00203                              MessageHandler* handler,
00204                              Image::CompressionOptions* options);
00205 
00206 }  
00207 
00208 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines