-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.cpp
100 lines (87 loc) · 2.08 KB
/
image.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
#include "image.h"
#include "imageset.h"
#include <fstream>
#include <sstream>
#include <string>
#include <QDebug>
#include <iostream>
using namespace std;
Image::Image(ImageSet *parent, QString path) : m_parentSet(parent), m_path(path)
{
loaded = false;
}
int Image::load()
{
int row = 0, col = 0, numrows = 0, numcols = 0, pixelmax = 0;
if(m_path.isEmpty() || m_parentSet->getPath().isEmpty())
{
//qDebug()<<"Calea catre fisier este goala!";
return -1; //fail
}
QString fullPath = m_parentSet->getPath().append("/").append(m_path);
ifstream infile(fullPath.toUtf8());
stringstream ss;
string inputLine = "";
getline(infile,inputLine);
if(inputLine.compare("P5") != 0)
{
//qDebug()<<"Fisier in format gresit." << fullPath;
return -1; //fail
}
ss << infile.rdbuf();
ss >> numcols >> numrows >> pixelmax;
if(!numcols && !numrows)
{
//qDebug()<<"Coloane/randuri nu pot fi citite.";
return -1;
}
// am inceput incarcarea efectiva
emit startedLoading();
unsigned char *array = new unsigned char[numrows*numcols];
for(int i = 0; i < numrows*numcols; i++)
ss >> array[i];
infile.close();
m_height = numrows;
m_width = numcols;
m_array = new float[m_width*m_height];
for(int i=0;i<m_width*m_height;i++)
{
m_array[i] = array[i];
}
//am terminat incarcarea
loaded = true;
emit finishedLoading();
//qDebug()<<"Am terminat incarcarea.";
return 0; //success
}
QImage Image::toQImage()
{
//qDebug()<<"Redau imaginea.";
/*
for(int row = 0; row < m_height; row++)
{
for(int col = 0; col<m_width; col++)
{
cout<<QString::number((int)m_array[row*col]).toStdString()<<' ';
}
cout<<"\n";
}*/
return QImage(m_parentSet->getPath().append("/").append(m_path));
}
Image::~Image()
{
delete(m_array);
delete(m_parentSet);
}
int Image::width()
{
return m_width;
}
int Image::height()
{
return m_height;
}
float *Image::getArray()
{
return m_array;
}