~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/image/jpeg_optimizer.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 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
 
 
17
 
// Author: Bryan McQuade, Matthew Steele, Satyanarayana Manyam
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_IMAGE_JPEG_OPTIMIZER_H_
20
 
#define PAGESPEED_KERNEL_IMAGE_JPEG_OPTIMIZER_H_
21
 
 
22
 
#include <setjmp.h>
23
 
#include <cstddef>
24
 
#include "pagespeed/kernel/base/basictypes.h"
25
 
#include "pagespeed/kernel/base/string.h"
26
 
#include "pagespeed/kernel/image/image_util.h"
27
 
#include "pagespeed/kernel/image/scanline_interface.h"
28
 
#include "pagespeed/kernel/image/scanline_status.h"
29
 
 
30
 
// DO NOT INCLUDE LIBJPEG HEADERS HERE. Doing so causes build errors
31
 
// on Windows.
32
 
 
33
 
namespace net_instaweb {
34
 
class MessageHandler;
35
 
}
36
 
 
37
 
namespace pagespeed {
38
 
 
39
 
namespace image_compression {
40
 
 
41
 
using net_instaweb::MessageHandler;
42
 
 
43
 
enum ColorSampling {
44
 
  RETAIN,
45
 
  YUV420,
46
 
  YUV422,
47
 
  YUV444
48
 
};
49
 
 
50
 
struct JpegLossyOptions {
51
 
  JpegLossyOptions() : quality(85), num_scans(-1), color_sampling(YUV420) {}
52
 
  // jpeg_quality - Can take values in the range [1,100].
53
 
  // For web images, the preferred value for quality is 85.
54
 
  // For smaller images like thumbnails, the preferred value for quality is 75.
55
 
  // Setting it to values below 50 is generally not preferable.
56
 
  int quality;
57
 
 
58
 
  // No. of progressive scan that needs to be included in the final output.
59
 
  // -1 indicates to use all scans that are present.
60
 
  int num_scans;
61
 
 
62
 
  // Color sampling that needs to be used while recompressing the image.
63
 
  ColorSampling color_sampling;
64
 
};
65
 
 
66
 
struct JpegCompressionOptions {
67
 
  JpegCompressionOptions()
68
 
    : progressive(false), retain_color_profile(false),
69
 
      retain_exif_data(false), lossy(false) {}
70
 
 
71
 
  // Whether or not to produce a progressive JPEG. This parameter will only be
72
 
  // applied for images with YCbCr colorspace, and it is ignored for other
73
 
  // colorspaces.
74
 
  bool progressive;
75
 
 
76
 
  // If set to 'true' any color profile information is retained.
77
 
  bool retain_color_profile;
78
 
 
79
 
  // If set to 'true' any exif information is retained.
80
 
  bool retain_exif_data;
81
 
 
82
 
  // Whether or not to use lossy compression.
83
 
  bool lossy;
84
 
 
85
 
  // Lossy compression options. Only applicable if lossy (above) is set to true.
86
 
  JpegLossyOptions lossy_options;
87
 
};
88
 
 
89
 
// Performs lossless optimization, that is, the output image will be
90
 
// pixel-for-pixel identical to the input image.
91
 
bool OptimizeJpeg(const GoogleString &original,
92
 
                  GoogleString *compressed,
93
 
                  MessageHandler* handler);
94
 
 
95
 
// Performs JPEG optimizations with the provided options.
96
 
bool OptimizeJpegWithOptions(const GoogleString &original,
97
 
                             GoogleString *compressed,
98
 
                             const JpegCompressionOptions &options,
99
 
                             MessageHandler* handler);
100
 
 
101
 
// User of this class must call this functions in the following sequence
102
 
// func () {
103
 
//   JpegScanlineWriter jpeg_writer;
104
 
//   jmp_buf env;
105
 
//   if (setjmp(env)) {
106
 
//     jpeg_writer.AbortWrite();
107
 
//     return;
108
 
//   }
109
 
//   jpeg_writer.SetJmpBufEnv(&env);
110
 
//   if (jpeg_writer.Init(width, height, format)) {
111
 
//     jpeg_writer.SetJpegCompressParams(quality);
112
 
//     jpeg_writer.InitializeWrite(out);
113
 
//     while(has_lines_to_write) {
114
 
//       writer.WriteNextScanline(next_scan_line);
115
 
//     }
116
 
//     writer.FinalizeWrite()
117
 
//   }
118
 
// }
119
 
class JpegScanlineWriter : public ScanlineWriterInterface {
120
 
 public:
121
 
  explicit JpegScanlineWriter(MessageHandler* handler);
122
 
  virtual ~JpegScanlineWriter();
123
 
 
124
 
  // Set the environment for longjmp calls.
125
 
  void SetJmpBufEnv(jmp_buf* env);
126
 
 
127
 
  // This function is only called when jpeg library call longjmp for
128
 
  // cleaning up the jpeg structs.
129
 
  void AbortWrite();
130
 
 
131
 
  virtual ScanlineStatus InitWithStatus(const size_t width, const size_t height,
132
 
                                        PixelFormat pixel_format);
133
 
  // Set the compression options via 'params', which should be a
134
 
  // JpegCompressionOptions*. Since writer only supports lossy
135
 
  // encoding, it is an error to pass in a 'params' that has the lossy
136
 
  // field set to false.
137
 
  virtual ScanlineStatus InitializeWriteWithStatus(const void* params,
138
 
                                                   GoogleString *compressed);
139
 
  virtual ScanlineStatus WriteNextScanlineWithStatus(
140
 
      const void *scanline_bytes);
141
 
  virtual ScanlineStatus FinalizeWriteWithStatus();
142
 
 
143
 
 private:
144
 
  // Since writer only supports lossy encoding, it is an error to pass
145
 
  // in a compression options that has lossy field set to false.
146
 
  void SetJpegCompressParams(const JpegCompressionOptions& options);
147
 
 
148
 
  // Opaque struct that is defined in the cc file and contains our
149
 
  // JPEG-compressor-specific structures.
150
 
  struct Data;
151
 
  Data* const data_;
152
 
  MessageHandler* message_handler_;
153
 
 
154
 
  DISALLOW_COPY_AND_ASSIGN(JpegScanlineWriter);
155
 
};
156
 
 
157
 
}  // namespace image_compression
158
 
 
159
 
}  // namespace pagespeed
160
 
 
161
 
#endif  // PAGESPEED_KERNEL_IMAGE_JPEG_OPTIMIZER_H_