Optimized Belief Propagation (CPU and GPU)
BpImage.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
27 #ifndef BPIMAGE_H_
28 #define BPIMAGE_H_
29 
30 #include <memory>
31 #include <algorithm>
32 #include <string>
33 #include <iostream>
34 #include <fstream>
35 #include <sstream>
36 #include <cmath>
37 #include <array>
38 #include <string_view>
39 #include <type_traits>
40 #include <ranges>
41 
42 namespace beliefprop {
43  enum class ImageType { kPgmImage, kPpmImage };
45  constexpr std::string_view kPGMExt{"pgm"};
46  constexpr std::string_view kPPMExt{"ppm"};
47 };
48 
54 template <class T>
55 requires std::is_arithmetic_v<T>
56 class BpImage {
57 public:
58  BpImage() : width_height_{0, 0} {}
59 
60  explicit BpImage(const std::array<unsigned int, 2>& width_height) :
61  width_height_{width_height},
62  pixels_(std::make_unique<T[]>(width_height_[0]*width_height_[1]))
63  {}
64 
65  explicit BpImage(
66  const std::array<unsigned int, 2>& width_height,
67  const T* input_pixel_vals) :
68  width_height_{width_height},
69  pixels_(std::make_unique<T[]>(width_height_[0] * width_height_[1]))
70  {
71  std::ranges::copy(
72  input_pixel_vals,
73  input_pixel_vals + (TotalPixels()),
74  pixels_.get());
75  }
76 
77  explicit BpImage(const std::string& file_name) {
78  LoadImageAsGrayScale(file_name);
79  }
80 
81  const std::unique_ptr<T[]>& UniquePtrToPixelData() const {
82  return pixels_;
83  }
84 
85  T* PointerToPixelsStart() const {
86  return pixels_.get();
87  }
88 
89  T PixelAtPoint(const std::array<unsigned int, 2>& point_xy) const {
90  return PixelAtPoint(point_xy[1]*width_height_[0] + point_xy[0]);
91  }
92 
93  T PixelAtPoint(unsigned int i) const {
94  return (pixels_.get())[i];
95  }
96 
97  void SetPixelAtPoint(const std::array<unsigned int, 2>& point_xy, T val) {
98  SetPixelAtPoint((point_xy[1]*width_height_[0] + point_xy[0]), val);
99  }
100 
101  void SetPixelAtPoint(unsigned int i, T val) {
102  (pixels_.get())[i] = val;
103  }
104 
105  unsigned int Width() const { return width_height_[0]; }
106  unsigned int Height() const { return width_height_[1]; }
107 
108  void SaveImageAsPgm(const std::string& filename) const {
109  std::ofstream file(filename, std::ios::out | std::ios::binary);
110  file << "P5\n" << width_height_[0] << " " << width_height_[1] << "\n"
111  << UCHAR_MAX << "\n";
112  file.write((char*)(pixels_.get()), TotalPixels() * sizeof(char));
113  file.close();
114  }
115 
116 protected:
117  std::array<unsigned int, 2> width_height_;
118  std::unique_ptr<T[]> pixels_;
119 
120  void LoadImageAsGrayScale(const std::string& file_path_image);
121 
122  void pnm_read(std::ifstream &file, std::string& buf) const;
123 
125  const std::string& file_name,
126  beliefprop::ImageType image_type,
127  bool weighted_rgb_conversion = true) const;
128 
129  //currently assuming single channel
130  inline unsigned int TotalPixels() const {
131  return (width_height_[0] * width_height_[1]);
132  }
133 };
134 
135 #endif /* BPIMAGE_H_ */
Class to define images that are used in bp processing.
Definition: BpImage.h:56
unsigned int Height() const
Definition: BpImage.h:106
std::array< unsigned int, 2 > width_height_
Definition: BpImage.h:117
std::unique_ptr< T[]> pixels_
Definition: BpImage.h:118
unsigned int TotalPixels() const
Definition: BpImage.h:130
BpImage(const std::array< unsigned int, 2 > &width_height)
Definition: BpImage.h:60
BpImage(const std::string &file_name)
Definition: BpImage.h:77
T PixelAtPoint(unsigned int i) const
Definition: BpImage.h:93
void SetPixelAtPoint(unsigned int i, T val)
Definition: BpImage.h:101
const std::unique_ptr< T[]> & UniquePtrToPixelData() const
Definition: BpImage.h:81
unsigned int Width() const
Definition: BpImage.h:105
void SaveImageAsPgm(const std::string &filename) const
Definition: BpImage.h:108
T PixelAtPoint(const std::array< unsigned int, 2 > &point_xy) const
Definition: BpImage.h:89
void LoadImageAsGrayScale(const std::string &file_path_image)
Definition: BpImage.cpp:31
void pnm_read(std::ifstream &file, std::string &buf) const
Definition: BpImage.cpp:72
BpImage()
Definition: BpImage.h:58
BpImage(const std::array< unsigned int, 2 > &width_height, const T *input_pixel_vals)
Definition: BpImage.h:65
void SetPixelAtPoint(const std::array< unsigned int, 2 > &point_xy, T val)
Definition: BpImage.h:97
T * PointerToPixelsStart() const
Definition: BpImage.h:85
BpImage< unsigned char > ImageRead(const std::string &file_name, beliefprop::ImageType image_type, bool weighted_rgb_conversion=true) const
Definition: BpImage.cpp:90
Namespace for enums, constants, structures, and functions specific to belief propagation processing.
constexpr std::string_view kPGMExt
Definition: BpImage.h:45
constexpr bool kUseWeightedRGBToGrayscaleConversion
Definition: BpImage.h:44
constexpr std::string_view kPPMExt
Definition: BpImage.h:46