00001
00008 #include <QMessageBox>
00009 #include <string>
00010 #include <iostream>
00011 #include <fstream>
00012 #include "image.h"
00013
00014 Image::Image()
00015 {
00016 }
00017
00018 Image::~Image()
00019 {
00020 ResetMem();
00021 }
00022
00023 void Image::ResetMem()
00024 {
00025 for (unsigned int i = 0; i < m_data.size(); ++i) {
00026 m_data.at(i).clear();
00027 }
00028 m_data.clear();
00029 }
00030
00031 bool Image::ReadFile(const std::string& filename)
00032 {
00033
00034
00035
00036 std::ifstream f;
00037 f.open( filename.c_str() );
00038
00039 if (!f.is_open()) {
00040 f.close();
00041 return false;
00042 }
00043
00044 ResetMem();
00067 std::string word;
00068 f >> word;
00069 if (word != "IMAGE") {
00070
00071 }
00072 f >> word;
00073 if (word != "WIDTH"){
00074
00075 }
00076 f >> m_nX;
00077
00078 f >> word;
00079 if (word != "HEIGHT") {
00080
00081 }
00082 f >> m_nY;
00083
00084 f >> word;
00085 if (word != "PIXELS") {
00086
00087 }
00088
00089
00090 std::vector <int> line;
00091 int elem = 0;
00092
00093
00094 for (int y = 0; y < m_nY; y++) {
00095 line.clear();
00096 for (int x = 0; x < m_nX; x++) {
00097 f >> elem;
00098 line.push_back(elem);
00099 }
00100 m_data.push_back(line);
00101 }
00102 word = "";
00103 f >> word;
00104 if (word != "IMAGE_END") {
00105
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 AdjustPaletteColors();
00123
00124 f.close();
00125 return true;
00126 }
00127
00128 void Image::AdjustPaletteColors() {
00129 int val;
00130
00131 m_min = GetData(0, 0);
00132 m_max = GetData(0, 0);
00133
00134 for (unsigned int y = 0; y < GetSizeY(); y++) {
00135 for (unsigned int x = 0; x < GetSizeX(); x++) {
00136 if ((val = GetData(x, y)) < m_min) {
00137 m_min = val;
00138 } else if (val > m_max) {
00139 m_max = val;
00140 }
00141 }
00142 }
00143 }
00144
00145 int Image::GetData(int x, int y) const {
00146 if (IsPointInside(x, y)) {
00147 return m_data.at(y).at(x);
00148 } else {
00149 return 0;
00150 }
00151 }
00152
00153 bool Image::IsPointInside(unsigned int x,unsigned int y) const {
00154 return ( (x < GetSizeX()) && (y < GetSizeY()) );
00155 }
00156
00157 unsigned int Image::GetSizeX() const
00158 {
00159 return m_data.size();
00160 }
00161
00162 unsigned int Image::GetSizeY() const
00163 {
00164 if(m_data.size() != 0) {
00165 return m_data.at(0).size();
00166 } else {
00167 return 0;
00168 }
00169 }
00170
00171 int Image::GetMin() const {
00172 return m_min;
00173 }
00174
00175 int Image::GetMax() const {
00176 return m_max;
00177 }