-
Notifications
You must be signed in to change notification settings - Fork 37
/
imload.cpp
109 lines (95 loc) · 2.9 KB
/
imload.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Speeded-Up Robust Features (SURF)
* https://github.com/herbertbay/SURF
*
* Authors: Herbert Bay, Andreas Ess, Geert Willems
* Windows port by Stefan Saur
*
* Copyright (2006): ETH Zurich, Switzerland
* Katholieke Universiteit Leuven, Belgium
* All rights reserved.
*
* For details, see the paper:
* Herbert Bay, Tinne Tuytelaars, Luc Van Gool,
* "SURF: Speeded Up Robust Features"
* Proceedings of the ninth European Conference on Computer Vision, May 2006
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for educational, research, and non-commercial
* purposes, without fee and without a signed licensing agreement, is
* hereby granted, provided that the above copyright notice and this
* paragraph appear in all copies modifications, and distributions.
*
* Any commercial use or any redistribution of this software
* requires a license from one of the above mentioned establishments.
*
* For further details, contact Herbert Bay ([email protected]).
*/
#include <iostream>
#include <fstream>
#include "imload.h"
#include "image.h"
namespace surf {
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) (((x) > 0) ? (x) : (-(x)))
using namespace std;
void ignoreComments(ifstream& imfile) {
char c;
do {
imfile >> c;
} while (c == ' ');
imfile.putback(c);
imfile >> c;
while (c == '#') {
imfile.ignore(256, '\n');
imfile >> c;
}
imfile.putback(c);
}
Image *ImLoad::readImage(const char *fn){
ifstream imfile(fn, ios::binary);
if (!imfile.is_open()) {
cerr << "Sorry, could not open: " << fn << endl;
exit(0);
}
// Reading file header
char P;
char num;
imfile >> P >> num;
ignoreComments(imfile);
// Read image dimensions and extremum value
int width, height, extr;
imfile >> width;
ignoreComments(imfile);
imfile >> height;
ignoreComments(imfile);
imfile >> extr;
// Check whether the file is OK
if (P != 'P' || num != '5' ||
width <= 0 || height <= 0 ||
extr > 255) {
cerr << "Input image has to be PGM format" << endl;
exit(0);
}
// Get the image intensities and normalise to 0 - 1.
imfile.get();
Image *im = new Image(width, height);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
im->setPix(x, y, ((double) imfile.get()) / extr);
return im;
}
void ImLoad::saveImage(const char *fn, Image *im) {
ofstream imfile(fn, ios::binary);
if (!imfile.is_open()) {
cerr << "Sorry, could not open: " << fn << endl;
exit(0);
}
imfile << "P5" << endl;
imfile << im->getWidth() << " " << im->getHeight() << " 255" << endl;
for (int y = 0; y < im->getHeight(); y++)
for (int x = 0; x < im->getWidth(); x++)
imfile.put((unsigned char)(im->getPix(x, y) * 255));
}
}