1 /* 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. 28 class 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 width()35 png_uint_32 width() const { 36 return width_; 37 } 38 height()39 png_uint_32 height() const { 40 return height_; 41 } 42 channels()43 png_byte channels() const { 44 return channels_; 45 } 46 bit_depth()47 int bit_depth() const { 48 return bit_depth_; 49 } 50 color_type()51 int color_type() const { 52 return color_type_; 53 } 54 png_ptr()55 png_structp png_ptr() const { 56 return png_ptr_; 57 } 58 info_ptr()59 png_infop info_ptr() const { 60 return info_ptr_; 61 } 62 error_code()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 }; 85 86 // Overrides the default resource dir, for testing purpose. 87 void res_set_resource_dir(const std::string&); 88