source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/SURF-V1.0.8_Original/imload.cpp @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 2.9 KB
Line 
1/*
2 * Speeded-Up Robust Features (SURF)
3 * http://people.ee.ethz.ch/~surf
4 *
5 * Authors: Herbert Bay, Andreas Ess, Geert Willems
6 * Windows port by Stefan Saur
7 *
8 * Copyright (2006): ETH Zurich, Switzerland
9 * Katholieke Universiteit Leuven, Belgium
10 * All rights reserved.
11 *
12 * For details, see the paper:
13 * Herbert Bay,  Tinne Tuytelaars,  Luc Van Gool,
14 *  "SURF: Speeded Up Robust Features"
15 * Proceedings of the ninth European Conference on Computer Vision, May 2006
16 *
17 * Permission to use, copy, modify, and distribute this software and
18 * its documentation for educational, research, and non-commercial
19 * purposes, without fee and without a signed licensing agreement, is
20 * hereby granted, provided that the above copyright notice and this
21 * paragraph appear in all copies modifications, and distributions.
22 *
23 * Any commercial use or any redistribution of this software
24 * requires a license from one of the above mentioned establishments.
25 *
26 * For further details, contact Andreas Ess (aess@vision.ee.ethz.ch).
27 */
28
29#include <iostream>
30#include <fstream>
31
32#include "imload.h"
33#include "image.h"
34
35namespace surf {
36
37#define MAX(x,y)  (((x) > (y)) ? (x) : (y))
38#define MIN(x,y)  (((x) < (y)) ? (x) : (y))
39#define ABS(x)    (((x) > 0) ? (x) : (-(x)))
40
41using namespace std;
42
43void ignoreComments(ifstream& imfile) {
44  char c;
45  do {
46    imfile >> c;
47  } while (c == ' ');
48  imfile.putback(c);
49
50  imfile >> c;
51  while (c == '#') {
52    imfile.ignore(256, '\n');
53    imfile >> c;
54  }
55  imfile.putback(c);
56}
57
58Image *ImLoad::readImage(const char *fn){
59  ifstream imfile(fn, ios::binary);
60  if (!imfile.is_open()){ 
61    cerr << "Sorry, could not open: " << fn << endl;
62    exit(0);
63  }
64
65  // Reading file header
66  char P;
67  char num;
68  imfile >> P >> num; 
69  ignoreComments(imfile);
70
71  // Read image dimensions and extremum value
72  int width, height, extr;
73  imfile >> width;
74  ignoreComments(imfile);
75  imfile >> height;
76  ignoreComments(imfile);
77  imfile >> extr;
78
79  // Check whether the file is OK
80  if (P != 'P' || num != '5' || 
81      width <= 0 || height <= 0 || 
82      extr > 255) {
83    cerr << "Input image has to be PGM format" << endl;
84    exit(0);
85  }
86
87  // Get the image intensities and normalise to 0 - 1.
88  imfile.get();
89  Image *im = new Image(width, height);
90  for (int y = 0; y < height; y++)
91    for (int x = 0; x < width; x++)
92      im->setPix(x, y, ((double) imfile.get()) / extr);
93  return im;
94}
95
96void ImLoad::saveImage(const char *fn, Image *im) {
97  ofstream imfile(fn, ios::binary);
98  if (!imfile.is_open()) {
99    cerr << "Sorry, could not open: " << fn << endl;
100    exit(0);
101  }
102  imfile << "P5" << endl;
103  imfile << im->getWidth() << " " << im->getHeight() << " 255" << endl;
104  for (int y = 0; y < im->getHeight(); y++)
105    for (int x = 0; x < im->getWidth(); x++)
106      imfile.put((unsigned char)(im->getPix(x, y) * 255));
107}
108
109}
Note: See TracBrowser for help on using the repository browser.