C++ How to create a large bitmap from a datalist stored in a deque? -
i building first ever program in c++ on , on again.
the program intended create picture, a gradient - parameters of height h
, width l
, in pixels, , 4 parameters of density da, db, dc, dd
. gradient produced '1 bit' pixels: black or white - , simplest error-diffusion algorithm (so-called "naive" sometimes),
>> push error on next pixel of line.
after having performed optimization (deque
instead of vector
allows bigger images created ex.), stuck problem cannot solve now:
my pixel values being stored in deque
, how transport them bitmap file?
i tried understand easybmp
library couldn't find solution.
in code, can see line 33
tried (unsuccessfully) make .pbm header (portable bit map)
actually, i'd copy values of deque <int> dequea;
(line 30) .bmp
file or other raster file-format, instead of in .txt
file happens line 72!
here's code, , nice picture of 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 = 0; float b = 0; // local variables double io = 0; // variable i/o while (a<l+1, b<h+1){ //values given & 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) { = 0; b = b++; e = 0; f.clear(); } } //export values .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 files easy create , should suffice purpose. no need use external library.
a problem in original code storing both pbm metadata , actual image data in same dequea
. don't that. mixing data makes program hard understand , maintain.
you can create valid pbm file adjusting code writing 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);
the resulting ppm file works fine gimp.
writing bmp or tga file works same - first write header, followed actual data. main difference file formats binary files, file i/o looks bit different, , header , image data formats more complicated. still, both formats quite simple , easy write without using library.
Comments
Post a Comment