Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image_util.h
Go to the documentation of this file.
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 
18 
19 #ifndef PAGESPEED_KERNEL_IMAGE_IMAGE_UTIL_H_
20 #define PAGESPEED_KERNEL_IMAGE_IMAGE_UTIL_H_
21 
22 #include <cstddef>
23 
28 #include "pagespeed/kernel/http/image_types.pb.h"
29 
30 namespace net_instaweb {
31 class MessageHandler;
32 class Timer;
33 }
34 
35 namespace pagespeed {
36 
37 namespace image_compression {
38 
43 enum QuirksMode {
44  QUIRKS_NONE = 0,
45  QUIRKS_CHROME,
46  QUIRKS_FIREFOX
47 };
48 
49 enum ImageFormat {
50  IMAGE_UNKNOWN,
51  IMAGE_JPEG,
52  IMAGE_PNG,
53  IMAGE_GIF,
54  IMAGE_WEBP
55 };
56 
57 enum PixelFormat {
58  UNSUPPORTED,
59  RGB_888,
60  RGBA_8888,
61  GRAY_8
62 };
63 
64 enum RgbaChannels {
65  RGBA_RED = 0,
66  RGBA_GREEN,
67  RGBA_BLUE,
68  RGBA_ALPHA,
69 
70  RGBA_NUM_CHANNELS
71 };
72 
73 enum PreferredLibwebpLevel {
74  WEBP_NONE = 0,
75  WEBP_LOSSY,
76  WEBP_LOSSLESS,
77  WEBP_ANIMATED
78 };
79 
80 const uint8_t kAlphaOpaque = 255;
81 const uint8_t kAlphaTransparent = 0;
82 typedef uint8_t PixelRgbaChannels[RGBA_NUM_CHANNELS];
83 
86 inline uint32_t PackHiToLo(uint8_t i3,
87  uint8_t i2,
88  uint8_t i1,
89  uint8_t i0) {
90  return
91  (static_cast<uint32_t>(i3) << 24) |
92  (i2 << 16) |
93  (i1 << 8) |
94  (i0);
95 }
96 
98 inline uint32_t PackAsArgb(uint8_t alpha,
99  uint8_t red,
100  uint8_t green,
101  uint8_t blue) {
102  return PackHiToLo(alpha, red, green, blue);
103 }
104 
107 inline uint32_t RgbaToPackedArgb(const PixelRgbaChannels rgba) {
108  return PackAsArgb(rgba[RGBA_ALPHA],
109  rgba[RGBA_RED],
110  rgba[RGBA_GREEN],
111  rgba[RGBA_BLUE]);
112 }
113 
116 inline uint32_t RgbToPackedArgb(const PixelRgbaChannels rgba) {
117  return PackAsArgb(kAlphaOpaque,
118  rgba[RGBA_RED],
119  rgba[RGBA_GREEN],
120  rgba[RGBA_BLUE]);
121 }
122 
125 inline uint32_t GrayscaleToPackedArgb(const uint8_t luminance) {
126  return PackAsArgb(kAlphaOpaque,
127  luminance,
128  luminance,
129  luminance);
130 }
131 
135 typedef uint32 size_px;
136 
138 const char* ImageFormatToMimeTypeString(ImageFormat image_type);
139 
141 const char* ImageFormatToString(ImageFormat image_type);
142 
144 const char* GetPixelFormatString(PixelFormat pixel_format);
145 
148 size_t GetBytesPerPixel(PixelFormat pixel_format);
149 
155 net_instaweb::ImageType ComputeImageType(const StringPiece& buf);
156 
159  public:
160  ConversionTimeoutHandler(int64 time_allowed_ms,
161  net_instaweb::Timer* timer,
162  net_instaweb::MessageHandler* handler) :
163  countdown_timer_(timer, NULL, time_allowed_ms),
164  time_allowed_ms_(time_allowed_ms),
165  time_elapsed_ms_(0),
166  was_timed_out_(false),
167  output_(NULL),
168  handler_(handler) {}
169 
175  static bool Continue(int percent, void* user_data);
176 
177  void Start(GoogleString* output) {
178  output_ = output;
179  countdown_timer_.Reset(time_allowed_ms_);
180  }
181 
182  void Stop() {
183  time_elapsed_ms_ = countdown_timer_.TimeElapsedMs();
184  }
185 
186  bool was_timed_out() const { return was_timed_out_; }
187  int64 time_elapsed_ms() const { return time_elapsed_ms_; }
188 
189  private:
190  net_instaweb::CountdownTimer countdown_timer_;
191  const int64 time_allowed_ms_;
192  int64 time_elapsed_ms_;
193  bool was_timed_out_;
194  GoogleString* output_;
196 };
197 
199  virtual ~ScanlineWriterConfig();
200 };
201 
202 }
203 
204 }
205 
206 #endif
static bool Continue(int percent, void *user_data)
Definition: countdown_timer.h:33
Class for managing image conversion timeouts.
Definition: image_util.h:158
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: message_handler.h:39
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27