Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
webp_optimizer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2009 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_WEBP_OPTIMIZER_H_
20 #define PAGESPEED_KERNEL_IMAGE_WEBP_OPTIMIZER_H_
21 
22 #include <cstddef>
23 #include "third_party/libwebp/src/webp/encode.h"
24 #include "third_party/libwebp/src/webp/mux.h"
32 
33 namespace net_instaweb {
34 class MessageHandler;
35 }
36 
37 namespace pagespeed {
38 
39 namespace image_compression {
40 
42 
46 
47  typedef bool (*WebpProgressHook)(int percent, void* user_data);
48 
50  : lossless(true), quality(75), method(3), target_size(0),
52  kmin(0), kmax(0), progress_hook(NULL), user_data(NULL) {}
53 
54  ~WebpConfiguration() override;
55 
56  void CopyTo(WebPConfig* webp_config) const;
57 
58  int lossless;
59  float quality;
60  int method;
61 
64  int alpha_compression;
66  int alpha_filtering;
68  int alpha_quality;
70 
73  size_px kmin;
74  size_px kmax;
80 
82 
83  void* user_data;
84 
91 };
92 
94  public:
95  explicit WebpFrameWriter(MessageHandler* handler);
96  virtual ~WebpFrameWriter();
97 
100  virtual ScanlineStatus Initialize(const void* config, GoogleString* out);
101 
104  virtual ScanlineStatus PrepareImage(const ImageSpec* image_spec);
105 
107  virtual ScanlineStatus PrepareNextFrame(const FrameSpec* frame_spec);
108 
109  virtual ScanlineStatus WriteNextScanline(const void *scanline_bytes);
110 
114  virtual ScanlineStatus FinalizeWrite();
115 
116  private:
120  static int ProgressHook(int percent, const WebPPicture* picture);
121 
123  ScanlineStatus CacheCurrentFrame();
124 
126  void FreeWebpStructs();
127 
129  const ImageSpec* image_spec_;
130  FrameSpec frame_spec_;
131 
134  size_px next_frame_;
135 
137  size_px next_scanline_;
138 
144  bool empty_frame_;
145 
147  size_px frame_stride_px_;
148 
151  uint32_t* frame_position_px_;
152 
154  uint32_t frame_bytes_per_pixel_;
155 
157  WebPPicture webp_image_;
158 
160  WebPPicture* webp_image_restore_;
161 
163  FrameSpec previous_frame_spec_;
164 
166  WebPAnimEncoder* webp_encoder_;
167 
169  WebPConfig libwebp_config_;
170 
172  int timestamp_;
173 
174 #ifndef NDEBUG
175  WebPAuxStats stats_;
176 #endif
177 
179  GoogleString* output_image_;
180 
182  bool has_alpha_;
183 
185  bool image_prepared_;
186 
189 
193  void* progress_hook_data_;
194 
198  bool should_expand_gray_to_rgb_;
199 
201  size_px kmin_;
202  size_px kmax_;
203 
204 
205 };
206 
212  public:
213  explicit WebpScanlineReader(MessageHandler* handler);
214  virtual ~WebpScanlineReader();
215 
217  virtual bool Reset();
218 
221  virtual ScanlineStatus InitializeWithStatus(const void* image_buffer,
222  size_t buffer_length);
223 
227  virtual ScanlineStatus ReadNextScanlineWithStatus(void** out_scanline_bytes);
228 
230  virtual size_t GetBytesPerScanline() { return bytes_per_row_; }
231 
232  virtual bool HasMoreScanLines() { return (row_ < height_); }
233  virtual PixelFormat GetPixelFormat() { return pixel_format_; }
234  virtual size_t GetImageHeight() { return height_; }
235  virtual size_t GetImageWidth() { return width_; }
237  virtual bool IsProgressive() { return false; }
238 
239  private:
241  const uint8_t* image_buffer_;
242  int buffer_length_;
243 
244  PixelFormat pixel_format_;
245  size_t height_;
246  size_t width_;
247  size_t bytes_per_row_;
248  size_t row_;
249  bool was_initialized_;
250 
253 
254  MessageHandler* message_handler_;
255 
256 
257 };
258 
259 }
260 
261 }
262 
263 #endif
virtual ScanlineStatus WriteNextScanline(const void *scanline_bytes)
int target_size
Parameters related to lossy compression only:
Definition: webp_optimizer.h:63
Definition: scanline_status.h:93
int alpha_quality
0: none, 1: fast, 2: best. Default is 1.
Definition: webp_optimizer.h:69
virtual PixelFormat GetPixelFormat()
Returns the pixel format that need to be used by writer.
Definition: webp_optimizer.h:233
WebpProgressHook progress_hook
If non-NULL, called during encoding.
Definition: webp_optimizer.h:81
virtual bool IsProgressive()
WebP does not have progressive mode.
Definition: webp_optimizer.h:237
virtual bool Reset()
Reset the scanline reader to its initial state.
bool(* WebpProgressHook)(int percent, void *user_data)
Definition: webp_optimizer.h:47
Definition: image_frame_interface.h:279
virtual size_t GetBytesPerScanline()
Return the number of bytes in a row (without padding).
Definition: webp_optimizer.h:230
virtual ScanlineStatus PrepareNextFrame(const FrameSpec *frame_spec)
frame_spec must remain valid while the frame is being written.
int method
quality/speed trade-off (0=fast, 6=slower-better)
Definition: webp_optimizer.h:60
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
int alpha_filtering
1 = compressed with WebP lossless). Default is 1.
Definition: webp_optimizer.h:67
Definition: webp_optimizer.h:93
Definition: webp_optimizer.h:211
Definition: image_frame_interface.h:43
Definition: image_frame_interface.h:79
virtual ScanlineStatus PrepareImage(const ImageSpec *image_spec)
float quality
between 0 (smallest file) and 100 (biggest)
Definition: webp_optimizer.h:59
int alpha_compression
Takes precedence over the 'compression' parameter.
Definition: webp_optimizer.h:65
virtual size_t GetImageHeight()
Returns the height of the image.
Definition: webp_optimizer.h:234
size_px kmax
Maximum keyframe interval.
Definition: webp_optimizer.h:79
int lossless
Lossless encoding (0=lossy(default), 1=lossless).
Definition: webp_optimizer.h:58
virtual bool HasMoreScanLines()
Returns true if there are more scanlines to read.
Definition: webp_optimizer.h:232
virtual ScanlineStatus Initialize(const void *config, GoogleString *out)
Definition: message_handler.h:39
void * user_data
Definition: webp_optimizer.h:83
virtual size_t GetImageWidth()
Returns the width of the image.
Definition: webp_optimizer.h:235
virtual ScanlineStatus ReadNextScanlineWithStatus(void **out_scanline_bytes)
virtual ScanlineStatus InitializeWithStatus(const void *image_buffer, size_t buffer_length)
size_px kmin
Default is 100.
Definition: webp_optimizer.h:73