Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image_optimizer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_IMAGE_IMAGE_OPTIMIZER_H_
20 #define PAGESPEED_KERNEL_IMAGE_IMAGE_OPTIMIZER_H_
21 
22 #include <memory>
23 
28 #include "pagespeed/kernel/http/image_types.pb.h"
29 #include "pagespeed/kernel/image/image_optimizer.pb.h"
31 
32 namespace net_instaweb {
33 class Timer;
34 }
35 
36 namespace pagespeed {
37 
38 namespace image_compression {
39 
54  public:
55  explicit ImageOptimizer(net_instaweb::MessageHandler* message_handler) :
56  message_handler_(message_handler) {
57  }
58 
65  bool Optimize(StringPiece original_contents,
66  GoogleString* optimized_contents,
67  ImageFormat* optimized_format);
68 
69  void set_options(const pagespeed::image_compression::ImageOptions& options) {
70  options_ = options;
71  }
72 
80  const pagespeed::image_compression::ImageDimensions&
81  requested_dimensions) {
82  requested_dim_ = requested_dimensions;
83  }
84 
87  int optimized_width() { return optimized_width_; }
88  int optimized_height() { return optimized_height_; }
89 
91  void set_timer(net_instaweb::Timer* timer) { timer_ = timer; }
92  bool was_timed_out() const { return was_timed_out_; }
93 
96  bool UsesLossyFormat() const { return !desired_lossless_; }
97 
98  private:
99  bool Run();
100  bool ComputeDesiredFormat();
101  bool ComputeResizedDimension();
102  bool ComputeDesiredQualityProgressive();
103  ImageFormat ImageTypeToImageFormat(net_instaweb::ImageType image_type);
104  bool ConfigureWriter();
105  bool RewriteSingleFrameImage();
106  bool RewriteAnimatedImage();
107 
109  net_instaweb::MessageHandler* const message_handler_;
110  pagespeed::image_compression::ImageOptions options_;
111  StringPiece original_contents_;
112  pagespeed::image_compression::ImageDimensions requested_dim_;
113  GoogleString* optimized_contents_ = nullptr;
114  net_instaweb::Timer* timer_ = nullptr;
115  std::unique_ptr<ConversionTimeoutHandler> timeout_handler_;
116  bool was_timed_out_ = false;
117 
119  ImageFormat original_format_;
120  int original_width_ = -1;
121  int original_height_ = -1;
122  bool is_progressive_ = false;
123  bool is_animated_ = false;
124  bool is_transparent_ = false;
125  bool is_photo_ = false;
126  int original_quality_ = -1;
127 
129  ImageFormat optimized_format_;
130  int optimized_width_ = -1;
131  int optimized_height_ = -1;
132  int desired_quality_ = -1;
133  bool desired_progressive_ = false;
134  bool desired_lossless_ = false;
135  std::unique_ptr<ScanlineWriterConfig> writer_config_;
136  bool is_valid_ = true;
137 
138 
139 };
140 
141 }
142 
143 }
144 
145 #endif
void set_requested_dimension(const pagespeed::image_compression::ImageDimensions &requested_dimensions)
Definition: image_optimizer.h:79
int optimized_width()
Definition: image_optimizer.h:87
Definition: image_optimizer.h:53
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
bool UsesLossyFormat() const
Definition: image_optimizer.h:96
void set_timer(net_instaweb::Timer *timer)
Timer and was_timed_out only apply to WebP images.
Definition: image_optimizer.h:91
bool Optimize(StringPiece original_contents, GoogleString *optimized_contents, ImageFormat *optimized_format)
Definition: message_handler.h:39
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27