Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- #include <iostream>
- using namespace std;
- bool debug = false;
- float dint = 0; //variable for debugging purposes
- void isdebug(char** argv) {
- int i = 0;
- while (argv[i] != NULL) {
- if (argv[i][0] == '-') break; //search command line for '-' flag indicator
- i++;
- }
- if (argv[i] != NULL && (argv[i][1] == 'd' || argv[i][1] == 'D')) debug = true;
- if (debug) cout << "Running debug..." << endl;
- }
- class myImage { //normally in .h file with methods in Image.cpp but keeping it all together for now
- private:
- typedef struct RGBQuad { //1 color index = 4 bytes
- unsigned char blue;
- unsigned char green;
- unsigned char red;
- unsigned char reserve;
- }RGBQuad;
- int width;
- int height;
- int psize; //size of pixel array
- int mostleft;
- int mostright;
- int mostup;
- int mostdown;
- unsigned char* imageArray; //1-pixel-per-byte for 8-bit bmp images
- unsigned char background; //store index to background color
- RGBQuad* colors; //color index array
- public:
- myImage() { //not really necessary for this exercise but good practice
- width = 0;
- height= 0;
- mostleft = 0;
- mostright = 0;
- mostup = 0;
- mostdown = 0;
- unsigned char* imageArray = nullptr;
- }
- int readBMP(char* filename) {
- std::basic_ifstream<unsigned char>* imFile;
- imFile = new basic_ifstream<unsigned char>;
- imFile->open("fozzie-in.bmp", ios::in | ios::binary); //temporary hardcode for name
- if (!imFile->good()) { cout << "Failed to open file!" << endl; return -1; }
- if (debug) cout << "File opened succesfully." << endl;
- unsigned char fhead[14]; //14 byte file header
- imFile->read(fhead, 14);
- if (debug) cout << "Read file header." << endl;
- int hsize = fhead[10] - 14; //get image header size (imagestart - fileheadersize)
- unsigned char bhead[124]; //at most 124 bytes needed
- imFile->read(bhead, hsize); //read image header (for this project: 40 bytes)
- if (debug) cout << "Read image header." << endl;
- // extract information from header
- width = bhead[4]; cout << "w:" << width << endl;
- height = bhead[8]; cout << "h:" << height << endl;
- //in this case only working with 8-bit images
- int bits = bhead[14];
- if (bits != 8) { cout << "Not an 8 bit image!" << endl; return -1; }
- int imsize = bhead[20]; cout << "s:" << imsize << endl; //not sure if I need this but might as well take it
- if (debug) cout << "Processing color table..." << endl;
- //start processing color table
- int colorsize = bhead[32]; //0 if 2^n
- int i; unsigned char quadbuffer[4];
- if (colorsize == 0) { //default to 2^n, 256 in this case
- colors = new RGBQuad[256]; //*4 bytes per index = 1024 bytes to read
- for (i = 0; i < 256; i ++) {
- imFile->read(quadbuffer, 4); //read next 4-byte color index
- if (quadbuffer[0] == 0 || quadbuffer[0] == NULL)
- i = i; //after reading 44 bytes everything is NULL????
- colors[i].blue = quadbuffer[0];
- colors[i].green = quadbuffer[1];
- colors[i].red = quadbuffer[2];
- colors[i].reserve = quadbuffer[3]; //should always be NULL/0!
- }
- }
- else {
- colors = new RGBQuad[colorsize];
- //not bothering with this right now since I know the image is 256 colors
- }
- //Read remaining pixel data
- psize = width * height; //width/height are divisible by 4 so no row padding
- imageArray = new unsigned char[psize]; //read into single array, use % for rows
- imFile->read(imageArray, psize);
- // for (i = 0; i < psize; i++)
- // cout << colors[imageArray[i]];
- return 0;
- }
- /*
- void findBorder() {
- background = colors[imageArray[0]];
- cout << "Background color: " << background << endl;
- //imageArray[height][width]
- int i, j;
- for (i = 0; i <= psize; i++) {
- //cout << "Checking: (" << i << "," << j << ") Color: " << imageArray[i] << endl;
- if (colors[imageArray[i]] != background) {
- //cout << "Found color: " << imageArray[i] << endl;
- if (j < mostleft) {
- // cout << "new right" << endl;
- mostright = j;
- }
- if (j > mostright) {
- // cout << "new left" << endl;
- mostleft = j;
- }
- if (i < mostup) {
- // cout << "new up" << endl;
- mostup = i;
- }
- if (i > mostdown) {
- // cout << "new down" << endl;
- mostdown = i;
- }
- }
- }
- cout << "mostleft: " << mostleft << endl;
- cout << "mostright" << mostright << endl;
- cout << "mostup" << mostup << endl;
- cout << "mostdown" << mostdown << endl;
- }
- */
- /*
- void printdata() {
- int i, j;
- for (i = 0; i <= psize; i++) {
- //if (imageArray[i][j] != background)
- cout << colors[imageArray[i]] << " ";
- //{cout << i << "," << j << ": " << imageArray[i][j] << endl;}
- }
- cout << endl;
- }
- */
- };
- int main(int argc, char** argv) {
- isdebug(argv);
- char filename[100];
- fgets(filename, 100, stdin);
- myImage image1;
- int cando = image1.readBMP(filename);
- if (cando == -1) { cout << "Operation failed! Ending program..." << endl; exit(-1); }
- //image1.printdata();
- //image1.findBorder();
- //CALL DESTRUCTOR HERE
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement