~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/image/image_util.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 2014 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: Victor Chudnovsky
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_IMAGE_IMAGE_UTIL_H_
20
 
#define PAGESPEED_KERNEL_IMAGE_IMAGE_UTIL_H_
21
 
 
22
 
#include <cstddef>
23
 
 
24
 
#include "pagespeed/kernel/base/basictypes.h"
25
 
#include "pagespeed/kernel/base/countdown_timer.h"
26
 
#include "pagespeed/kernel/base/string.h"
27
 
#include "pagespeed/kernel/base/string_util.h"
28
 
 
29
 
namespace net_instaweb {
30
 
class MessageHandler;
31
 
class Timer;
32
 
}
33
 
 
34
 
namespace pagespeed {
35
 
 
36
 
namespace image_compression {
37
 
 
38
 
enum ImageFormat {
39
 
  IMAGE_UNKNOWN,
40
 
  IMAGE_JPEG,
41
 
  IMAGE_PNG,
42
 
  IMAGE_GIF,
43
 
  IMAGE_WEBP
44
 
};
45
 
 
46
 
enum PixelFormat {
47
 
  UNSUPPORTED,  // Not supported.
48
 
  RGB_888,      // RGB triplets, 24 bits per pixel.
49
 
  RGBA_8888,    // RGB triplet plus alpha channel, 32 bits per pixel.
50
 
  GRAY_8        // Grayscale, 8 bits per pixel.
51
 
};
52
 
 
53
 
enum RgbaChannels {
54
 
  RGBA_RED = 0,
55
 
  RGBA_GREEN,
56
 
  RGBA_BLUE,
57
 
  RGBA_ALPHA,
58
 
 
59
 
  RGBA_NUM_CHANNELS
60
 
};
61
 
 
62
 
const uint8_t kAlphaOpaque = 255;
63
 
const uint8_t kAlphaTransparent = 0;
64
 
typedef uint8_t PixelRgbaChannels[RGBA_NUM_CHANNELS];
65
 
 
66
 
// Packs the given A, R, G, B values into a single uint32.
67
 
inline uint32_t PackAsArgb(uint8_t alpha,
68
 
                           uint8_t red,
69
 
                           uint8_t green,
70
 
                           uint8_t blue) {
71
 
  return
72
 
      (static_cast<uint32_t>(alpha) << 24) |
73
 
      (red << 16) |
74
 
      (green << 8) |
75
 
      (blue);
76
 
}
77
 
 
78
 
// Packs a pixel's color channel data in RGBA format to a single
79
 
// uint32_t in ARGB format.
80
 
inline uint32_t RgbaToPackedArgb(const PixelRgbaChannels rgba) {
81
 
  return PackAsArgb(rgba[RGBA_ALPHA],
82
 
                    rgba[RGBA_RED],
83
 
                    rgba[RGBA_GREEN],
84
 
                    rgba[RGBA_BLUE]);
85
 
}
86
 
 
87
 
// Packs a pixel's color channel data in RGB format to a single
88
 
// uint32_t in ARGB format.
89
 
inline uint32_t RgbToPackedArgb(const PixelRgbaChannels rgba) {
90
 
  return PackAsArgb(kAlphaOpaque,
91
 
                    rgba[RGBA_RED],
92
 
                    rgba[RGBA_GREEN],
93
 
                    rgba[RGBA_BLUE]);
94
 
}
95
 
 
96
 
// Converts a pixel's grayscale data into a single uint32_t in ARGB
97
 
// format.
98
 
inline uint32_t GrayscaleToPackedArgb(const uint8_t luminance) {
99
 
  return PackAsArgb(kAlphaOpaque,
100
 
                    luminance,
101
 
                    luminance,
102
 
                    luminance);
103
 
}
104
 
 
105
 
// Sizes that can be measured in units of pixels: width, height,
106
 
// number of frames (a third dimension of the image), and indices into
107
 
// the same.
108
 
typedef uint32 size_px;
109
 
 
110
 
// Returns the MIME-type string corresponding to the given ImageFormat.
111
 
const char* ImageFormatToMimeTypeString(ImageFormat image_type);
112
 
 
113
 
// Returns a string representation of the given ImageFormat.
114
 
const char* ImageFormatToString(ImageFormat image_type);
115
 
 
116
 
// Returns a string representation of the given PixelFormat.
117
 
const char* GetPixelFormatString(PixelFormat pixel_format);
118
 
 
119
 
// Returns the number of bytes needed to encode each pixel in the
120
 
// given format.
121
 
size_t GetBytesPerPixel(PixelFormat pixel_format);
122
 
 
123
 
// Returns format of the image by inspecting magic numbers (cetain values at
124
 
// cetain bytes) in the file content. This method is super fast, but if a
125
 
// random binary file happens to have the magic numbers, it will incorrectly
126
 
// reports a format for the file. The problem will be corrected when the binary
127
 
// file is decoded.
128
 
ImageFormat ComputeImageFormat(const StringPiece& buf,
129
 
                               bool* is_webp_lossless_alpha);
130
 
 
131
 
// Class for managing image conversion timeouts.
132
 
class ConversionTimeoutHandler {
133
 
 public:
134
 
  ConversionTimeoutHandler(int64 time_allowed_ms,
135
 
                           net_instaweb::Timer* timer,
136
 
                           net_instaweb::MessageHandler* handler) :
137
 
    countdown_timer_(timer, NULL, time_allowed_ms),
138
 
    time_allowed_ms_(time_allowed_ms),
139
 
    time_elapsed_ms_(0),
140
 
    was_timed_out_(false),
141
 
    output_(NULL),
142
 
    handler_(handler) {}
143
 
 
144
 
  // Returns true if (1) the timer has not expired, or (2) the timer has
145
 
  // expired but "output_" is not empty which means that some data are
146
 
  // being written to it. This method can be passed as progress hook to
147
 
  // WebP writer. Input parameter "user_data" must point to a
148
 
  // ConversionTimeoutHandler object.
149
 
  static bool Continue(int percent, void* user_data);
150
 
 
151
 
  void Start(GoogleString* output) {
152
 
    output_ = output;
153
 
    countdown_timer_.Reset(time_allowed_ms_);
154
 
  }
155
 
 
156
 
  void Stop() {
157
 
    time_elapsed_ms_ = countdown_timer_.TimeElapsedMs();
158
 
  }
159
 
 
160
 
  bool was_timed_out() const { return was_timed_out_; }
161
 
  int64 time_elapsed_ms() const { return time_elapsed_ms_; }
162
 
 
163
 
 private:
164
 
  net_instaweb::CountdownTimer countdown_timer_;
165
 
  const int64 time_allowed_ms_;
166
 
  int64 time_elapsed_ms_;
167
 
  bool was_timed_out_;
168
 
  GoogleString* output_;
169
 
  net_instaweb::MessageHandler* handler_;
170
 
};
171
 
 
172
 
}  // namespace image_compression
173
 
 
174
 
}  // namespace pagespeed
175
 
 
176
 
#endif  // PAGESPEED_KERNEL_IMAGE_IMAGE_UTIL_H_