From 3d0cd1d0360ab3e536be87798415fef60222aac4 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 13 Apr 2018 13:41:58 -0700 Subject: Expose PngHandler via resources.h. As a private header for testing purpose. PngHandler additionally loads a given filename if the one with '/res/images' prefix is not available. It also provides color_type/bit_depth that are parsed from the PNG file. This allows reusing the same code for the ResourcesTest (renamed from ResourceTest). Test: Run recovery_manual_test on marlin. Change-Id: I3f939d79a1cb1b83a899847dbe2d51bde15d16d8 --- minui/include/private/resources.h | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 minui/include/private/resources.h (limited to 'minui/include') diff --git a/minui/include/private/resources.h b/minui/include/private/resources.h new file mode 100644 index 000000000..2a83a1028 --- /dev/null +++ b/minui/include/private/resources.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include +#include + +#include + +// This class handles the PNG file parsing. It also holds the ownership of the PNG pointer and the +// opened file pointer. Both will be destroyed / closed when this object goes out of scope. +class PngHandler { + public: + // Constructs an instance by loading the PNG file from '/res/images/.png', or ''. + PngHandler(const std::string& name); + + ~PngHandler(); + + png_uint_32 width() const { + return width_; + } + + png_uint_32 height() const { + return height_; + } + + png_byte channels() const { + return channels_; + } + + int bit_depth() const { + return bit_depth_; + } + + int color_type() const { + return color_type_; + } + + png_structp png_ptr() const { + return png_ptr_; + } + + png_infop info_ptr() const { + return info_ptr_; + } + + int error_code() const { + return error_code_; + }; + + operator bool() const { + return error_code_ == 0; + } + + private: + png_structp png_ptr_{ nullptr }; + png_infop info_ptr_{ nullptr }; + png_uint_32 width_; + png_uint_32 height_; + png_byte channels_; + int bit_depth_; + int color_type_; + + // The |error_code_| is set to a negative value if an error occurs when opening the png file. + int error_code_{ 0 }; + // After initialization, we'll keep the file pointer open before destruction of PngHandler. + std::unique_ptr png_fp_{ nullptr, fclose }; +}; -- cgit v1.2.3