OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef PNG_LOADER_H_ | |
6 #define PNG_LOADER_H_ | |
7 | |
8 #include <png.h> | |
9 #include <string.h> | |
10 | |
11 #include "boost/scoped_array.hpp" | |
12 #include "ppapi/cpp/size.h" | |
13 #include "threading/thread_condition.h" | |
14 #include "url_io/web_resource_loader.h" | |
15 | |
16 namespace flocking_geese { | |
17 | |
18 // Class that knows how to load and unpack a .png file into a buffer. The | |
19 // caller is expected to provide the memory for the final decompressed ARGB | |
20 // pixel buffer. | |
21 class PngLoader : public url_io::WebResourceLoader::Delegate { | |
22 public: | |
23 PngLoader() | |
24 : content_length_(0), | |
25 url_bytes_read_(0), | |
26 is_valid_(false), | |
27 png_data_pos_(0), | |
28 png_ptr_(NULL), | |
29 png_info_ptr_(NULL) {} | |
30 ~PngLoader(); | |
31 | |
32 // WebResourceLoader::Delegate interface. | |
33 virtual void OnLoaderReceivedResponseInfo(url_io::WebResourceLoader* loader); | |
34 virtual void OnLoaderReceivedData(url_io::WebResourceLoader* loader); | |
35 virtual void OnLoaderCompletedDownload(url_io::WebResourceLoader* loader); | |
36 virtual void OnLoaderError(int32_t error, url_io::WebResourceLoader* loader); | |
37 virtual void OnLoaderDone(url_io::WebResourceLoader* loader); | |
38 | |
39 // Fill in the given buffer with the decompressed image. |pixel_buffer| is | |
40 // assumed to point to enough memory to hold all the pixel data. If there | |
41 // isn't enough memory, there will be unpredictable results. | |
42 void FillPixelBuffer(uint32_t* pixel_buffer); | |
43 | |
44 // Return |true| if the PNG data was sucessfully downloaded. | |
45 bool is_valid() const { | |
46 return is_valid_; | |
47 } | |
48 | |
49 const uint8_t* png_data() const { | |
50 return png_data_.get(); | |
51 } | |
52 | |
53 // The size in pixels of the final, decompressed pixel buffer. | |
54 const pp::Size& png_image_size() const { | |
55 return png_image_size_; | |
56 } | |
57 | |
58 private: | |
59 // Support routines for using libpng. | |
60 | |
61 // Wrapper function given to libpng. | |
62 friend void ReadDataFromInputStream(png_structp png_ptr, | |
63 png_bytep out_bytes, | |
64 png_size_t byte_count); | |
65 | |
66 // Copy the next |byte_count| bytes from the PNG buffer into the supplied | |
67 // buffer. This advances the internal stream pointer by |byte_count|. | |
68 // Returns the actual number of bytes read. This method is passed into the | |
69 // libpng API. | |
70 size_t ReadPngData(uint8_t* buffer, const size_t byte_count); | |
71 | |
72 // Premultiply all pixels in a scanline by their alpha. | |
73 void PreMultiplyAlpha(uint32_t* scanline, int32_t line_width) ; | |
74 | |
75 // Delete all internal PNG sturctures and invlaidate this object. | |
76 void ReleaseAndInvalidate(); | |
77 | |
78 int32_t content_length_; // The total content length. | |
79 int32_t url_bytes_read_; // The number of bytes read so far. | |
80 bool is_valid_; | |
81 boost::scoped_array<uint8_t> png_data_; | |
82 size_t png_data_pos_; | |
83 png_structp png_ptr_; // The main PNG structure maintained by libpng. | |
84 png_infop png_info_ptr_; | |
85 pp::Size png_image_size_; | |
86 }; | |
87 | |
88 } // namespace flocking_geese | |
89 | |
90 #endif // PNG_LOADER_H_ | |
OLD | NEW |