0

Question: What program / file format should I use?

I want to create an image (handdrawn pixel by pixel) and have it exported to text file formated as a pseudo-array of integers like this:

16  8

0   0   0   0   0   0   0   0   1   1   1   1   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   2   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   3   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   4   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   9   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   10  0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   11  0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   12  0   0   256 0

I want every number to represent unique color (not actually matter what color as long as drawing program (the simpler, the better - MS Paint would do) itself can read it and I can distinguish it from others). Ultimately I want the ability to quickly draw an image with external program and use such exported data within my code. Can someone point me to the right direction?

I tried to export image to .c with GIMP, but the results:

/* GIMP RGB C-Source image dump (Bez nazwy.c) */

static const struct {
  guint      width;
  guint      height;
  guint      bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ 
  guint8     pixel_data[64 * 64 * 2 + 1];
} gimp_image = {
  64, 64, 2,
  "\377\377\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"...

though close to the point, are not exactly what I expect.

And here I see other possible solution: draw simply bmp image and try to translate it to an array of ints in my actual code (C#). But I would like to avoid this unless there is some simple and quick way / handy library to do this.

Or maybe some of You know any online converter that can take .bmp and return an array of ints?

Math Guy
  • 115
  • 2
  • 10
  • Can you use something like this http://stackoverflow.com/questions/19586524/get-all-pixel-information-of-an-image-efficiently ? – KMoussa May 17 '17 at 12:20
  • Nice, but I'm looking rather for something like this [link]http://www.digole.com/tools/PicturetoC_Hex_converter.php – Math Guy May 17 '17 at 15:52

1 Answers1

0
#include "ui_mainwindow.h"
#include <QString>

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

void generate(Ui::MainWindow* ui)
{
    if (ui->pic_label->pixmap() != 0)
    {
        int cap = 1024 * 4;
        int ct = 32;    //color tolerance
        //int number_of_colors_third = 256 / color_tolerance;//=256/32=8
        int *uncompressedcolortable = new int[256*256*256];
        for (int k=0; k<256; k++){
            for (int j=0; j<256; j++){
                for (int i=0; i< 256; i++){
                    uncompressedcolortable[k*256*256+j*256+i] = k*1000000+j*1000+i;
                }
            }
        }
        int width = ui->pic_label->pixmap()->width();
        int height = ui->pic_label->pixmap()->height();
//        int tiles = width * height;

        if ( (width > cap || height > cap) && !(ui->actionSurpass_sanity_check->isChecked())){
            ui->label->setText("File is too big to generate map. Check menu option if you want to process it anyway.");
            return;
        }

        QImage image = ui->pic_label->pixmap()->toImage();
        ui->lcdRED->setPalette(Qt::red);
        ui->lcdGREEN->setPalette(Qt::green);
        ui->lcdBLUE->setPalette(Qt::blue);
        ui->progressBar->setMaximum(height);
        QColor pixel_color;
        ofstream outfile ("C:/Users/Niewzruszony/Desktop/map.map");
        outfile << width << " " << height << std::endl;

        for (int j = 0; j < height; j++ )
        {
            outfile << std::endl;
            for (int i = 0; i < width; i++)
            {
                pixel_color = image.pixelColor(i,j);
                outfile << uncompressedcolortable[pixel_color.red()*256*256 + pixel_color.green()*256 + pixel_color.blue()];
                if(i != width-1) outfile << "\t";
                ui->lcdRED->display(QString::fromStdString(to_string( pixel_color.red())));
                ui->lcdGREEN->display(QString::fromStdString(to_string( pixel_color.green())));
                ui->lcdBLUE->display(QString::fromStdString(to_string( pixel_color.blue())));
            }
            ui->progressBar->setValue(j+1);
        }

        ui->lcdRED->setPalette(Qt::lightGray);
        ui->lcdGREEN->setPalette(Qt::lightGray);
        ui->lcdBLUE->setPalette(Qt::lightGray);
        ui->lcdRED->display(QString::fromStdString("0"));
        ui->lcdGREEN->display(QString::fromStdString("0"));
        ui->lcdBLUE->display(QString::fromStdString("0"));

        ui->label->setText("Generated RGB map.");
        return;
    }
    ui->pic_label->setText("First please choose image to process!");

}

Done it wit Qt. Just thougth I will share the code. Note I've done some quick cleanup while posting, to not to include unimportant stuff (at least not all).

Math Guy
  • 115
  • 2
  • 10