blob: 047ebe2e3abe28acdd192a12f6fec86a6f256c38 [file] [log] [blame]
Tao Bao3d0cd1d2018-04-13 13:41:58 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
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#pragma once
18
19#include <stdio.h>
20
21#include <memory>
22#include <string>
23
24#include <png.h>
25
26// This class handles the PNG file parsing. It also holds the ownership of the PNG pointer and the
27// opened file pointer. Both will be destroyed / closed when this object goes out of scope.
28class PngHandler {
29 public:
30 // Constructs an instance by loading the PNG file from '/res/images/<name>.png', or '<name>'.
31 PngHandler(const std::string& name);
32
33 ~PngHandler();
34
35 png_uint_32 width() const {
36 return width_;
37 }
38
39 png_uint_32 height() const {
40 return height_;
41 }
42
43 png_byte channels() const {
44 return channels_;
45 }
46
47 int bit_depth() const {
48 return bit_depth_;
49 }
50
51 int color_type() const {
52 return color_type_;
53 }
54
55 png_structp png_ptr() const {
56 return png_ptr_;
57 }
58
59 png_infop info_ptr() const {
60 return info_ptr_;
61 }
62
63 int error_code() const {
64 return error_code_;
65 };
66
67 operator bool() const {
68 return error_code_ == 0;
69 }
70
71 private:
72 png_structp png_ptr_{ nullptr };
73 png_infop info_ptr_{ nullptr };
74 png_uint_32 width_;
75 png_uint_32 height_;
76 png_byte channels_;
77 int bit_depth_;
78 int color_type_;
79
80 // The |error_code_| is set to a negative value if an error occurs when opening the png file.
81 int error_code_{ 0 };
82 // After initialization, we'll keep the file pointer open before destruction of PngHandler.
83 std::unique_ptr<FILE, decltype(&fclose)> png_fp_{ nullptr, fclose };
84};
Tao Bao6cd81682018-05-03 21:53:11 -070085
86// Overrides the default resource dir, for testing purpose.
87void res_set_resource_dir(const std::string&);