Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image_frame_interface.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 
21 
22 #ifndef PAGESPEED_KERNEL_IMAGE_IMAGE_FRAME_INTERFACE_H_
23 #define PAGESPEED_KERNEL_IMAGE_IMAGE_FRAME_INTERFACE_H_
24 
25 #include <cstddef>
30 
31 namespace net_instaweb {
32 class MessageHandler;
33 }
34 
35 namespace pagespeed {
36 
37 namespace image_compression {
38 
40 
41 struct FrameSpec;
42 
43 struct ImageSpec {
44  ImageSpec();
45  void Reset();
46 
47  size_px width;
48  size_px height;
49  size_px num_frames;
50 
53  unsigned int loop_count;
54 
55  PixelRgbaChannels bg_color;
56  bool use_bg_color;
57 
61 
64  size_px TruncateXIndex(size_px x) const;
65 
68  size_px TruncateYIndex(size_px y) const;
69 
71  bool CanContainFrame(const FrameSpec& frame_spec) const;
72 
73  GoogleString ToString() const;
74  bool Equals(const ImageSpec& other) const;
75 };
76 
79 struct FrameSpec {
81  DISPOSAL_UNKNOWN = 0,
82  DISPOSAL_NONE,
83  DISPOSAL_BACKGROUND,
84 
88  };
89 
90  FrameSpec();
91  void Reset();
92 
93  size_px width;
94  size_px height;
95  size_px top;
96  size_px left;
97  PixelFormat pixel_format;
98  size_t duration_ms;
99  DisposalMethod disposal;
100 
106 
107  GoogleString ToString() const;
108  bool Equals(const FrameSpec& other) const;
109 };
110 
111 #if defined(IF_OK_RUN)
112 #error "Preprocessor macro collision."
113 #endif
114 
120 #define IF_OK_RUN(_STATUS, _CALL) \
121  if (!_STATUS->Success()) { \
122  return false; \
123  } \
124  *_STATUS = _CALL; \
125  return _STATUS->Success();
126 
147  public:
148  explicit MultipleFrameReader(MessageHandler* handler);
149  virtual ~MultipleFrameReader();
150 
152  virtual ScanlineStatus Reset() = 0;
153 
158  virtual ScanlineStatus Initialize() = 0;
159 
163  ScanlineStatus Initialize(const void* image_buffer, size_t buffer_length) {
164  image_buffer_ = image_buffer;
165  buffer_length_ = buffer_length;
166  return Initialize();
167  }
168 
173  virtual bool HasMoreFrames() const = 0;
174 
177  virtual bool HasMoreScanlines() const = 0;
178 
181  virtual ScanlineStatus PrepareNextFrame() = 0;
182 
188  virtual ScanlineStatus ReadNextScanline(const void** out_scanline_bytes) = 0;
189 
195  virtual ScanlineStatus GetFrameSpec(FrameSpec* frame_spec) const = 0;
196 
199  virtual ScanlineStatus GetImageSpec(ImageSpec* image_spec) const = 0;
200 
204  return message_handler_;
205  }
206 
207  virtual ScanlineStatus set_quirks_mode(QuirksMode quirks_mode) {
208  quirks_mode_ = quirks_mode;
209  return ScanlineStatus(SCANLINE_STATUS_SUCCESS);
210  }
211 
212  virtual QuirksMode quirks_mode() const {
213  return quirks_mode_;
214  }
215 
220 
221  bool Reset(ScanlineStatus* status) {
222  IF_OK_RUN(status, Reset());
223  }
224  bool Initialize(const void* image_buffer, size_t buffer_length,
225  ScanlineStatus* status) {
226  IF_OK_RUN(status, Initialize(image_buffer, buffer_length));
227  }
228  bool Initialize(ScanlineStatus* status) {
229  IF_OK_RUN(status, Initialize());
230  }
231  bool PrepareNextFrame(ScanlineStatus* status) {
232  IF_OK_RUN(status, PrepareNextFrame());
233  }
234  bool ReadNextScanline(const void** out_scanline_bytes,
235  ScanlineStatus* status) {
236  IF_OK_RUN(status, ReadNextScanline(out_scanline_bytes));
237  }
238  bool GetFrameSpec(FrameSpec* frame_spec,
239  ScanlineStatus* status) {
240  IF_OK_RUN(status, GetFrameSpec(frame_spec));
241  }
242  bool GetImageSpec(ImageSpec* image_spec,
243  ScanlineStatus* status) {
244  IF_OK_RUN(status, GetImageSpec(image_spec));
245  }
246 
247  bool set_quirks_mode(QuirksMode quirks_mode,
248  ScanlineStatus* status) {
249  IF_OK_RUN(status, set_quirks_mode(quirks_mode));
250  }
251 
252  protected:
253  const void* image_buffer_;
254  size_t buffer_length_;
255 
256  private:
258  MessageHandler* message_handler_;
259 
261  QuirksMode quirks_mode_;
262 
263 
264 };
265 
266 
280  public:
281  explicit MultipleFrameWriter(MessageHandler* handler);
282  virtual ~MultipleFrameWriter();
283 
288  virtual ScanlineStatus Initialize(const void* config,
289  GoogleString* out) = 0;
290 
293  virtual ScanlineStatus PrepareImage(const ImageSpec* image_spec) = 0;
294 
298  virtual ScanlineStatus PrepareNextFrame(const FrameSpec* frame_spec) = 0;
299 
305  virtual ScanlineStatus WriteNextScanline(const void *scanline_bytes) = 0;
306 
308  virtual ScanlineStatus FinalizeWrite() = 0;
309 
313  return message_handler_;
314  }
315 
320 
321  bool Initialize(const void* config, GoogleString* out,
322  ScanlineStatus* status) {
323  IF_OK_RUN(status, Initialize(config, out));
324  }
325  bool PrepareImage(const ImageSpec* image_spec, ScanlineStatus* status) {
326  IF_OK_RUN(status, PrepareImage(image_spec));
327  }
328  bool PrepareNextFrame(const FrameSpec* frame_spec, ScanlineStatus* status) {
329  IF_OK_RUN(status, PrepareNextFrame(frame_spec));
330  }
331  bool WriteNextScanline(const void* scanline_bytes, ScanlineStatus* status) {
332  IF_OK_RUN(status, WriteNextScanline(scanline_bytes));
333  }
334  bool FinalizeWrite(ScanlineStatus* status) {
335  IF_OK_RUN(status, FinalizeWrite());
336  }
337 
338  private:
340  MessageHandler* message_handler_;
341 
342 
343 };
344 
345 #undef IF_OK_RUN
346 
347 }
348 
349 }
350 
351 #endif
ScanlineStatus Initialize(const void *image_buffer, size_t buffer_length)
Definition: image_frame_interface.h:163
virtual ScanlineStatus GetImageSpec(ImageSpec *image_spec) const =0
virtual ScanlineStatus GetFrameSpec(FrameSpec *frame_spec) const =0
MessageHandler * message_handler() const
Definition: image_frame_interface.h:312
Definition: scanline_status.h:93
virtual ScanlineStatus FinalizeWrite()=0
Finalizes the image once all the frames have been written.
virtual ScanlineStatus Initialize(const void *config, GoogleString *out)=0
Definition: image_frame_interface.h:146
MessageHandler * message_handler() const
Definition: image_frame_interface.h:203
unsigned int loop_count
Definition: image_frame_interface.h:53
Definition: image_frame_interface.h:279
virtual ScanlineStatus ReadNextScanline(const void **out_scanline_bytes)=0
size_px TruncateXIndex(size_px x) const
virtual ScanlineStatus Reset()=0
Resets the MultipleFrameReader to its initial state.
bool Reset(ScanlineStatus *status)
Definition: image_frame_interface.h:221
bool hint_progressive
Definition: image_frame_interface.h:105
#define IF_OK_RUN(_STATUS, _CALL)
Definition: image_frame_interface.h:120
virtual ScanlineStatus WriteNextScanline(const void *scanline_bytes)=0
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: image_frame_interface.h:87
Definition: image_frame_interface.h:43
Definition: image_frame_interface.h:79
bool CanContainFrame(const FrameSpec &frame_spec) const
Returns true iff frame_spec fits entirely within this ImageSpec.
bool image_size_adjusted
Definition: image_frame_interface.h:60
size_px TruncateYIndex(size_px y) const
Definition: message_handler.h:39
DisposalMethod
Definition: image_frame_interface.h:80
virtual ScanlineStatus PrepareImage(const ImageSpec *image_spec)=0
virtual ScanlineStatus PrepareNextFrame(const FrameSpec *frame_spec)=0
bool Initialize(const void *config, GoogleString *out, ScanlineStatus *status)
Definition: image_frame_interface.h:321