SHARE
TWEET

Untitled

a guest May 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cmath>
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. #include "Image.h"
  7.  
  8. Image::Image()
  9. {
  10.     width=0;
  11.     height=0;
  12.     colorMode=GRAY_8BPP;
  13.     pixelData=0;
  14. }
  15.  
  16. Image::Image(unsigned int size_x, unsigned int size_y, int setColorMode)
  17. {
  18.     width = size_x;
  19.     height = size_y;
  20.     pixelData = 0;
  21.     colorMode = setColorMode;
  22. }
  23.  
  24. Image::Image(const Image& right)
  25. {
  26.     copyAll(right);
  27. }
  28.  
  29. Image& Image::operator=(const Image& right)
  30. {
  31.     if (this != &right) {
  32.         freePixelData();
  33.         copyAll(right);
  34.     }
  35.     return *this;
  36. }
  37.  
  38. void Image::copyAll(const Image& right)
  39. {
  40.     width = right.width;
  41.     height = right.height;
  42.     colorMode = right.colorMode;
  43.     pixelData = NULL;
  44.     createImage(width, height);
  45.    
  46.     for (int x = 0; x < width; x++) {
  47.         for (int y = 0; y < height; y++) {
  48.             pixelData[x][y] = right.pixelData[x][y];
  49.         }
  50.     }
  51. }
  52.  
  53. void Image::freePixelData()
  54. {
  55.     if (pixelData == NULL) // if already freed
  56.         return;
  57.     for (int x = 0; x < width; x++) {
  58.         delete [] pixelData[x];
  59.     }
  60.     width = 0;
  61.     height = 0;
  62.     delete [] pixelData;
  63.     pixelData = NULL;
  64. }
  65.  
  66. void Image::createImage(int width_x, int height_y)
  67. {
  68.     width = width_x;
  69.     height = height_y;
  70.     if (pixelData!=NULL)
  71.         freePixelData();
  72.     if (width <= 0 || height <= 0) {
  73.         return;
  74.     }
  75.  
  76.     pixelData = new Color* [width];  // array of Pixel*
  77.     for (int x = 0; x < width; x++) {
  78.         pixelData[x] = new Color [height];  // this is 2nd dimension of pixelData
  79.     }
  80. }
  81.  
  82.  
  83. Image::~Image()
  84. {
  85.     freePixelData();
  86. }
  87.  
  88.  
  89.  
  90. int Image::loadBMP(const std::string &filename)
  91. {
  92.     ifstream file;
  93.     unsigned long filesize, data_offset, i, offset_row;
  94.     unsigned long fill_null, count_row;
  95.     unsigned char buffer[4];
  96.     int x, y;
  97.  
  98.     file.open(filename.c_str() , ios_base::in | ios_base::binary);
  99.     if(file.fail()){
  100.         cerr << "[Image::loadBmp] Error opening " << filename << endl;
  101.         return -1;
  102.     }
  103.     file.seekg(0, ios_base::beg);
  104.     file.read((char*)buffer, 2);
  105.     if(file.fail()){
  106.         cerr << "[Image::loadBmp] Error reading " << filename << endl;
  107.         return -1;
  108.     }
  109.  
  110.     if(buffer[0] != 'B' || buffer[1] != 'M'){
  111.         cerr << "[Image::loadBmp] File is not a bitmap file" << endl;
  112.         return -1;
  113.     }
  114.  
  115.     freePixelData(); // clear out array in case something is there already
  116.  
  117.     // file length : offset 2
  118.     file.read((char *)&filesize, 4);
  119.     // reserved: offset 6
  120.     file.read((char *)&i, 4);
  121.     // data offset: offset 10
  122.     file.read((char *)&data_offset, 4);
  123.    
  124.     // Bitmap info header
  125.     bmp_info_header info;
  126.     file.read((char *)&info, BMP_INFO_SIZE);
  127.  
  128.     if(info.compression != 0){
  129.         cerr << "[Image::loadBmp] Compressed bitmap is not supported" << endl;
  130.         return -1;
  131.     }
  132.     width = info.width;
  133.     height = info.height;
  134.  
  135.         createImage(width, height);
  136.  
  137.     if(info.bpp == 24){
  138.         fill_null = 3 - (width * 3 - 1) % 4;
  139.         count_row = width * 3 + fill_null;
  140.         for (y = height-1; y >= 0; y--) {
  141.             file.seekg(data_offset, ios_base::beg);
  142.             offset_row = width * y;
  143.             for(x = 0; x < width; x++){
  144.                 file.read((char*)buffer, 3);
  145.                 pixelData[x][y] = Color((((int)buffer[0]) << 16) | (((int)buffer[1]) << 8) | (int)buffer[2]);
  146.                 //pixelData[x][y] = Color((((int)buffer[0]) ) | (((int)buffer[1]) << 8) | (int)buffer[2] << 16);
  147.             }
  148.             data_offset += count_row;
  149.         }
  150.         colorMode = COLOR_24BPP;
  151.         cout << width << "*" << height << " 24bpp true color" << endl;
  152.     } else if(info.bpp == 8){
  153.         fill_null = 3 - (width - 1) % 4;
  154.         count_row = width + fill_null;
  155.         for(y = 0; y < height; y ++){
  156.             file.seekg(data_offset, ios_base::beg);
  157.             offset_row = width * y;
  158.             for(x = 0; x < width; x++){
  159.                 file.read((char *)buffer, 1);
  160.                 pixelData[x][y] = Color((int)buffer[0]);
  161.             }
  162.             data_offset += count_row;
  163.         }
  164.         colorMode = GRAY_8BPP;
  165.         cout << width << "*" << height << " 8bpp gray scale" << endl;
  166.     }
  167.     if(file.fail()){
  168.         cerr << "[Image::loadBmp] Error reading file." << endl;
  169.         return -1;
  170.     }
  171.     return 0;
  172. }
  173. int Image::saveBMP(std::string &filename) const
  174. {
  175.     ofstream file;
  176.  
  177.     unsigned char header[] = "BM", buffer[4];
  178.     unsigned long filesize, data_offset;
  179.     unsigned int i, offset, offset_row, fill_dword, count_row;
  180.     int x, y;
  181.  
  182.     bmp_info_header info;
  183.     info.width = width;
  184.     info.height = height;
  185.  
  186.     if(colorMode == GRAY_8BPP){
  187.         info.bpp = 8;
  188.         data_offset = 14 + BMP_INFO_SIZE + 256*4;
  189.         fill_dword = 3 - (width - 1) % 4;
  190.         count_row = width + fill_dword;
  191.     } else if(colorMode == COLOR_24BPP){
  192.         info.bpp = 24;
  193.         data_offset = 14 + BMP_INFO_SIZE;
  194.         fill_dword = 3 - (width * 3 - 1) % 4;
  195.         count_row = width * 3 + fill_dword;
  196.     } else {
  197.         cerr << "[Image::saveBmp] Color mode " << colorMode << " is not supported" << endl;
  198.         return -1;
  199.     }
  200.     info.image_size = count_row * height;
  201.     filesize = data_offset + info.image_size;
  202.  
  203.     file.open(filename.c_str() , ios::out | ios::binary | ios::trunc);
  204.     if(file.fail()){
  205.         cerr << "[Image::saveBmp] Error opening " << filename << endl;
  206.         return -1;
  207.     }
  208.     file.write((char*)header, 2);
  209.     file.write((char *)&filesize, 4);
  210.     i = 0;
  211.     file.write((char *)&i, 4);
  212.     file.write((char *)&data_offset, 4);
  213.     file.write((char *)&info, BMP_INFO_SIZE);
  214.     if(colorMode == GRAY_8BPP){
  215.         unsigned char pallet[256 * 4];
  216.         for(i = 0;i < 256;i++){
  217.             offset = i << 2;
  218.             pallet[offset] = (unsigned char)i; // Blue
  219.             pallet[offset + 1] = (unsigned char)i; // Green
  220.             pallet[offset + 2] = (unsigned char)i; // Red
  221.             pallet[offset + 3] = 0;
  222.         }
  223.         file.write((char *)pallet, 256 * 4);
  224.     }
  225.     i = 0;
  226.     for(y = height-1; y >=0; y--){
  227.         offset_row = width * y;
  228.         for(x = 0; x < width; x++){
  229.             if(colorMode == GRAY_8BPP){
  230.                 buffer[0]= (unsigned char)pixelData[x][y].getRed();
  231.                 file.write((char *)buffer, 1);
  232.             } else if(colorMode == COLOR_24BPP){
  233.                 buffer[0] = (pixelData[x][y].RGB_to_int() & 0xff0000)>> 16; // Blue
  234.                 buffer[1] = (pixelData[x][y].RGB_to_int() & 0xff00) >> 8; // Green
  235.                 buffer[2] = (pixelData[x][y].RGB_to_int() & 0xff); // Red
  236.                
  237.                 file.write((char *)buffer, 3);
  238.             }
  239.         }
  240.         if(fill_dword)
  241.             file.write((char *)&i, fill_dword);
  242.     }
  243.     if(file.fail()){
  244.         cerr << "[Image::saveBmp] Error writing " << filename << endl;
  245.         return -1;
  246.     }
  247.     return 0;
  248. }
  249.  
  250.  
  251. bmp_info_header::bmp_info_header(void)
  252. {
  253.     info_size = 40;
  254.     width = 0;
  255.     height = 0;
  256.     plane = 1;
  257.     bpp = 8;
  258.     compression = 0;
  259.     image_size = 0;
  260.     ppm_x = 0;
  261.     ppm_y = 0;
  262.     colors = 0;
  263.     important_colors = 0;
  264. }
  265.  
  266. void Image::initialize(unsigned int size_x, unsigned int size_y, int setColorMode)
  267. {
  268.     width=size_x;
  269.     height=size_y;
  270.     pixelData[width][height];
  271.     colorMode=setColorMode;
  272. }
  273.  
  274. bool Image::empty()
  275. {
  276.     if(pixelData==NULL)
  277.         return true;
  278.     else
  279.         return false;
  280.    
  281. }
  282.  
  283. void Image::setColorMode(int newColorMode)
  284. {
  285.     colorMode=newColorMode;
  286. }
  287.  
  288. int Image::getColorMode( ) const
  289. {
  290.     return colorMode;
  291. }
  292.  
  293. Color Image::getColor(int x, int y) const
  294. {
  295.     Color newTemp;
  296.     newTemp.setRed(pixelData[x][y].getRed());
  297.     newTemp.setGreen(pixelData[x][y].getGreen());
  298.     newTemp.setBlue(pixelData[x][y].getBlue());
  299.     return newTemp;
  300.  
  301. }
  302.  
  303. void Image::setColor(int x, int y, Color color)
  304. {
  305.     color=pixelData[x][y];
  306. }
  307.  
  308. int Image::getHeight() const
  309. {
  310.     return height;
  311. }
  312.  
  313. int Image::getWidth() const
  314. {
  315.     return width;
  316. }
  317.  
  318. void Image::scaleColor(double redAmount, double greenAmount, double blueAmount)
  319. {
  320.     pixelData[x][y].red*=redAmount;
  321.     pixelData[x][y].green*=greenAmount;
  322.     pixelData[x][y].blue*=blueAmount;
  323. }
  324.  
  325. void flipLeftRight()
  326. {
  327.     Image temp;
  328.     for (int x = 0; x < width; x++) {
  329.         for (int y = 0; y < height; y++) {
  330.             pixelData[x][y] = temp.pixelData[x][y]-pixelData[x][y];
  331.         }
  332.     }
  333. }
RAW Paste Data
Top