~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/image/image_converter.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 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
 
 
17
 
// Author: Satyanarayana Manyam
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_IMAGE_IMAGE_CONVERTER_H_
20
 
#define PAGESPEED_KERNEL_IMAGE_IMAGE_CONVERTER_H_
21
 
 
22
 
#include "pagespeed/kernel/base/basictypes.h"
23
 
#include "pagespeed/kernel/base/string.h"
24
 
#include "pagespeed/kernel/image/jpeg_optimizer.h"
25
 
#include "pagespeed/kernel/image/scanline_status.h"
26
 
#include "pagespeed/kernel/image/webp_optimizer.h"
27
 
 
28
 
namespace net_instaweb {
29
 
class MessageHandler;
30
 
}
31
 
 
32
 
namespace pagespeed {
33
 
 
34
 
namespace image_compression {
35
 
 
36
 
using net_instaweb::MessageHandler;
37
 
 
38
 
class MultipleFrameReader;
39
 
class MultipleFrameWriter;
40
 
class PngReaderInterface;
41
 
class ScanlineReaderInterface;
42
 
class ScanlineWriterInterface;
43
 
 
44
 
class ImageConverter {
45
 
 public:
46
 
  enum ImageType {
47
 
    IMAGE_NONE = 0,
48
 
    IMAGE_PNG,
49
 
    IMAGE_JPEG,
50
 
    IMAGE_WEBP
51
 
  };
52
 
 
53
 
  // Converts image one line at a time, between different image
54
 
  // formats. Both 'reader' and 'writer' must be non-NULL.
55
 
  static ScanlineStatus ConvertImageWithStatus(
56
 
      ScanlineReaderInterface* reader,
57
 
      ScanlineWriterInterface* writer);
58
 
 
59
 
  inline static bool ConvertImage(ScanlineReaderInterface* reader,
60
 
                                  ScanlineWriterInterface* writer) {
61
 
    return ConvertImageWithStatus(reader, writer).Success();
62
 
  }
63
 
 
64
 
  // Converts image frame by frame, and then line by line within each
65
 
  // frame, between different image formats. Both 'reader' and
66
 
  // 'writer' must be non-NULL.
67
 
  static ScanlineStatus ConvertMultipleFrameImage(MultipleFrameReader* reader,
68
 
                                                  MultipleFrameWriter* writer);
69
 
 
70
 
  static bool ConvertPngToJpeg(
71
 
      const PngReaderInterface& png_struct_reader,
72
 
      const GoogleString& in,
73
 
      const JpegCompressionOptions& options,
74
 
      GoogleString* out,
75
 
      MessageHandler* handler);
76
 
 
77
 
  // Reads the PNG encoded in 'in' with 'png_struct_reader', encodes
78
 
  // it in WebP format using the options in 'config', and writes the
79
 
  // resulting WebP in 'out'. Note that if config.alpha_quality==0,
80
 
  // this function will fail when attempting to convert an image with
81
 
  // transparent pixels. Returns is_opaque set to true iff the 'in'
82
 
  // image was opaque.
83
 
  static bool ConvertPngToWebp(
84
 
      const PngReaderInterface& png_struct_reader,
85
 
      const GoogleString& in,
86
 
      const WebpConfiguration& config,
87
 
      GoogleString* out,
88
 
      bool* is_opaque,
89
 
      MessageHandler* handler);
90
 
 
91
 
  // Reads the PNG encoded in 'in' with 'png_struct_reader', encodes
92
 
  // it in WebP format using the options in 'config', and writes the
93
 
  // resulting WebP in 'out'. Note that if config.alpha_quality==0,
94
 
  // this function will fail when attempting to convert an image with
95
 
  // transparent pixels. Returns is_opaque set to true iff the 'in'
96
 
  // image was opaque. On entry, '*webp_writer' must be NULL; on exit,
97
 
  // it contains the webp writer that was used to write the WebP, and
98
 
  // the caller is responsible for deleting it. Most clients will
99
 
  // prefer to use the other form ConvertPngToWebp.
100
 
  static bool ConvertPngToWebp(
101
 
      const PngReaderInterface& png_struct_reader,
102
 
      const GoogleString& in,
103
 
      const WebpConfiguration& config,
104
 
      GoogleString* out,
105
 
      bool* is_opaque,
106
 
      ScanlineWriterInterface** webp_writer,
107
 
      MessageHandler* handler);
108
 
 
109
 
  // Optimizes the given png image, also converts to jpeg and take the
110
 
  // the one that has smaller size and set the output. Returns false
111
 
  // if both of them fails.
112
 
  static bool OptimizePngOrConvertToJpeg(
113
 
      const PngReaderInterface& png_struct_reader,
114
 
      const GoogleString& in,
115
 
      const JpegCompressionOptions& options,
116
 
      GoogleString* out,
117
 
      bool* is_out_png,
118
 
      MessageHandler* handler);
119
 
 
120
 
  // Populates 'out' with a version of the input image 'in' resulting
121
 
  // in the smallest size, and returns the corresponding
122
 
  // ImageType. The image formats that are candidates for the output
123
 
  // image are: lossless WebP, optimized PNG, custom JPEG (if
124
 
  // jpeg_options != NULL), and custom WebP (if webp_config !=
125
 
  // NULL). To compensate for the loss in quality in the custom JPEG
126
 
  // and WebP (which are presumably lossy), these two formats must be
127
 
  // substantially smaller than the optimized PNG and the lossless
128
 
  // WebP in order to be chosen. In the case where none of these image
129
 
  // formats could be generated or the original image turns out to be
130
 
  // the smallest, copies the original image to 'out' and returns
131
 
  // IMAGE_NONE.
132
 
  static ImageType GetSmallestOfPngJpegWebp(
133
 
      // TODO(bmcquade): should be a ScanlineReaderInterface.
134
 
      const PngReaderInterface& png_struct_reader,
135
 
      const GoogleString& in,
136
 
      const JpegCompressionOptions* jpeg_options,
137
 
      const WebpConfiguration* webp_config,
138
 
      GoogleString* out,
139
 
      MessageHandler* handler);
140
 
 
141
 
 private:
142
 
  ImageConverter();
143
 
  ~ImageConverter();
144
 
 
145
 
  DISALLOW_COPY_AND_ASSIGN(ImageConverter);
146
 
};
147
 
 
148
 
}  // namespace image_compression
149
 
 
150
 
}  // namespace pagespeed
151
 
 
152
 
#endif  // PAGESPEED_KERNEL_IMAGE_IMAGE_CONVERTER_H_