How to store rgb information of a pixel on a text file?
divyesh ladva

I want to make an application in which any asset/image's rgb information is copied on a text file and then he can use that text file on my other application which reads rgb data of all pixels and construct a new copy of that particular asset. It reduces chances of asset alteration or simply makes it hard for others to change actual asset (according to me atleast).


What version of Allegro are you using, and is this for C or C++?

But for A4, and C, something like this:

int c = getpixel(x, y);
fprintf(file, "%d, %d, %d", getr(c), getg(c), getb(c));


To be fair though, it's not that hard to write a program to construct a bitmap from R,G,B,R,G,B, etc.

It's harder if you perform a basic encryption and decryption of your assets because then people have to examine the binary of your program to figure out the key and how to decrypt the assets. Plus encryption works on any asset type - not just rgb images.


You could calculate a sha1 checksum of your resources and use that for verifying them.

Oscar Giner

If you're so concerned about people not being able to use modified assets with that second application (I'm assuming the first app is a private one only you have access to and the second one is the public application that everyone else uses) your best bet is implementing a digital signature system:

Your application generates a digital signature for your assets, using a private key.
Your other application checks that the signature is correct when it loads the assets. People won't be able to modify your assets without knowing the private key, as they won't be able to generate a valid digital signature.

This is very easy to do by using any cryptography lib.

divyesh ladva

1) LennyLen, I'm using Allegro 5.2.0.

2) jmasterx, i know about that, but I'm making this application for learning purpose only and yes i was gonna store rgb info in binary form too.

3) beoran, i don't know much about sha1, i know it is some form of hashing (i guess,).Can you pls elaborate it?

4) Oscar Giner, as i said earlier I'm making it for information purpose only. But i would like to know how can i give my assets a digital signature. Is it done using allegro?

Thanks in advance.

Mark Oates

You can use the sha2 functions from AllegroFlare if you're interested:

Oscar Giner

4) Oscar Giner, as i said earlier I'm making it for information purpose only. But i would like to know how can i give my assets a digital signature. Is it done using allegro?

1. Generate a pair of public and private keys for a public-key cryptography algorithm (like RSA). Your first app will use the private key to generate the signature and your other app will use the public key to verify the signature.
1. Generate a hash of the data (this is for performance reasons not for security, so just use the fastest hashing algorithm you can get).
2. Encrypt the hash with the private key. This is the signature.
3. When the other program wants to read the data you generate again the hash (using the exact same algorithm, both programs must generate the same hash for the same data) and decrypt the signature with the public key (remember, the signature is just the hash encrypted). The computed hash and the decrypted signature must be identical, otherwise the data has been modified.

Since no one knows the private key but you, if anyone tries to modify the data they won't be able to generate the signature (encrypted hash). Verifying the signature only requires the public key, so anyone can do that.

divyesh ladva

Oscar Giner, i understand what you say but how to actually implement it in a code? Can you provide a pseudo code or something like that?

Also i want to learn how to store multiple value in a single key and read em back or just store values in a section without any keys and read em back. For example,

1[red_pixels_of_tiles] 2 31,2,3,4,5, 46,7,8,9,0; 5 6[/red_pixels_of_tiles] 7 8And so on

Or like this,

1 2[pixels] 3 4r=1,2,3,4; 5g=5,6,7,8; 6b=9,0,1,2; 7 8[/pixels]

I want to know how to write this type of config file and also how to read from this files and get any desired value.

Thanks in advance.

Mark Oates

If you're only interested in validating that the content hasn't been tampered with, a simple hashing function is the easiest approach. A public/private key isn't necessary in this case because you aren't needing to prove the identity of the owner.

Generating a Hash

The easiest way to generate a hash of an image file is to use the get_sha256_hash_of_file() function from AllegroFlare. Simply pass the filename of the image to obtain the string.

std::string image_hash = get_sha256_hash_of_file("bin/data/doggie.jpg");
std::cout << image_hash << std::endl;  // will print 479d98072585c81c26019899c4b7161f6a844ed5ae9d707b656b7767cc5fb9aa

You can then save the output of this string in your application as a constant.

#define DOGGIE_IMAGE_HASH "479d98072585c81c26019899c4b7161f6a844ed5ae9d707b656b7767cc5fb9aa"

When you want to load the image in your application, you can check that the generated hash is identical to the expected hash:

void validate_doggie_image(std::string filename="bin/data/doggie.jpg")
   std::string image_hash = get_sha256_hash_of_file(filename);
   if (image_hash != DOGGIE_IMAGE_HASH) raise std::runtime_error("Doggie image invalid");

divyesh ladva

Mark Oates, your answer for hash function came in handy to me. Thanks for that. But my original question still remains unanswered, how do i store rgb values of a single pixel of an image to a text file or config file(pls)? To quote again I'm using allegro 5.2.2 with code blocks compiler on windows 7. And secondly i want to know how to store them in a specific manner in a config file in the way i told in my last reply.

Arthur Kalliokoski

Are you asking about basic file input/output or are you asking about accessing the pixel data? LennyLen already demonstrated writing the file in the second post (first you need to fopen the filename). One option to read it back in is using fscanf to read it as a formatted string in the reverse of fprintf. Those are C functions.

If you want a C++ solution then you'd use a std::ofstream and std::ifstream to write and read the file similarly to how you write data to the standard output (`std::cout`) (console) stream.

You can learn loads of information about writing and reading files in C or C++ with Google. We'd be happy to help you understand it if you have trouble.

Update: Fixed C++ class names. :-X

Mark Oates

how do i store rgb values of a single pixel of an image to a text file or config file(pls)?

Well, there's two parts to that. First, you need to obtain the pixel color data from the image, and next, write it to the file.

Get the pixel color of an image.

To obtain the color from an image, you'll use al_get_pixel to grab individual color components at a coordinate.

1#include <string> 2#include <allegro5/allegro.h> 3 4ALLEGRO_COLOR get_image_color_at(std::string image_filename, int x, int y) 5{ 6 ALLEGRO_BITMAP *bitmap = al_load_bitmap(image_filename.c_str()); 7 8 if (!bitmap) 9 throw std::runtime_error("Cannot load pixel data: The image does not exist."); 10 11 if (x < 0 || x >= al_get_bitmap_width(bitmap) || y < 0 || y >= al_get_bitmap_height(bitmap)) 12 throw std::runtime_error("Cannot load pixel data: Coordinates of bounds"); 13 14 ALLEGRO_COLOR obtained_color = al_get_pixel(bitmap, x, y); 15 16 al_destroy_bitmap(bitmap); 17 18 return obtained_color; 19}

Note that you could also get the alpha color value if you use al_map_rgba, or you can get the values as floats rather than unsigned chars with the al_map_rgba_f, and al_map_rgb_f variations. :)

Writing color data to a file

If you're using C++, writing data to a file is relatively simple.

1#include <allegro5/allegro_color.h> 2#include <fstream> 3#include <string> 4 5bool write_color_to_file(ALLEGRO_COLOR color, std::string filename="my_color_file.txt") 6{ 7 unsigned char r; 8 unsigned char g; 9 unsigned char b; 10 11 // get the color 12 13 al_unmap_rgb(color, &r, &g, &b); // will "fill" r, g, and b with that color's value. 14 15 // write the values to a file 16 17 std::ofstream outfile(filename); // ofstream is an "output file" stream 18 outfile << r << " " << g << " " << b << std::endl; 19 outfile.close(); 20 21 return true; 22}

There are a lot of optimizations you could make with these functions - for example, if you wanted to store multiple color values to a file you might fold get_image_color_at() and write_color_to_file() together (and possibly add a loop). Ideally, you should only open an ALLEGRO_BITMAP once.

divyesh ladva

Sorry late reply guys. Mark Oates, let me make self very very clear now, the function in allegro 5 lib "al_set_config_value()" takes a const char* as it's last parameter which is for the value to be written to a configuration file. The function "al_unmap_rgb()" takes unsigned char for the last 3 parameters, which are red, green and blue value of a pixel. Now my compiler "code::blocks v16.01" gives an error in the function that "it cannot convert unsigned char to const char*". Now how to solve that one. That's the problem I've been facing since LennyLen and Arthur Kalliokoski first replied. Please me with this problem.

Arthur Kalliokoski

"it cannot convert unsigned char to const char*"

It's asking for a char pointer, which is not a char, but a pointer to a char.

What al_unmap_rgb() returns are certainly chars, but it would be better to think of them as bytes, rather than chars, which seems to hint that they're printable characters.

What you need is to "print" the values of those unsigned chars to a char buffer, then point your al_set_config_value() at that buffer.

1#include <stdio.h> 2#include <allegro5/allegro5.h> 3 4 5int main(void) 6{ 7 char buffer[256]; 8 ALLEGRO_COLOR color; 9 ALLEGRO_CONFIG myconfig; 10 unsigned char mysection[20]; 11 unsigned char mykey[20]; 12 unsigned char r, g, b; 13 14 al_unmap_rgb(color, &r, &g, &b); 15 sprintf(buffer, "%u %u %u\n",r,g,b); 16 al_set_config_value(myconfig, mysection, mykey, buffer); 17 return 0; 18}

Now I can't get the above to compile because it doesn't know the size of ALLEGRO_CONFIG or something, but it should get you closer to what you want.


The best way to get help with this kind of problem is to post the exact error message text and ideally the code (or a shortened subset of the code) suffering from the error so that we don't have to go out of our way to invent a program that's nothing like what you're doing.

Looks like there's an example program (ex_config) that demonstrates it, and the source for that is available here by clicking on "ex_config":

Thread #616987. Printed from