00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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::kDefaultImageWebpRecompressQuality),
00061 jpeg_quality(RewriteOptions::kDefaultImageJpegRecompressQuality),
00062 progressive_jpeg(false),
00063 convert_png_to_jpeg(false),
00064 retain_color_profile(false),
00065 retain_color_sampling(false),
00066 retain_exif_data(false),
00067 jpeg_num_progressive_scans(
00068 RewriteOptions::kDefaultImageJpegNumProgressiveScans) {}
00069 bool webp_preferred;
00070 int webp_quality;
00071 int jpeg_quality;
00072 bool progressive_jpeg;
00073 bool convert_png_to_jpeg;
00074 bool retain_color_profile;
00075 bool retain_color_sampling;
00076 bool retain_exif_data;
00077 int jpeg_num_progressive_scans;
00078 };
00079
00080 virtual ~Image();
00081
00083 static const ContentType* TypeToContentType(Type t);
00084
00086 static const Type kImageTypeStart = IMAGE_UNKNOWN;
00087 static const Type kImageTypeEnd = IMAGE_WEBP;
00088
00097 virtual void Dimensions(ImageDim* natural_dim) = 0;
00098
00100 size_t input_size() const {
00101 return original_contents_.size();
00102 }
00103
00105 size_t output_size() {
00106 size_t ret;
00107 if (output_valid_ || ComputeOutputContents()) {
00108 ret = output_contents_.size();
00109 } else {
00110 ret = input_size();
00111 }
00112 return ret;
00113 }
00114
00115 Type image_type() {
00116 if (image_type_ == IMAGE_UNKNOWN) {
00117 ComputeImageType();
00118 }
00119 return image_type_;
00120 }
00121
00125 virtual bool ResizeTo(const ImageDim& new_dim) = 0;
00126
00130 virtual void SetTransformToLowRes() = 0;
00131
00134 const ContentType* content_type() {
00135 return TypeToContentType(image_type());
00136 }
00137
00140 StringPiece Contents();
00141
00144 virtual bool DrawImage(Image* image, int x, int y) = 0;
00145
00151 virtual bool EnsureLoaded(bool output_useful) = 0;
00152
00153 protected:
00154 explicit Image(const StringPiece& original_contents);
00155 explicit Image(Type type);
00156
00158 virtual void ComputeImageType() = 0;
00159 virtual bool ComputeOutputContents() = 0;
00160
00161 Type image_type_;
00162 const StringPiece original_contents_;
00163 GoogleString output_contents_;
00164 bool output_valid_;
00165
00166 private:
00167 friend class ImageTest;
00168
00169 DISALLOW_COPY_AND_ASSIGN(Image);
00170 };
00171
00185 Image* NewImage(const StringPiece& original_contents,
00186 const GoogleString& url,
00187 const StringPiece& file_prefix,
00188 Image::CompressionOptions* options,
00189 MessageHandler* handler);
00190
00193 Image* BlankImage(int width, int height, Image::Type type,
00194 const StringPiece& tmp_dir, MessageHandler* handler);
00195
00196 }
00197
00198 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_IMAGE_H_