summaryrefslogtreecommitdiffstats
path: root/minui/include/private/resources.h
blob: 2a83a1028f23533f266a9dba109d53e52d01a1d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 <stdio.h>

#include <memory>
#include <string>

#include <png.h>

// 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/<name>.png', or '<name>'.
  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<FILE, decltype(&fclose)> png_fp_{ nullptr, fclose };
};