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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
//main
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <fstream>
#include "ppmformat.cpp"
using namespace std;
int main(){
string file = "Image01.ppm";
ReadPPM(file.c_str());
}
//image.cpp
#ifndef _IMAGE
#define _IMAGE
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include "Color.h"
#include "Image.h"
using namespace std;
class Image{
public:
enum channel_t { RED = 0, GREEN, BLUE };
protected:
imaging::component_t * buffer;
public:
unsigned int width, height;
const unsigned int getWidth() const { return width; }
const unsigned int getHeight() const { return height; }
imaging::component_t * getRawDataPtr(){
return buffer;
}
imaging::Color getPixel(unsigned int x, unsigned int y) const{
imaging::Color color;
if ((x <= width) && (y <= height) && (x*y >= 0)){
color[RED] = x*y;
color[GREEN] = x*y + GREEN;
color[BLUE] = x*y + BLUE;
}
return color;
}
void setPixel(unsigned int x, unsigned int y, imaging::Color & value){
if ((x <= width) && (y <= height) && (x*y >= 0)){
buffer[x * y + RED] = value[RED];
buffer[x * y + GREEN] = value[GREEN];
buffer[x * y + BLUE] = value[BLUE];
}
else{
cout << "Width or heigth out of bounds!" << endl;
}
}
void setData(const imaging::component_t * & data_ptr){
int size = 3 * width * height;
buffer = new imaging::component_t[size];
for (int i = 0; i < size; i++){
buffer[i] = data_ptr[i];
}
}
void resize(unsigned int new_width, unsigned int new_height){
/*to be filled!!!*/
}
Image() : width(0), height(0), buffer(0){}
Image(unsigned int _width, unsigned int _height) : width(0), height(0), buffer(0){
width = _width;
height = _height;
}
Image(unsigned int _width, unsigned int _height, const imaging::component_t * data_ptr) : width(0), height(0), buffer(0){
width = _width;
height = _height;
setData(data_ptr);
}
Image(const Image &src) :width(src.width), height(src.height), buffer(src.buffer){}
~Image(){
delete[] buffer;
}
Image & operator = (const Image & right){
Image left = Image(right);
return left;
}
};
#endif
//image.h
//------------------------------------------------------------
//
// C++ course assignment code
//
// G. Papaioannou, 2015
//
//
#ifndef _IMAGE
#define _IMAGE
#include "Color.h"
// We put every class or function associated with the image storage, compression and manipulation
// in the "imaging" namespace
namespace imaging
{
//------------------------------------ class Image ------------------------------------------------
//
// It is the class that represents a generic data container for an image. It holds the actual buffer
// of the pixel values and provides methods for accessing them either as individual pixels or as
// a memory block. The Image class alone does not provide any functionality for loading and storing an image, as
// it is the result or input to such a procedure.
//
// The internal buffer of an image object stores the actual bytes (data) of the color image as
// a contiguous sequence of RGB triplets. Hence, the size of the buffer variable holding these data is
// 3 X width X height bytes.
class Image
{
public:
enum channel_t {RED=0,GREEN, BLUE}; // now you can use the names RED, GREEN, BLUE instead of 0,1,2
// to index individual channels
protected:
component_t * buffer; // Holds the image data
public:
unsigned int width, height; // width and height of the image (in pixels)
// metric accessors
const unsigned int getWidth() const {return width;} // returns the width of the image
const unsigned int getHeight() const {return height;} // returns the height of the image
// data accessors
component_t * getRawDataPtr(); // Obtain a pointer to the internal data
// This is NOT a copy of the internal image data, but rather
// a pointer to the internally allocated space, so DO NOT
// attempt to delete the pointer.
Color getPixel(unsigned int x, unsigned int y) const; // get the color of the image at location (x,y)
// Do any necessary bound checking. Also take into account
// the "interleaved" flag to fetch the appropriate data
// Return a black (0,0,0) color in case of an out-of-bounds
// x,y pair
// data mutators
void setPixel(unsigned int x, unsigned int y, Color & value);
// Set the RGB values for an (x,y) pixel. Do all
// necessary bound checks and respect the "interleaved"
// flag when updating our data.
void setData(const component_t * & data_ptr); // Copy the data from data_ptr to the internal buffer.
// The function ASSUMES a proper size for the incomming data array.
void resize(unsigned int new_width, unsigned int new_height);
// Change the internal data storage size to the new ones.
// If the one or both of the dimensions are smaller, clip the
// by discarding the remaining pixels in the rows / columns outside
// the margins. If the new dimensions are larger, pad the old pixels
// with zero values (black color).
// constructors and destructor
Image(); // default: zero dimensions, nullptr for the buffer.
Image(unsigned int width, unsigned int height);
Image(unsigned int width, unsigned int height, const component_t * data_ptr);
Image(const Image &src);
~Image();
Image & operator = (const Image & right);
};
} //namespace imaging
#endif
//ppmformat.h
//------------------------------------------------------------
//
// C++ course assignment code
//
// G. Papaioannou, 2015
//
//
//-------------------------------------------------------------
#ifndef _PPM
#define _PPM
#include "Image.h"
namespace imaging
{
Image * ReadPPM(const char * filename);
} //namespace imaging
#endif
//ppmformat.cpp
#ifndef _PPM
#define _PPM
#include <iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include "Image.h"
#include "ppmformat.h"
using namespace std;
static imaging::Image *ReadPPM(const char *filename)
{
imaging::Image * img;
//imaging::Image * img = new imaging::Img();
//img = new imaging::Image();
std::ifstream ifs;
std::string s2;
std::string header;
unsigned int w, h, rgb_comp_color;
//anoigma arxeiou
ifs.open(filename, std::ios::binary); // need to spec. binary mode for Windows users
if (ifs.fail()) { throw("Can't open input file"); }
ifs >> header;
if (strcmp(header.c_str(), "P6") != 0) throw("Can't read input file");//to header einai to p6
ifs >> w >> h >> rgb_comp_color;
cout<<w<<endl;
cout<<h<<endl;
cout<<rgb_comp_color<<endl;
cout<<img->getWidth()<<endl;
cout<<img->getHeight()<<endl;
//alloc memory form image
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
imaging::component_t *buffer=new imaging::component_t[3*h*w];
imaging::component_t getRawDataPtr();
cout<<"edw"<<ifs.get()<<endl;
ifs.read ((char *)buffer,3*h*w);
ifs.close();
buffer = new float[1000];
//img=new imaging::Image(w,h,buffer);
return img;
}
#endif
| |