Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <fstream>
- #include <cmath>
- #include "functions.h"
- using namespace std;
- Pixel** createImage(int width, int height) {
- cout << "Start createImage... " << endl;
- // Create a one dimensional array on the heap of pointers to Pixels
- // that has width elements (i.e. the number of columns)
- Pixel** image = new Pixel*[width];
- bool fail = false;
- for (int i=0; i < width; ++i) { // loop through each column
- // assign that column to a one dimensional array on the heap of Pixels
- // that has height elements (i.e. the number of rows)
- image[i] = new Pixel[height];
- if (image[i] == nullptr) { // failed to allocate
- fail = true;
- }
- }
- if (fail) { // if any allocation fails, clean up and avoid memory leak
- // deallocate any arrays created in for loop
- for (int i=0; i < width; ++i) {
- delete [] image[i]; // deleting nullptr is not a problem
- }
- delete [] image; // dlete array of pointers
- return nullptr;
- }
- // initialize cells
- //cout << "Initializing cells..." << endl;
- for (int row=0; row<height; ++row) {
- for (int col=0; col<width; ++col) {
- //cout << "(" << col << ", " << row << ")" << endl;
- image[col][row] = { 0, 0, 0 };
- }
- }
- cout << "End createImage... " << endl;
- return image;
- }
- void deleteImage(Pixel** image, int width) {
- cout << "Start deleteImage..." << endl;
- // avoid memory leak by deleting the array
- for (int i=0; i<width; ++i) {
- delete [] image[i]; // delete each individual array placed on the heap
- }
- delete [] image;
- image = nullptr;
- }
- int* createSeam(int length) {
- int* arr = new int[length];
- for(int i = 0; i < length; i++) {
- arr[i] = 0;
- }
- return arr;
- return nullptr;
- }
- void deleteSeam(int* seam) {
- delete [] seam;
- }
- bool loadImage(string filename, Pixel** image, int width, int height) {
- ifstream ifs(filename);
- if(!ifs.is_open()) {
- cout << "Error: failed to open input file - " << filename;
- return false;
- }
- char type[3];
- ifs >> type;
- if((toupper(type[0] != 'P')) || (type[1] != '3')) {
- cout << "Error: type is " << type << " instead of P3";
- return false;
- }
- int w =0;
- int h =0;
- ifs >> w >> h;
- if(!ifs.good()) {
- cout << "Error: read non-integer value" << endl;
- return false;
- }
- if(w != width) {
- cout <<"Error: input width (" << width << ") does not match value in file (" << w << ")" << endl;
- return false;
- }
- if(h != height) {
- cout << "Error: input height (" << height << ") does not match value in file ("<< h << ")" << endl;
- return false;
- }
- int colorMax = 0;
- ifs >> colorMax;
- if(colorMax != 255) {
- cout << "Error: file is not using RGB color values."<<endl;
- return false;
- }
- int fr;
- int fg;
- int fb;
- for(int i = 0; i < height; i++) {
- for(int j = 0; j < width; j++ ) {
- if(ifs.eof()) {
- cout << "Error: not enough color values" << endl;
- return false;
- }
- ifs >> fr;
- if(ifs.eof()) {
- cout << "Error: not enough color values" << endl;
- return false;
- }
- if(ifs.fail()) {
- cout << "Error: read non-integer value" << endl;
- return false;
- }
- image[j][i].r = fr;
- ifs >> fg;
- if(ifs.eof()) {
- cout << "Error: not enough color values" << endl;
- return false;
- }
- if(ifs.fail()) {
- cout << "Error: read non-integer value" << endl;
- return false;
- }
- image[j][i].g = fg;
- ifs >> fb;
- if(!ifs.eof() && !ifs.good()) {
- cout << "Error: read non-integer value" << endl;
- return false;
- }
- if(ifs.eof()) {
- cout << "Error: not enough color values" << endl;
- }
- image[j][i].b = fb;
- if(fb<0 || fb>255){
- cout << "Error: invalid color value "<< fb << endl;
- return false;
- }
- if(fg<0 || fg>255){
- cout << "Error: invalid color value "<< fg << endl;
- return false;
- }
- if(fr<0 || fr>255){
- cout << "Error: invalid color value "<< fr << endl;
- return false;
- }
- }
- }
- string check;
- if(!ifs.eof()) {
- ifs >> check;
- if(check != " ") {
- cout << "Error: too many color values";
- }
- if(ifs.eof()) {
- return true;
- }
- }
- ifs.close();
- return true;
- }
- bool outputImage(string filename, Pixel** image, int width, int height) {
- ofstream ofs(filename);
- if(!ofs.is_open()) {
- cout << "Error: failed to open output file - " << filename;
- return false;
- }
- ofs << 'P' << '3' << "\n";
- ofs << width << " " << height << "\n";
- ofs << 255 << "\n";
- for(int col = 0; col < width; col++) {
- for(int row = 0; row < height; row++) {
- ofs <<image[row][col].r << "\n" << image[row][col].g << "\n" << image[row][col].b << "\n";
- }
- ofs << "\n";
- }
- ofs.close();
- return true;
- }
- int energy(Pixel** image, int x, int y, int width, int height) {
- if((y>0 && x>0) && (x<width-1 && y<height-1)){
- //2, 1
- int rx = image[x+1][y].r - image[x-1][y].r;
- int gx = image[x+1][y].g - image[x-1][y].g;
- int bx = image[x+1][y].b - image[x-1][y].b;
- int ry = image[x][y-1].r - image[x][y+1].r;
- int gy = image[x][y-1].g - image[x][y+1].g;
- int by = image[x][y-1].b - image[x][y+1].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x==width-1 && y==0) {
- int rx = image[0][0].r - image[x-1][y].r;
- int gx = image[0][0].g - image[x-1][y].g;
- int bx = image[0][0].b - image[x-1][y].b;
- int ry = image[x][height-1].r - image[x][y+1].r;
- int gy = image[x][height-1].g - image[x][y+1].g;
- int by = image[x][height-1].b - image[x][y+1].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x==0 && y==0){
- //0,0
- int rx = image[1][0].r - image[width-1][0].r;
- int gx = image[1][0].g - image[width-1][0].g;
- int bx = image[1][0].b - image[width-1][0].b;
- int ry = image[0][height-1].r - image[0][1].r;
- int gy = image[0][height-1].g - image[0][1].g;
- int by = image[0][height-1].b - image[0][1].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x==width-1 && y==height-1){
- int rx = image[0][y].r - image[x-1][y].r;
- int gx = image[0][y].g - image[x-1][y].g;
- int bx = image[0][y].b - image[x-1][y].b;
- int ry = image[x][y-1].r - image[width-1][0].r;
- int gy = image[x][y-1].g - image[width-1][0].g;
- int by = image[x][y-1].b - image[width-1][0].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x==0 && y==height-1){
- int rx = image[1][y].r - image[width-1][y].r;
- int gx = image[1][y].g - image[width-1][y].g;
- int bx = image[1][y].b - image[width-1][y].b;
- int ry = image[x][y-1].r - image[0][0].r;
- int gy = image[x][y-1].g - image[0][0].g;
- int by = image[x][y-1].b - image[0][0].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x!=0 && y==0){
- int rx = image[x+1][y].r - image[x-1][y].r;
- int gx = image[x+1][y].g - image[x-1][y].g;
- int bx = image[x+1][y].b - image[x-1][y].b;
- int ry = image[x][height-1].r - image[x][y+1].r;
- int gy = image[x][height-1].g - image[x][y+1].g;
- int by = image[x][height-1].b - image[x][y+1].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x==0 && y!=0){
- int rx = image[x+1][y].r - image[width-1][y].r;
- int gx = image[x+1][y].g - image[width-1][y].g;
- int bx = image[x+1][y].b - image[width-1][y].b;
- int ry = image[x][y-1].r - image[x][y+1].r;
- int by = image[x][y-1].b - image[x][y+1].b;
- int gy = image[x][y-1].g - image[x][y+1].g;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(x==width-1){
- int rx = image[0][y].r - image[x-1][y].r;
- int gx = image[0][y].g - image[x-1][y].g;
- int bx = image[0][y].b - image[x-1][y].b;
- int ry = image[x][y-1].r - image[x][y+1].r;
- int gy = image[x][y-1].g - image[x][y+1].g;
- int by = image[x][y-1].b - image[x][y+1].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- if(y==height-1){
- int rx = image[x+1][y].r - image[x-1][y].r;
- int gx = image[x+1][y].g - image[x-1][y].g;
- int bx = image[x+1][y].b - image[x-1][y].b;
- int ry = image[x][y-1].r - image[x][0].r;
- int gy = image[x][y-1].g - image[x][0].g;
- int by = image[x][y-1].b - image[x][0].b;
- int totX = pow(rx,2) + pow(gx,2) + pow(bx,2);
- int totY = pow(ry,2) + pow(gy,2) + pow(by,2);
- int energy = totX + totY;
- return energy;
- }
- return 0;
- }
- int loadVerticalSeam(Pixel** image, int start_col, int width, int height, int* seam) {
- seam[0] = start_col;
- int check = start_col;
- int totalE = energy(image, start_col, 0, width, height);
- for(int i=0; i<height-1; i++){
- if(check==0){
- int e1 = energy(image, check, i+1, width, height);
- int e3 = energy(image, check+1, i+1, width, height);
- if(e1 <= e3){
- seam[i+1] = check;
- totalE += e1;
- continue;
- }
- else if(e3 < e1){
- seam[i+1] = check+1;
- totalE += e3;
- check += 1;
- continue;
- }
- }
- if(check==width-1){
- int e1 = energy(image, check, i+1, width, height);
- int e2 = energy(image, check-1, i+1, width, height);
- if(e1 <= e2){
- seam[i+1] = check;
- totalE += e1;
- continue;
- }
- else if(e2 < e1){
- seam[i+1] = check-1;
- totalE += e2;
- check -= 1;
- continue;
- }
- }
- int e1 = energy(image, check, i+1, width, height);
- int e2 = energy(image, check-1, i+1, width, height);
- int e3 = energy(image, check+1, i+1, width, height);
- if(e1 < e2 && e1 < e3){
- seam[i+1] = check;
- totalE += e1;
- continue;
- }
- int smallest = min(min(e2, e1), e3);
- totalE += smallest;
- if(smallest == e1){
- seam[i+1] = check;
- continue;
- }
- if(smallest == e3){
- seam[i+1] = check+1;
- check += 1;
- continue;
- }
- if(smallest == e2){
- seam[i+1] = check-1;
- check -= 1;
- continue;
- }
- }
- return totalE;
- }
- int* findMinVerticalSeam(Pixel** image, int width, int height) {
- int* seam = createSeam(height);
- int small = loadVerticalSeam(image,0,width,height,seam);
- for(int i = 0; i < width; i++) {
- int* seam2 = new int[height];
- int c = loadVerticalSeam(image,i,width,height,seam2);
- if(c < small) {
- small = loadVerticalSeam(image, i , width, height, seam);
- }
- delete [] seam2;
- //seam2 = nullptr;
- }
- return seam;
- }
- void removeVerticalSeam(Pixel** image, int width, int height, int* verticalSeam) {
- for(int i = 0; i < height; i++){
- for(int j = verticalSeam[i]; j < (width-1); j++) {
- image[j][i] = image[j+1][i];
- }
- }
- }
- int loadHorizontalSeam(Pixel** image, int start_row, int width, int height, int* seam) {
- return 0;
- }
- int* findMinHorizontalSeam(Pixel** image, int width, int height) {
- return nullptr;
- }
- void removeHorizontalSeam(Pixel** image, int width, int height, int* horizontalSeam) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement