【问题标题】:Save pbm ascii to pbm binary c++将 pbm ascii 保存为 pbm 二进制 c++
【发布时间】:2013-12-04 00:18:49
【问题描述】:

我有文件 pbm P1 (ascii)

P1
#Created by Venom 
2 2 
1 0 
0 1

我想将其转换为 P4(pbm 二进制)。我该怎么做?

我有 int 的像素数组

int pixel[HEIGHT][WIDTH];

这里需要转成二进制数组

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

             }
}

【问题讨论】:

  • 您是要编写自己的转换器,还是只是在寻找可以进行必要转换的工具,还是要编写输出二进制 pbm 的程序?
  • 这是一种非常简单的格式。应该很容易弄清楚。 en.wikipedia.org/wiki/Netpbm_format
  • 你读过这个问题吗?我不知道如何转换
  • 我做了,我链接了一个描述格式的页面。这不是一个“为我编写代码”的网站,您需要自己尝试,并且欢迎您在此过程中提出任何具体问题。
  • 我怎样才能每个字节打包 8 个像素?

标签: c++ image-processing


【解决方案1】:

这似乎适用于我能够找到用于测试它的文件。

#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\n"))
        {
            return false;
        }
        if(!comment.empty() && !(f << comment << "\n"))
        {
            return false;
        }
        if(!(f << width << " " << height << "\n"))
        {
            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>\n";
        return 0;
    }

    PBM pbm;

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

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

    return 0;
}

【讨论】:

  • 转换器到字节不起作用,我有这个效果imghost.in/img/2013-11/23/exngc12dyrh6hblbkn1onajd1.jpg
  • 它适用于我尝试的每个源文件,但我无法找到看起来像那个的 Lena 图像。你是只测试我的代码还是其他什么?
  • 我找到了一个 512x512 灰度版本的 Lena,用 IrfanView 将其转换为黑白并保存为 ascii PBM。我的程序将其转换为二进制 pbm,没有任何问题。如果没有看到您的实际代码,恐怕我无法帮助您。
猜你喜欢
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
  • 2012-04-03
相关资源
最近更新 更多