71 static void pnm_read(std::ifstream &file, std::string& buf) {
123 std::ifstream file(name, std::ios::in | std::ios::binary);
125 if (strncmp(buf.c_str(),
"P5", 2))
129 int width = std::stoi(buf);
131 int height = std::stoi(buf);
134 if (std::stoi(buf) > UCHAR_MAX)
139 file.read((
char *)
imPtr(im, 0, 0), width * height *
sizeof(
uchar));
182 std::ifstream file(name, std::ios::in | std::ios::binary);
184 if (strncmp(buf.c_str(),
"P6", 2))
188 int width = std::stoi(buf);
190 int height = std::stoi(buf);
193 if (std::stoi(buf) > UCHAR_MAX)
201 float r_channel_weight = 1.0f / 3.0f;
202 float g_channel_weight = 1.0f / 3.0f;
203 float b_channel_weight = 1.0f / 3.0f;
206 r_channel_weight = 0.299f;
207 g_channel_weight = 0.114f;
208 b_channel_weight = 0.587f;
211 for (
int i=0; i<width; i++)
213 for (
int j=0; j<height; j++)
215 imRef(imGrayScale, i, j) = floor(((
float)
imRef(im, i, j).r)*r_channel_weight + ((
float)
imRef(im, i, j).g)*g_channel_weight + ((
float)
imRef(im, i, j).b)*b_channel_weight + 0.5f);
234 char pgmExtension[] =
"pgm";
235 char ppmExtension[] =
"ppm";
236 char* file_path_image_copy =
new char[strlen(name) + 1]{};
237 std::copy(name, name + strlen(name), file_path_image_copy);
243 token = strtok_s(file_path_image_copy,
".", &next_token);
245 char* token = strtok(file_path_image_copy,
".");
247 char* lastToken =
new char[strlen(token) + 1]{};
248 std::copy(token, token + strlen(token), lastToken);
249 while( token != NULL )
252 lastToken =
new char[strlen(token) + 1]{};
253 std::copy(token, token + strlen(token), lastToken);
255 token = strtok_s(NULL,
".", &next_token);
257 token = strtok(NULL,
".");
262 if (strcmp(lastToken, pgmExtension) == 0)
264 delete [] file_path_image_copy;
267 return loadPGM(name);
269 else if (strcmp(lastToken, ppmExtension) == 0)
271 delete [] file_path_image_copy;
274 return loadPPMAndConvertToGrayScale(name);
278 delete [] file_path_image_copy;
279 std::cout <<
"CPU ERROR, IMAGE FILE " << name <<
" NOT SUPPORTED\n";
289 std::ifstream file(name, std::ios::in | std::ios::binary);
291 if (strncmp(buf.c_str(),
"VLIB", 9))
295 int width = std::stoi(buf);
297 int height = std::stoi(buf);
301 file.read((
char *)
imPtr((*im), 0, 0), width * height *
sizeof(T));
306 int width = im->
width();
307 int height = im->
height();
308 std::ofstream file(name, std::ios::out | std::ios::binary);
310 file <<
"VLIB\n" << width <<
" " << height <<
"\n";
311 file.write((
char *)
imPtr(im, 0, 0), width * height *
sizeof(T));
Class and structs in single-thread CPU bp implementation by Pedro Felzenwalb available at https://cs....
void load_image(bp_single_thread_imp::image< T > **im, const char *name)
void save_image(bp_single_thread_imp::image< T > *im, const char *name)
const bool kUseWeightedRGBToGrayscaleConversion_PNMFILE