Чтение ppm-файла и покрытие в оттенках серого или инвертированное - PullRequest
0 голосов
/ 23 октября 2019

У меня не получается получить файл для чтения, я хочу, чтобы пользователь ввел имя файла, скажем «обои», а затем я преобразовал его в имя файла «wallpaper.ppm». Я хочу взять этот ввод, преобразовать числа в векторы, а затем либо преобразовать файл в оттенки серого, либо инвертировать его в зависимости от ввода пользователя. спасибо

// A5.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <array>
using namespace std;


int main()
{
    struct Pixel {
        int Red;
        int Green;
        int Blue;
    };
    vector <vector< Pixel> > image;
    string fileType;
    int height{}, width{}, maxColor{};
    string p3;
    cout << "Enter the name of the file:";
    cin >> fileType;
    string fileTypeActual = fileType + ".ppm";
    ifstream myImage(fileTypeActual);
    if (myImage.fail()) {
        cerr << "Fail";
        return 1;
    }
    while (!myImage.eof()) {
        string p3;
        myImage >> p3 >> width >> height >> maxColor;
        int i, j;
        for (i = 0; i < height; i++) {
            vector<Pixel>rowofPixels;
            for (j = 0; j < width; j++) {
                Pixel p;
                myImage >> p.Red;
                myImage >> p.Green;
                myImage >> p.Blue;
                rowofPixels.push_back(p);
            }
            image.push_back(rowofPixels);
        }
    };
        myImage.close();
        int userChoice;
        cout << "Would you like to invert the colors or convert to greyscale? Type 1 for greyscale and 2 for color inversion";
            cin >> userChoice;
            if (userChoice == 1) {
                //begin greyscale conversion
                string fileGray;
                fileGray = fileType + "_grayscale.ppm";
                ofstream imageOut(fileGray);
                if (imageOut.fail()) {
                    cerr << "Fail";
                    return 1;
                }

                struct greyPixel {
                    double redGrey;
                    double blueGrey;
                    double greenGrey;
                };
                vector <vector< greyPixel > > greyScale;
                //gray = 0.2989 * Red + 0.5870 * Green + 0.1140 * Blue
                for (int i = 0; i < height; i++) {
                    vector<greyPixel>rowofGrey;
                    for (int j = 0; j < width; j++) {
                        int pixelRed = ((image.at(i)).at(j)).Red;
                        int pixelBlue = ((image.at(i)).at(j)).Blue;
                        int pixelGreen = ((image.at(i)).at(j)).Green;
                        greyPixel g;
                        g.redGrey = 0.2989 * pixelRed + 0.5870 * pixelGreen + 0.1140 * pixelBlue;
                        g.blueGrey = 0.2989 * pixelRed + 0.5870 * pixelGreen + 0.1140 * pixelBlue;
                        g.greenGrey = 0.2989 * pixelRed + 0.5870 * pixelGreen + 0.1140 * pixelBlue;
                        rowofGrey.push_back(g);
                    }
                    greyScale.push_back(rowofGrey);
                }
                imageOut.close();
            }
            else if (userChoice == 2) {
                //begin color inverson 
                string fileInvert;
                fileInvert = fileType + "_inverted.ppm";
                ofstream imageInv(fileInvert);
                if (imageInv.fail()) {
                    cerr << "Fail";
                    return 1;
                }
                struct invertPixel {
                    int redInvert;
                    int blueInvert;
                    int greenInvert;
                };
                vector <vector< invertPixel > > invert;
                for (int i = 0; i < height; i++) {
                    vector<invertPixel>rowofInvert;
                    for (int j = 0; j < width; j++) {
                        int pixelRed = ((image.at(i)).at(j)).Red;
                        int pixelBlue = ((image.at(i)).at(j)).Blue;
                        int pixelGreen = ((image.at(i)).at(j)).Green;
                        invertPixel inv;
                        inv.blueInvert = maxColor - pixelBlue;
                        inv.greenInvert = maxColor - pixelGreen;
                        inv.redInvert = maxColor - pixelRed;
                        rowofInvert.push_back(inv);
                    }
                    invert.push_back(rowofInvert);
                }
                imageInv.close();
            }
            return 0;
        }
...