C ++如何创建从存储在一个deque一个DataList大位图? [英] C++ How to create a large bitmap from a datalist stored in a deque?

查看:301
本文介绍了C ++如何创建从存储在一个deque一个DataList大位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设我的第一次节目在C ++中一遍又一遍。

I am building my first ever program in C++ over and over again.

该计划旨在创建一个图片,的渐变的 - 有高度 ^ h ,宽度升参数,在像素和4参数密度 DA,DB,DC,DD
此梯度通过'1位'像素产生:它们是黑色或白色 - 和由简单的误差扩散算法(所谓的幼稚有时),

The program is intended to create a picture, a gradient - with parameters of height h, width l, in pixels, and 4 parameters of density Da, Db, Dc, Dd. This gradient is produced by '1 bit' pixels: they are black or white - and by the simplest error-diffusion algorithm (so-called "naive" sometimes),

>>推线的下一个像素的误差。

在已经进行的优化(的双端而不是矢量允许为前创造更大的图像。)我坚持,我不能现在解决一个问题:

After having performed an optimization (deque instead of vector allows bigger images to be created for ex.), I am stuck with a problem that I cannot solve for now:

我的像素值存储在双端,我怎么运送他们到一个位图文件?

My pixel values being stored in a deque, how do I transport them into a bitmap file?

我试图理解 EasyBMP 库,但我无法找到解决方案。

I tried to understand the EasyBMP library but I couldn't find the solution.

在我的code,你可以看到 33行,我试图(失败)做出.PBM头(便携位图)

In my code, you can see line 33 that I tried (unsuccessfully) to make a .PBM header (Portable Bit Map)

其实,我想的值复制我的双端< INT> dequeA; (第30行)
.BMP 文件或任何其它光栅文件格式,而不是在 .TXT 文件中像它发生线72!

Actually, I'd like to copy the values of my deque <int> dequeA; (line 30) to a .BMP file or any other raster file-format, instead of in a .txt file like it happens line 72!

下面是我的code和它带来多么大的漂亮的图片:

Here's my code, and a nice picture of what it makes :

#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <iterator>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;

// constant values

double   Da=1;          //densities
double   Db=0.5;
double   Dc=0.5;
double   Dd=0;

double      l = 999;    //width & height => L = l+1  ///  999 = 1000 pixels
double      h = 999;

//double      u = 1;    // UNIT

double      D = 0;      // GAMMA
double      E = 0;      // ERROR LOCAL
vector <double> F;      // ERROR DYNAMIC

int main ()
{
    // vector
    deque <int> dequeA;
    F.push_back (0);

    //dequeA.push_back (2);     //FAKE PBM header (old)
    //dequeA.push_back (l+1);
    //dequeA.push_back (h+1);
    //dequeA.push_back (1);

    float a = 0;
    float b = 0;                // Local variables
    double IO = 0;              // variable I/O

    while (a<l+1, b<h+1){

        //values for given a & b
        double DL = Da-Da*(b/h)+Dc*(b/h);
        double DR = Db-Db*(b/h)+Dd*(b/h);
        double D  = DL-DL*(a/l)+DR*(a/l); //GAMMA

        if (E+0-D<=-0.5) {
            dequeA.push_back(1);
            IO = 1;
        }

        else {
            dequeA.push_back(0);
            IO = 0;
        }

        E = E+IO-D;
        F.push_back(E);

        // next pixel & next line
        a++;
        if (a>l) {
            a = 0;
            b = b++;
            E = 0;
            F.clear();
        }
    }
    //export values to .txt file
    ofstream output_file("./test.txt");
    ostream_iterator<int> output_iterator(output_file, "\n");
    copy(dequeA.begin(), dequeA.end(), output_iterator);
    dequeA.clear();
}

推荐答案

PBM文件非常容易创建和应该能满足你的目的。无需使用外部库。

PBM files are very easy to create and should suffice for your purpose. No need to use an external library.

在你原来的code的一个问题是,你是存储两个PBM元数据,并在同一 dequeA 实际的图像数据。不要那样做。混合数据,如这使得你的程序难于理解和维护。

A problem in your original code was that you were storing both the PBM metadata and the actual image data in the same dequeA. Don't do that. Mixing data like this makes your program hard to understand and maintain.

您可以通过略微调整code编写的文件中创建一个有效的PBM文件:

You can create a valid PBM file by slightly adjusting the code writing the file:

ofstream output_file("./test.ppm");

// write file header
output_file << "P1\n" << (l+1) << " " << (h+1) << "\n";
//write image data
ostream_iterator<int> output_iterator(output_file, "\n");
copy(dequeA.begin(), dequeA.end(), output_iterator);

由此产生的PPM文件工作正常瘸子。

The resulting PPM file works fine with Gimp.

写入BMP或TGA文件的工作原理基本相同 - 你先写一个标题,然后是实际数据。主要的区别是,那些文件格式是二进制文件,所以文件I / O看起来有点不同,并且首标和图象数据格式是稍微复杂一些。但尽管如此,这两种格式都相当简单容易写,甚至不使用图书馆。

Writing to a BMP or TGA file works basically the same - you first write a header, followed by the actual data. The main difference is that those file formats are binary files, so the file I/O looks a bit different, and the header and image data formats are slightly more complicated. But still, both formats are quite simple and easy to write even without using a library.

这篇关于C ++如何创建从存储在一个deque一个DataList大位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆