Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
png_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_PNG_OPTIMIZER_H_
20 #define PAGESPEED_KERNEL_IMAGE_PNG_OPTIMIZER_H_
21 
24 
25 extern "C" {
26 #ifdef USE_SYSTEM_LIBPNG
27 #include "png.h"
28 #else
29 #include "third_party/libpng/src/png.h"
30 #endif
31 }
32 
33 #include <setjmp.h>
34 #include <cstddef>
35 
36 #include "third_party/optipng/src/opngreduc/opngreduc.h"
43 
44 namespace net_instaweb {
45 class MessageHandler;
46 }
47 
48 namespace pagespeed {
49 
50 namespace image_compression {
51 
53 
54 class ScanlineStreamInput;
55 
57  PngCompressParams(int level, int strategy, bool is_progressive);
58  PngCompressParams(bool try_best_compression, bool is_progressive);
59  ~PngCompressParams() override;
60 
83 };
84 
87  public:
88  enum Type {
89  READ,
90  WRITE
91  };
92 
93  ScopedPngStruct(Type type, MessageHandler* handler);
94  ~ScopedPngStruct();
95 
96  bool valid() const { return png_ptr_ != NULL && info_ptr_ != NULL; }
97 
100  bool reset();
101 
102  png_structp png_ptr() const { return png_ptr_; }
103  png_infop info_ptr() const { return info_ptr_; }
104 
105  private:
106  png_structp png_ptr_;
107  png_infop info_ptr_;
108  Type type_;
109  MessageHandler* message_handler_;
110 };
111 
115  public:
117  virtual ~PngReaderInterface();
118 
124  virtual bool ReadPng(const GoogleString& body,
125  png_structp png_ptr,
126  png_infop info_ptr,
127  int transforms,
128  bool require_opaque) const = 0;
129 
133  bool ReadPng(const GoogleString& body,
134  png_structp png_ptr,
135  png_infop info_ptr,
136  int transforms) const {
137  return ReadPng(body, png_ptr, info_ptr, transforms, false);
138  }
139 
144  virtual bool GetAttributes(const GoogleString& body,
145  int* out_width,
146  int* out_height,
147  int* out_bit_depth,
148  int* out_color_type) const = 0;
149 
153  static bool GetBackgroundColor(
154  png_structp png_ptr, png_infop info_ptr,
155  unsigned char *red, unsigned char* green, unsigned char* blue,
156  MessageHandler* handler);
157 
161  static bool IsAlphaChannelOpaque(png_structp png_ptr, png_infop info_ptr,
162  MessageHandler* handler);
163 
164  private:
165 
166 };
167 
185  public:
186  explicit PngScanlineReader(MessageHandler* handler);
187  virtual ~PngScanlineReader();
188 
189  jmp_buf* GetJmpBuf();
190 
193  virtual bool Reset();
194 
196  bool InitializeRead(const PngReaderInterface& reader, const GoogleString& in);
197  bool InitializeRead(const PngReaderInterface& reader, const GoogleString& in,
198  bool* is_opaque);
199 
200  virtual size_t GetBytesPerScanline();
201  virtual bool HasMoreScanLines();
202  virtual ScanlineStatus ReadNextScanlineWithStatus(void** out_scanline_bytes);
203  virtual size_t GetImageHeight();
204  virtual size_t GetImageWidth();
205  virtual PixelFormat GetPixelFormat();
206  virtual bool IsProgressive();
207 
208  void set_transform(int transform);
209  void set_require_opaque(bool require_opaque);
210  int GetColorType();
211  bool GetBackgroundColor(
212  unsigned char* red, unsigned char* green, unsigned char* blue);
213 
215  virtual ScanlineStatus InitializeWithStatus(const void* image_buffer,
216  size_t buffer_length);
217 
218  private:
219  ScopedPngStruct read_;
220  size_t current_scanline_;
221  int transform_;
222  bool require_opaque_;
223  MessageHandler* message_handler_;
224 
225 
226 };
227 
229  public:
230  static bool OptimizePng(const PngReaderInterface& reader,
231  const GoogleString& in,
232  GoogleString* out,
233  MessageHandler* handler);
234 
235  static bool OptimizePngBestCompression(const PngReaderInterface& reader,
236  const GoogleString& in,
237  GoogleString* out,
238  MessageHandler* handler);
239 
240  static bool CopyPngStructs(const ScopedPngStruct& from, ScopedPngStruct* to);
241 
242  private:
243  explicit PngOptimizer(MessageHandler* handler);
244  ~PngOptimizer();
245 
249  bool CreateOptimizedPng(const PngReaderInterface& reader,
250  const GoogleString& in,
251  GoogleString* out,
252  MessageHandler* handler);
253 
256  void EnableBestCompression() { best_compression_ = true; }
257 
258  bool WritePng(ScopedPngStruct* write, GoogleString* buffer);
259  bool CopyReadToWrite();
260  bool CreateBestOptimizedPngForParams(const PngCompressParams* param_list,
261  size_t param_list_size,
262  GoogleString* out);
263  bool CreateOptimizedPngWithParams(ScopedPngStruct* write,
264  const PngCompressParams& params,
265  GoogleString* out);
266  ScopedPngStruct read_;
267  ScopedPngStruct write_;
268  bool best_compression_;
269  MessageHandler* message_handler_;
270 
271 
272 };
273 
276  public:
277  explicit PngReader(MessageHandler* handler);
278  virtual ~PngReader();
279  virtual bool ReadPng(const GoogleString& body,
280  png_structp png_ptr,
281  png_infop info_ptr,
282  int transforms,
283  bool require_opaque) const;
284 
285  virtual bool GetAttributes(const GoogleString& body,
286  int* out_width,
287  int* out_height,
288  int* out_bit_depth,
289  int* out_color_type) const;
290 
291  private:
292  MessageHandler* message_handler_;
293 
294 };
295 
312  public:
313  explicit PngScanlineReaderRaw(MessageHandler* handler);
314  virtual ~PngScanlineReaderRaw();
315 
318  virtual bool Reset();
319 
322  virtual ScanlineStatus InitializeWithStatus(const void* image_buffer,
323  size_t buffer_length);
324 
330  virtual ScanlineStatus ReadNextScanlineWithStatus(void** out_scanline_bytes);
331 
333  virtual size_t GetBytesPerScanline() { return bytes_per_row_; }
334 
335  virtual bool HasMoreScanLines() { return (row_ < height_); }
336  virtual PixelFormat GetPixelFormat() { return pixel_format_; }
337  virtual size_t GetImageHeight() { return height_; }
338  virtual size_t GetImageWidth() { return width_; }
339  virtual bool IsProgressive() { return is_progressive_; }
340 
341  private:
342  PixelFormat pixel_format_;
343  bool is_progressive_;
344  size_t height_;
345  size_t width_;
346  size_t bytes_per_row_;
347  size_t row_;
348  bool was_initialized_;
356  MessageHandler* message_handler_;
357 
358 
359 };
360 
364  public:
365  explicit PngScanlineWriter(MessageHandler* handler);
366  virtual ~PngScanlineWriter();
367 
370  virtual ScanlineStatus InitWithStatus(const size_t width, const size_t height,
371  PixelFormat pixel_format);
372 
376  virtual ScanlineStatus InitializeWriteWithStatus(const void* params,
377  GoogleString* png_image);
378 
381  const void *scanline_bytes);
382 
387 
388  private:
390  bool Reset();
391 
393  bool Validate(const PngCompressParams* params,
394  GoogleString* png_image);
395 
396  bool DoBestCompression();
397 
398  private:
399  size_t width_;
400  size_t height_;
401  size_t bytes_per_row_;
402  size_t row_;
403  PixelFormat pixel_format_;
405  bool was_initialized_;
406  bool try_best_compression_;
408  MessageHandler* message_handler_;
409 
410 
411 };
412 
413 }
414 
415 }
416 
417 #endif
Definition: png_optimizer.h:228
Definition: scanline_status.h:93
bool is_progressive
Indicates whether to encode the image in progressive / interlacing format.
Definition: png_optimizer.h:82
virtual bool ReadPng(const GoogleString &body, png_structp png_ptr, png_infop info_ptr, int transforms, bool require_opaque) const
virtual bool IsProgressive()
Definition: png_optimizer.h:339
virtual bool HasMoreScanLines()
Returns true if there are more scanlines to read.
Definition: png_optimizer.h:335
bool InitializeRead(const PngReaderInterface &reader, const GoogleString &in)
Initializes the read structures with the given input.
virtual size_t GetImageWidth()
Returns the width of the image.
Definition: png_optimizer.h:338
virtual bool ReadPng(const GoogleString &body, png_structp png_ptr, png_infop info_ptr, int transforms, bool require_opaque) const =0
virtual ScanlineStatus FinalizeWriteWithStatus()
virtual ScanlineStatus InitializeWithStatus(const void *image_buffer, size_t buffer_length)
This is a no-op and should not be called.
Helper that manages the lifetime of the png_ptr and info_ptr.
Definition: png_optimizer.h:86
Definition: scoped_ptr.h:30
bool try_best_compression
Definition: png_optimizer.h:80
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual bool GetAttributes(const GoogleString &body, int *out_width, int *out_height, int *out_bit_depth, int *out_color_type) const
virtual size_t GetImageHeight()
Returns the height of the image.
virtual ScanlineStatus InitializeWriteWithStatus(const void *params, GoogleString *png_image)
virtual size_t GetBytesPerScanline()
Return the number of bytes in a row (without padding).
Definition: png_optimizer.h:333
virtual ScanlineStatus InitWithStatus(const size_t width, const size_t height, PixelFormat pixel_format)
virtual bool GetAttributes(const GoogleString &body, int *out_width, int *out_height, int *out_bit_depth, int *out_color_type) const =0
virtual size_t GetBytesPerScanline()
Returns number of bytes that required to store a scanline.
bool ReadPng(const GoogleString &body, png_structp png_ptr, png_infop info_ptr, int transforms) const
Definition: png_optimizer.h:133
virtual ScanlineStatus WriteNextScanlineWithStatus(const void *scanline_bytes)
Write a scanline with the data provided. Return false in case of error.
Reader for PNG-encoded data.
Definition: png_optimizer.h:275
int compression_strategy
Definition: png_optimizer.h:77
virtual PixelFormat GetPixelFormat()
Returns the pixel format that need to be used by writer.
Definition: png_optimizer.h:336
virtual ScanlineStatus InitializeWithStatus(const void *image_buffer, size_t buffer_length)
virtual size_t GetImageWidth()
Returns the width of the image.
Definition: message_handler.h:39
virtual bool HasMoreScanLines()
Returns true if there are more scanlines to read.
virtual size_t GetImageHeight()
Returns the height of the image.
Definition: png_optimizer.h:337
static bool GetBackgroundColor(png_structp png_ptr, png_infop info_ptr, unsigned char *red, unsigned char *green, unsigned char *blue, MessageHandler *handler)
virtual ScanlineStatus ReadNextScanlineWithStatus(void **out_scanline_bytes)
virtual PixelFormat GetPixelFormat()
Returns the pixel format that need to be used by writer.
virtual ScanlineStatus ReadNextScanlineWithStatus(void **out_scanline_bytes)
int filter_level
Definition: png_optimizer.h:69
static bool IsAlphaChannelOpaque(png_structp png_ptr, png_infop info_ptr, MessageHandler *handler)