Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

image processing - Save pbm ascii to pbm binary c++

I have file pbm P1 (ascii)

P1
#Created by Venom 
2 2 
1 0 
0 1

And I want to convert it as P4 (pbm binary). How I can do this?

i have pixel array of int

int pixel[HEIGHT][WIDTH];

I need convert it to binary array here

for (int i =0; i < HEIGHT ; i++)
     {
         for (int j =0; j < WIDTH; j++)
         {

             }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This seems to work for the files I was able to find to test it with.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct PBM
{
    unsigned int width;
    unsigned int height;
    std::string comment;
    std::vector<std::vector<char>> data;

    bool ReadAscii(std::ifstream& f)
    {
        std::string id;
        if(!std::getline(f, id) || id != "P1")
        {
            return false;
        }
        if(f.peek() == '#' && !std::getline(f, comment))
        {
            return false;
        }
        if(!(f >> width >> height))
        {
            return false;
        }
        data.resize(height);
        int temp;
        for(size_t y = 0; y < data.size(); ++y)
        {
            data[y].resize(width);
            for(size_t x = 0; x < width; ++x)
            {
                if(!(f >> temp))
                {
                    return false;
                }
                data[y][x] = static_cast<char>(temp);
            }
        }
        return true;
    }

    bool WriteBinary(std::ofstream& f)
    {
        if(!(f << "P4
"))
        {
            return false;
        }
        if(!comment.empty() && !(f << comment << "
"))
        {
            return false;
        }
        if(!(f << width << " " << height << "
"))
        {
            return false;
        }
        std::vector<char> linebits((width + (CHAR_BIT - 1)) / CHAR_BIT);
        for(size_t y = 0; y < data.size(); ++y)
        {
            std::fill(linebits.begin(), linebits.end(), 0);
            for(size_t x = 0; x < width; ++x)
            {
                const int byte_pos = x / CHAR_BIT;
                const int bit_pos = (CHAR_BIT - 1) - (x % CHAR_BIT);
                linebits[byte_pos] |= (data[y][x] << bit_pos);
            }
            if(!f.write(&linebits[0], linebits.size()))
            {
                return false;
            }
        }
        return true;
    }
};

int main(int argc, char * argv[])
{
    if(argc != 3)
    {
        std::cerr << "Usage: convertpbm <ascii filename> <binary filename>
";
        return 0;
    }

    PBM pbm;

    std::ifstream inFile(argv[1]);
    if(!inFile)
    {
        std::cerr << "Could not open '" << argv[1] << "' for input!
";
        return 0;
    }
    if(!pbm.ReadAscii(inFile))
    {
        std::cerr << "Error reading input file!
";
    }

    std::ofstream outFile(argv[2], std::ios::binary);
    if(!outFile)
    {
        std::cerr << "Could not open '" << argv[1] << "' for output!
";
        return 0;
    }
    if(!pbm.WriteBinary(outFile))
    {
        std::cerr << "Error writing output file!
";
    }

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...