Advertisement
Guest User

fozzie.cpp

a guest
Nov 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <fstream>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. bool debug = false;
  12. float dint = 0; //variable for debugging purposes
  13. void isdebug(char** argv) {
  14.     int i = 0;
  15.     while (argv[i] != NULL) {
  16.         if (argv[i][0] == '-') break; //search command line for '-' flag indicator
  17.         i++;
  18.     }
  19.     if (argv[i] != NULL && (argv[i][1] == 'd' || argv[i][1] == 'D')) debug = true;
  20.     if (debug) cout << "Running debug..." << endl;
  21. }
  22.  
  23. class myImage { //normally in .h file with methods in Image.cpp but keeping it all together for now
  24. private:
  25.     typedef struct RGBQuad { //1 color index = 4 bytes
  26.         unsigned char blue;
  27.         unsigned char green;
  28.         unsigned char red;
  29.         unsigned char reserve;
  30.     }RGBQuad;
  31.  
  32.     int width;
  33.     int height;
  34.     int psize; //size of pixel array
  35.     int mostleft;
  36.     int mostright;
  37.     int mostup;
  38.     int mostdown;
  39.     unsigned char* imageArray; //1-pixel-per-byte for 8-bit bmp images
  40.     unsigned char background; //store index to background color
  41.     RGBQuad* colors; //color index array
  42.  
  43. public:
  44.  
  45.     myImage() { //not really necessary for this exercise but good practice
  46.          width = 0;
  47.          height= 0;
  48.          mostleft = 0;
  49.          mostright = 0;
  50.          mostup = 0;
  51.          mostdown = 0;
  52.          unsigned char* imageArray = nullptr;
  53.     }
  54.  
  55.     int readBMP(char* filename) {
  56.         std::basic_ifstream<unsigned char>* imFile;
  57.         imFile = new basic_ifstream<unsigned char>;
  58.         imFile->open("fozzie-in.bmp", ios::in | ios::binary); //temporary hardcode for name
  59.         if (!imFile->good()) { cout << "Failed to open file!" << endl;  return -1; }
  60.         if (debug) cout << "File opened succesfully." << endl;
  61.  
  62.         unsigned char fhead[14]; //14 byte file header
  63.         imFile->read(fhead, 14);
  64.  
  65.         if (debug) cout << "Read file header." << endl;
  66.  
  67.         int hsize = fhead[10] - 14; //get image header size (imagestart - fileheadersize)
  68.         unsigned char bhead[124]; //at most 124 bytes needed
  69.         imFile->read(bhead, hsize); //read image header (for this project: 40 bytes)
  70.  
  71.         if (debug) cout << "Read image header." << endl;
  72.  
  73.         // extract information from header
  74.         width = bhead[4]; cout << "w:" << width << endl;
  75.         height = bhead[8]; cout << "h:" << height << endl;
  76.         //in this case only working with 8-bit images
  77.         int bits = bhead[14];
  78.         if (bits != 8) { cout << "Not an 8 bit image!" << endl; return -1; }
  79.         int imsize = bhead[20]; cout << "s:" << imsize << endl; //not sure if I need this but might as well take it
  80.  
  81.         if (debug) cout << "Processing color table..." << endl;
  82.         //start processing color table
  83.         int colorsize = bhead[32]; //0 if 2^n
  84.         int i; unsigned char quadbuffer[4];
  85.         if (colorsize == 0) { //default to 2^n, 256 in this case
  86.             colors = new RGBQuad[256]; //*4 bytes per index = 1024 bytes to read
  87.             for (i = 0; i < 256; i ++) {
  88.                 imFile->read(quadbuffer, 4); //read next 4-byte color index
  89.                 if (quadbuffer[0] == 0 || quadbuffer[0] == NULL)
  90.                     i = i; //after reading 44 bytes everything is NULL????
  91.                 colors[i].blue = quadbuffer[0];
  92.                 colors[i].green = quadbuffer[1];
  93.                 colors[i].red = quadbuffer[2];
  94.                 colors[i].reserve = quadbuffer[3]; //should always be NULL/0!
  95.             }
  96.         }
  97.         else {
  98.             colors = new RGBQuad[colorsize];
  99.             //not bothering with this right now since I know the image is 256 colors
  100.         }
  101.  
  102.         //Read remaining pixel data
  103.         psize = width * height; //width/height are divisible by 4 so no row padding
  104.         imageArray = new unsigned char[psize]; //read into single array, use % for rows
  105.         imFile->read(imageArray, psize);
  106.     //  for (i = 0; i < psize; i++)
  107.     //      cout << colors[imageArray[i]];
  108.         return 0;
  109.     }
  110.  
  111.  
  112.     /*
  113.     void findBorder() {
  114.         background = colors[imageArray[0]];
  115.         cout << "Background color: " << background << endl;
  116.         //imageArray[height][width]
  117.         int i, j;
  118.         for (i = 0; i <= psize; i++) {
  119.                 //cout << "Checking: (" << i << "," << j << ") Color: " << imageArray[i] << endl;
  120.                 if (colors[imageArray[i]] != background) {
  121.                     //cout << "Found color: " << imageArray[i] << endl;
  122.                     if (j < mostleft) {
  123.                 //      cout << "new right" << endl;
  124.                         mostright = j;
  125.                     }
  126.                     if (j > mostright) {
  127.                 //      cout << "new left" << endl;
  128.                         mostleft = j;
  129.                     }
  130.                     if (i < mostup) {
  131.                 //      cout << "new up" << endl;
  132.                         mostup = i;
  133.                     }
  134.                     if (i > mostdown) {
  135.                 //      cout << "new down" << endl;
  136.                         mostdown = i;
  137.                     }
  138.             }
  139.         }
  140.  
  141.         cout << "mostleft: " << mostleft << endl;
  142.         cout << "mostright" << mostright << endl;
  143.         cout << "mostup" << mostup << endl;
  144.         cout << "mostdown" << mostdown << endl;
  145.     }
  146.     */
  147.     /*
  148.     void printdata() {
  149.         int i, j;
  150.         for (i = 0; i <= psize; i++) {
  151.                 //if (imageArray[i][j] != background)
  152.                 cout << colors[imageArray[i]] << " ";
  153.                 //{cout << i << "," << j << ": " << imageArray[i][j] << endl;}
  154.         }
  155.             cout << endl;
  156.     }
  157.     */
  158. };
  159.  
  160. int main(int argc, char** argv) {
  161.     isdebug(argv);
  162.     char filename[100];
  163.  
  164.     fgets(filename, 100, stdin);
  165.     myImage image1;
  166.     int cando = image1.readBMP(filename);
  167.     if (cando == -1) { cout << "Operation failed! Ending program..." << endl; exit(-1); }
  168.     //image1.printdata();
  169.     //image1.findBorder();
  170.  
  171.     //CALL DESTRUCTOR HERE
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement