#ifndef __MASC_BUFFER #define __MASC_BUFFER #include #include #include "mascFile.h" namespace ImageMasc { typedef union _mascColor { unsigned char color[4]; unsigned int value; } mascColor; typedef struct _screenPixel { int x,y; mascColor color; }screenPixel; template class buffer { public: std::vector > dvec; explicit buffer() { data_buffer = 0; } ~buffer() { if(data_buffer != 0) free(data_buffer); } bool createDataBuffer(int w, int h, int depth); void insertFile(std::string fname, ImageMasc::mascFile &f); void addImage(gdImagePtr img); void setPixel(int x, int y, unsigned int color); unsigned int getPixel(int x, int y); /* functions to manipulate multiple layers of buffered data */ void red_swap(); protected: char *data_buffer; // contains all data in the buffer int width, height; void concatToBuffer(char *buff); }; template bool buffer::createDataBuffer(int w, int h, int depth) { width = w, height = h; data_buffer = (char*) malloc ( depth*(w*h) ); if(!data_buffer) return false; return true; } template void buffer::setPixel(int x, int y, unsigned int color) { unsigned int *p = (unsigned int *)data_buffer; p += x+(y*width); *p = color; } template unsigned int buffer::getPixel(int x, int y) { unsigned int *p = (unsigned int *)data_buffer; p += x+(y*width); return *p; } template void buffer::insertFile(std::string fname, ImageMasc::mascFile &f) { if(f.mascOpen(fname.c_str())) { char *data = f.readall(); concatToBuffer(data); free(data); } } template void buffer::concatToBuffer(char *data) { int i,z; std::vector wrap; for(i = 0; i < width; i++) { for(z = 0; z < height; z++) { int rgb[4]; rgb[0] = *data; data++; rgb[1] = *data; data++; rgb[2] = *data; data += 2; mascColor col; col.color[0] = rgb[0]; col.color[1] = rgb[1]; col.color[2] = rgb[2]; col.color[3] = 0; screenPixel pix; pix.x = i, pix.y = z; pix.color.value = col.value; wrap.push_back(pix); } } dvec.push_back(wrap); std::cout << "successfully proccessed image\n"; } template void buffer::red_swap() { if(dvec.size() <= 1) { std::cout << "only one layer of data exists cannot continue\n"; } } template void buffer::addImage(gdImagePtr image) { int i,z; std::vector list; for(i = 0; i < image->sx; i++) { for(z = 0; z < image->sy; z++) { screenPixel scol; scol.color.value = gdImageGetPixel(image, i, z); scol.x = i, scol.y = z; list.push_back(scol); } } dvec.push_back(list); } } #endif