c++ - 尝试读取和写入 bmp 文件时中止 C++

标签 c++ bmp

这是我在 stackoverflow 上的第一篇文章。 我写了一个程序,应该输入一个 bmp 文件,然后将其黑白化,然后将其写入 out.bmp。 开始写代码的时候,我把input名称末尾的bmp文件格式删掉,然后用文本编辑器打开,然后写代码,输出的风格和input一样。 当我输入 ./a.out <in.bmp>out.bmp在终端中,我收到一个中止错误(Aborted (core dumped)),当我给出 ./a.out < in > out.bmp 时gimp 告诉我它不是 bmp 文件。 这是代码:

// In the Name of God

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <math.h>
#include <sstream>

using namespace std;

bool TypeIsTrue (string type){
    if (type == "424d")
        return true;
    return false;
}
int GrayIt (int b , int g , int r){


    return (b + g + r) / 3 ;
}
int ConvertToDec(string hex){
    long int dec = 0;
    reverse(hex.begin(), hex.end());
    for (int i = 0 ; i <hex.size();i++){
        if (hex[i] == 'a')
            dec = dec + 10 *(pow(16,i));
        else if (hex[i] == 'b')
            dec = dec + 11 *(pow(16,i));
        else if (hex[i] == 'c')
            dec = dec + 12 *(pow(16,i));
        else if (hex[i] == 'd')
            dec = dec + 13 *(pow(16,i));
        else if (hex[i] == 'e')
            dec = dec + 14 *(pow(16,i));
        else if (hex[i] == 'f')
            dec = dec + 15 *(pow(16,i));
        else
            dec = dec + ((hex[i] - '0')*(pow(16,i)));
    }
    return dec;
}
string ConvertToHex(int dec){
    string hex;
    int reminded,Divided;
    reminded = dec % 16 ;
    Divided = dec / 16;
    if (Divided == 10){
        hex = "a";
    }
    else if (Divided == 11){
        hex = "b";
    }
    else if (Divided == 12){
        hex = "c";
    }
    else if (Divided == 13){
        hex = "d";
    }
    else if (Divided == 14){
        hex = "e";
    }
    else if (Divided == 15){
        hex = "f";
    }
    else if (Divided == 0){
        hex = "0";
    }
    else if (Divided == 1){
        hex = "1";
    }
    else if (Divided == 2){
        hex = "2";
    }
    else if (Divided == 3){
        hex = "3";
    }
    else if (Divided == 4){
        hex = "4";
    }
    else if (Divided == 5){
        hex = "5";
    }
    else if (Divided == 6){
        hex = "6";
    }
    else if (Divided == 7){
        hex = "7";
    }
    else if (Divided == 8){
        hex = "8";
    }
    else if (Divided == 9){
        hex = "9";
    }
    if (reminded == 10){
        hex = hex+"a";
    }
    else if (reminded == 11){
        hex = hex+"b";
    }
    else if (reminded == 12){
        hex = hex+"c";
    }
    else if (reminded == 13){
        hex = hex+"d";
    }
    else if (reminded == 14){
        hex = hex+"e";
    }
    else if (reminded == 15){
        hex = hex+"f";
    }
    else if (reminded == 0){
        hex = hex+"0";
    }
    else if (reminded == 1){
        hex = hex+"1";
    }
    else if (reminded == 2){
        hex = hex+"2";
    }
    else if (reminded == 3){
        hex = hex+"3";
    }
    else if (reminded == 4){
        hex = hex+"4";
    }
    else if (reminded == 5){
        hex = hex+"5";
    }
    else if (reminded == 6){
        hex = hex+"6";
    }
    else if (reminded == 7){
        hex = hex+"7";
    }
    else if (reminded == 8){
        hex = hex+"8";
    }
    else if (reminded == 9){
        hex = hex+"9";
    }
    return hex;
}
int main (){
    vector <string> a;
    vector <string> r;
    vector <string> g;
    vector <string> b;
    vector <string> out;
    string temp;
    int red,green,blue;
    while(cin >> temp){
        a.push_back (temp);
    }
    if(!TypeIsTrue(a[0])){
        cout<<"The file is not bmp\nRerun program"<<endl;
        abort();
    }   
    int phase = 1;
    for (int i = 27 ; i < a.size(); i++){ //int i = 27

        string first;
        string last;
        first = a[i].substr(0,2);
        last = a[i].substr(2,3);
        if(phase == 4)
            phase = 1;
        if(phase == 1){
            b.push_back(first);
            g.push_back(last);
            phase ++;
            // cout<<"push_backed"<<endl;   
        }
        else if(phase == 2){
            r.push_back(first);
            b.push_back(last);
            phase ++;
            // cout<<"push_backed"<<endl;
        }
        else if(phase == 3){
            g.push_back(first);
            r.push_back(last);
            phase ++;
            // cout<<"push_backed"<<endl;
        }

    }
    for (int i = 0 ; i <27 ; i++){
        out.push_back(a[i]);
    }
    for(int i = 27 ; i<b.size() ; i++){
        blue = ConvertToDec(b[i]);  
        green = ConvertToDec(g[i]);
        red = ConvertToDec(r[i]);
        out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
        out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
        out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
    }
    int j = 1 ;
    for (int i = 0 ; i < 27 ; i++){
        cout<< out[i] << " ";
        if (j == 8){
            cout<<endl;
            j = 0;
        }
        j++;
    } 
    j=1;
    bool space = false;
    for (int i = 27 ; i < out.size(); i++){
        if( i == 27 + 10){
            cout<<endl;
            j = 1;

        }
        cout<<out[i];
        if (space)
            cout<<" ";
        j++;
        if(j == 17){
            cout<<endl;
            j = 1 ;
        }
        space=!space;
    }
    return 0;
}

最佳答案

你收到一个中止错误,因为你要求一个。

if(!TypeIsTrue(a[0])){
    cout<<"The file is not bmp\nRerun program"<<endl;
    abort();
}

如果您的程序被设计为重定向输出,那么将错误消息发送到 stderr(和 std::cerrstd: :clog) 而不是 stdout

顺便说一句,类型测试失败是因为 BMP 文件不是文本,使用 cin >> 变量 读取它们没有任何意义。

此外,甚至无法保证 a[0] 存在。您需要先测试 a.size()

if(a.size() < 1 || !TypeIsTrue(a[0])){
    cerr << "The file is not bmp\nRerun program\n";
    abort();
}

关于c++ - 尝试读取和写入 bmp 文件时中止 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28463363/

相关文章:

c - 在 C 中处理 bmp 文件时,为什么 fopenf_s 会将文件指针初始化为 NULL?

c - 在 C 中解析/读取位图文件

派生类的 C++ 数组

c++ - Visual C++ 2010 Beta 2 上的复制省略

c++ - 如何从 qtablewidget 中的项目获取文本?

c++ - 从 boost 套接字接收数据问题

c++ - 在 qt 小部件应用程序中运行 CGAl 包中的 draw_polygon 示例

c - 以度为单位旋转位图的函数工作得非常快,但会重新定位输出图像

c++ - BMP 纹理不显示

c++ - 添加 BMP 灰度 header