Change pixel to Symbols/Chars - c++

I want to try and make, in C++, a Program that takes an Image and scans pixel by pixel, for relatively Dark pixels it prints a space and for Light Pixels it prints symbols like:
"##$%^!M()-~, etc" that will paint new picture with only symbols.
I am new and I really would appreciate some guidance for how it is I can approach that,
I would like to Surprise my teacher.
Thank you very much in advance.
ok that what i already had:
#include <windows.h>
#include <stdio.h>
BYTE* ConvertBMPToRGBBuffer(BYTE* Buffer, int width, int height)
{
// first make sure the parameters are valid
if ((NULL == Buffer) || (width == 0) || (height == 0))
return NULL;
// find the number of padding bytes
int padding = 0;
int scanlinebytes = width * 3;
while ((scanlinebytes + padding) % 4 != 0) // DWORD = 4 bytes
padding++;
// get the padded scanline width
int psw = scanlinebytes + padding;
// create new buffer
BYTE* newbuf = new BYTE[width*height * 3];
// now we loop trough all bytes of the original buffer,
// swap the R and B bytes and the scanlines
long bufpos = 0;
long newpos = 0;
for (int y = 0; y < height; y++)
for (int x = 0; x < 3 * width; x += 3)
{
newpos = y * 3 * width + x;
bufpos = (height - y - 1) * psw + x;
newbuf[newpos] = Buffer[bufpos + 2];
newbuf[newpos + 1] = Buffer[bufpos + 1];
newbuf[newpos + 2] = Buffer[bufpos];
}
return newbuf;
}
BYTE* LoadBMP(int* width, int* height, long* size, LPCTSTR bmpfile)
{
// declare bitmap structures
BITMAPFILEHEADER bmpheader;
BITMAPINFOHEADER bmpinfo;
// value to be used in ReadFile funcs
DWORD bytesread;
// open file to read from
HANDLE file = CreateFile(bmpfile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (NULL == file)
return NULL; // coudn't open file
// read file header
if (ReadFile(file, &bmpheader, sizeof (BITMAPFILEHEADER), &bytesread, NULL) == false)
{
CloseHandle(file);
return NULL;
}
//read bitmap info
if (ReadFile(file, &bmpinfo, sizeof (BITMAPINFOHEADER), &bytesread, NULL) == false)
{
CloseHandle(file);
return NULL;
}
// check if file is actually a bmp
if (bmpheader.bfType != 'MB')
{
CloseHandle(file);
return NULL;
}
// get image measurements
*width = bmpinfo.biWidth;
*height = abs(bmpinfo.biHeight);
// check if bmp is uncompressed
if (bmpinfo.biCompression != BI_RGB)
{
CloseHandle(file);
return NULL;
}
// check if we have 24 bit bmp
if (bmpinfo.biBitCount != 24)
{
CloseHandle(file);
return NULL;
}
// create buffer to hold the data
*size = bmpheader.bfSize - bmpheader.bfOffBits;
BYTE* Buffer = new BYTE[*size];
// move file pointer to start of bitmap data
SetFilePointer(file, bmpheader.bfOffBits, NULL, FILE_BEGIN);
// read bmp data
if (ReadFile(file, Buffer, *size, &bytesread, NULL) == false)
{
delete[] Buffer;
CloseHandle(file);
return NULL;
}
// everything successful here: close file and return buffer
CloseHandle(file);
return Buffer;
}
void main()
{
int x, y;
long s, s2;
BYTE* a = LoadBMP(&x, &y, &s, L"20140626_143101.bmp);
BYTE* b = ConvertBMPToRGBBuffer(a, x, y);
// what now??
delete[] a;
delete[] b;
}
after i convert the BMP to RGB Buffer what i can do next?? to check about the buffer if it dark or light pixels

I would like to set you on the correct path, that is, without giving you the entire answer. I have found some references which will take you to what it is you would like to learn.
Firstly, one must decode the image. That is copy the image into a variable in another, readable, format. Writing one of these, especially when knew to C++, is not a good idea. But there are many libraries which do this very thing. I recommend:
http://cimg.sourceforge.net/ and then as brief tutorial: http://www.math.ucla.edu/~wittman/hyper/vick/Cimg_tutorial.pdf and Proceeding That to Edit a Single Pixel: http://cimg.sourceforge.net/reference/group__cimg__loops.html (Look at the: Loops over interior regions and borders section)
This hopefully will point you in the right direction.
P.S. Feel free to ask if you have any more questions.

Related

C++ bitmap to base64

I have the following code to create a bitmap:
//raw data
PBYTE firstPixel = (PBYTE)((PBYTE)AnsiBdbRecord) + sizeof(WINBIO_BDB_ANSI_381_RECORD);
// declare other bmp structures
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER info;
RGBQUAD rq[256];
// create the grayscale palette
for (int i = 0; i<256; i++)
{
rq[i].rgbBlue = i;
rq[i].rgbGreen = i;
rq[i].rgbRed = i;
rq[i].rgbReserved = 0;
}
//RGBQUAD bl = { 0,0,0,0 }; //black color
//RGBQUAD wh = { 0xff,0xff,0xff,0xff }; // white color
// andinitialize them to zero
memset(&bmfh, 0, sizeof(BITMAPFILEHEADER));
memset(&info, 0, sizeof(BITMAPINFOHEADER));
// fill the fileheader with data
bmfh.bfType = 0x4d42; // 0x4d42 = 'BM'
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); // + padding;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD);
// fill the infoheader
info.biSize = sizeof(BITMAPINFOHEADER);
info.biWidth = Width;
info.biHeight = Height;
info.biPlanes = 1; // we only have one bitplane
info.biBitCount = PixelDepth; // RGB mode is 24 bits
info.biCompression = BI_RGB;
info.biSizeImage = 0; // can be 0 for 24 bit images
info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values
info.biYPelsPerMeter = 0x0ec4;
info.biClrUsed = 0; // we are in RGB mode and have no palette
info.biClrImportant = 0; // all colors are importantenter code here
And I save it as follows:
HANDLE file = CreateFile(bmpfile, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == NULL)
{
DWORD dw = GetLastError();
CloseHandle(file);
}
// write file header
if (WriteFile(file, &bmfh, sizeof(BITMAPFILEHEADER), &bwritten, NULL) == false)
{
DWORD dw = GetLastError();
CloseHandle(file);
}
// write infoheader
if (WriteFile(file, &info, sizeof(BITMAPINFOHEADER), &bwritten, NULL) == false)
{
DWORD dw = GetLastError();
CloseHandle(file);
}
//write rgbquad for black
if (WriteFile(file, &rq, sizeof(rq), &bwritten, NULL) == false)
{
DWORD dw = GetLastError();
CloseHandle(file);
}
// write image data
if (WriteFile(file, &firstPixel[0], imageSize, &bwritten, NULL) == false)
{
DWORD dw = GetLastError();
CloseHandle(file);
}
// and clean up
CloseHandle(file);
I think the above is the standard way of saving bitmap images. However, instead of saving the image, I want it to be available as BASE64 and pass it in a HTTP Post. Therefore, this question relates to this one, but I am having a lot of difficulties converting the bmp structure to BASE64. I have taken the BASE64 encoder from here, but I have no idea how to pass the BMPFILEHEADER, BMPINFOHEADER, RGBQUAD, and raw data structure as a parameter to the BASE64 encoder.
Any thoughts or pointers on how to combine the information I gathered?
UPDATE
Thanks to Roman Pustylnikov, I have gotten a bit farther already:
I'm creating a struct like this:
struct ImageBuffer
{
BITMAPFILEHEADER bfheader;
BITMAPINFOHEADER infobmp;
RGBQUAD rgb[256];
PBYTE bitmap;
};
Fill it as follows:
ImageBuffer capture;
capture.bfheader = bmfh;
capture.infobmp = info;
// create the grayscale palette
for (int i = 0; i<256; i++)
{
capture.rgb[i].rgbBlue = i;
capture.rgb[i].rgbGreen = i;
capture.rgb[i].rgbRed = i;
capture.rgb[i].rgbReserved = 0;
}
capture.bitmap = firstPixel;
And convert it as follows:
int totalSize = sizeof(capture.bfheader) + sizeof(capture.infobmp) + sizeof(capture.rgb) + imageSize;
std::string encodedImage = base64_encode(reinterpret_cast<const unsigned char*>(&capture), totalSize);
However, it gives me an invalid bitmap. Also, when I load the bitmap from disk (the one that is generated with writefile), I get a different base64 string. I use C# code to compare the two Base64 strings:
// generated base64 string
string test = "Put base64string generated from C++ here";
byte[] imageBytes = Convert.FromBase64String(test);
// generate the same string based on the actual bmp
byte[] data = File.ReadAllBytes(#"c:\successtest.bmp");
string original = Convert.ToBase64String(data);
UPDATE TWO: solution
The solution can be found in the latest update of Roman Pustylnikov's answer.
You can do it without file in the middle, using the following example as encoder/decoder:
The first approach is to create the contiguous memory and put it as an input:
int size=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256 + imageSize ;
unsigned char * bmpBuff = new char[size];
int i = 0;
memcpy((void *)( &( bmpBuff[i] )), (void *) &bfheader,sizeof(BITMAPFILEHEADER) );
i+=sizeof(BITMAPFILEHEADER);
memcpy((void *)( &( bmpBuff[i] )), (void *) &infobmp,sizeof(BITMAPINFOHEADER) );
i+=sizeof(BITMAPINFOHEADER);
memcpy((void *)( &( bmpBuff[i] )), (void *) &rq,sizeof(RGBQUAD)*256 );
i+=sizeof(RGBQUAD)*256;
memcpy((void *)( &( bmpBuff[i] )), (void *) firstPixel, imageSize );
std::string encodedImage = base64_encode(bmpBuff, size);
The cons of this approach is that you need to duplicate the memory.
Another approach is to handle the "lost triplet". For this we'll need to define a structure:
struct ScreenShotBuffer
{
BITMAPFILEHEADER bfheader;
BITMAPINFO infobmp;
RGBQUAD rgb[256];
};
Now here comes the tricky part. Since the encoding handles the triples of bytes, you need to handle the border between two buffers (I haven't tested this so it might contain bugs, just a general approach).
int size=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256;
unsigned char *info = einterpret_cast<const unsigned char*> &screenshotInfo;
unsigned char *img = einterpret_cast<const unsigned char*> firstPixel;
std::string encodedInfo = base64_encode(info , size);
std::string encodedLostTriplet = "";
int offset = size%3;
unsigned char lostTriplet[3];
if (offset) {
lostTriplet[0]=info[size-offset];
if (offset==2)
lostTriplet[1] = info[size-offset+1];
else
lostTriplet[1] = img[0];
lostTriplet[2] = img[2-offset];
encodedLostTriplet = base64_encode(lostTriplet, 3);
}
else {
offset=3;
}
std::string encodedData = base64_encode(reinterpret_cast<const unsigned char*> &img[3-offset], imageSize - (3 - offset) );
std::string encodedImage = encodedInfo + lostTriplet + encodedData;
A little bit messy but should work on the same memory.

How can I put a device-independent bitmap into the Windows clipboard using just direct WinAPI? (No MFC or other wrappers)

I've been trying to get this to work for awhile now, but I can't seem to figure it out, and hours of Googling has yet to reveal any useful results.
I have an array of 32-bit pixels in RGBA order, and I want to create a device-independent bitmap from them and place it on the clipboard using SetClipboardData(CF_DIBV5, dib) or similar (ideally, I want to preserve the alpha channel). Registering a custom clipboard type is not an option, since the point of putting it on the clipboard is so that it can be pasted into another program. Bonus points if I don't have to manually convert my pixel data into some other format (such as planar BGRA).
My current code goes like this (it's all within a set_clipboard_img function):
if(!OpenClipboard(hwnd)) return;
BITMAPV5HEADER* info = (BITMAPV5HEADER*) GlobalAlloc(GMEM_MOVEABLE, sizeof(BITMAPV5HEADER));
info->bV5Size = sizeof(BITMAPV5HEADER);
info->bV5Width = img_width;
info->bV5Height = -img_height;
info->bV5Planes = 1; // The docs say this is the only valid value here.
info->bV5BitCount = 32;
info->bV5Compression = BI_BITFIELDS;
info->bV5SizeImage = img_width * img_height * 4;
info->bV5RedMask = 0xff000000;
info->bV5GreenMask = 0x00ff0000;
info->bV5BlueMask = 0x0000ff00;
info->bV5AlphaMask = 0x000000ff;
unsigned char* buf;
// One of the sources I found said that I can pass a BITMAPV5HEADER in place of the BITMAPINFO, hence the first reinterpret_cast.
HBITMAP dib = CreateDIBSection(NULL, reinterpret_cast<BITMAPINFO*>(info), DIB_RGB_COLORS, reinterpret_cast<void**>(&buf), NULL, 0);
if(dib == NULL) {
CloseClipboard();
return;
}
// img_pixels_ptr is a unsigned char* to the pixels in non-planar RGBA format
std::copy_n(img_pixels_ptr, info->bV5SizeImage, buf);
EmptyClipboard();
auto result = SetClipboardData(CF_DIBV5, dib);
if(result == NULL) {
char str[256];
str[255] = 0;
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, str, 255, NULL);
std::cerr << "Error setting clipboard: " << str << std::endl;
// Here I get "handle not valid". I have no idea _why_ it's not valid, though.
}
CloseClipboard();
Ultimately, I'll also need to be able to reverse the process (getting a potentially-transparent bitmap off the clipboard), but one thing at a time.
You cannot pass an HBITMAP to SetClipboardData(). It requires an HGLOBAL from GlobalAlloc() instead. That is why SetClipboardData() is failing with an ERROR_INVALID_HANDLE error.
You need to put your BITMAPV5HEADER and pixel data directly into the allocated HGLOBAL and put it as-is onto the clipboard, forget using CreateDIBSection() at all:
Standard Clipboard Formats
CF_DIBV5
17
A memory object containing a BITMAPV5HEADER structure followed by the bitmap color space information and the bitmap bits.
Try something more like this:
void printErr(const char *msg)
{
char str[256] = {0};
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, str, 255, NULL);
std::cerr << msg << ": " << str << std::endl;
}
...
DWORD size_pixels = img_width * img_height * 4;
HGLOBAL hMem = GlobalAlloc(GHND, sizeof(BITMAPV5HEADER) + size_pixels);
if (!hMem)
{
printErr("Error allocating memory for bitmap data");
return;
}
BITMAPV5HEADER* hdr = (BITMAPV5HEADER*) GlobalLock(hMem);
if (!hdr)
{
printErr("Error accessing memory for bitmap data");
GlobalFree(hMem);
return;
}
hdr->bV5Size = sizeof(BITMAPV5HEADER);
hdr->bV5Width = img_width;
hdr->bV5Height = -img_height;
hdr->bV5Planes = 1;
hdr->bV5BitCount = 32;
hdr->bV5Compression = BI_BITFIELDS;
hdr->bV5SizeImage = size_pixels;
hdr->bV5RedMask = 0xff000000;
hdr->bV5GreenMask = 0x00ff0000;
hdr->bV5BlueMask = 0x0000ff00;
hdr->bV5AlphaMask = 0x000000ff;
// img_pixels_ptr is a unsigned char* to the pixels in non-planar RGBA format
CopyMemory(hdr+1, img_pixels_ptr, size_pixels);
GlobalUnlock(hMem);
if (!OpenClipboard(hwnd))
{
printErr("Error opening clipboard");
}
else
{
if (!EmptyClipboard())
printErr("Error emptying clipboard");
else if (!SetClipboardData(CF_DIBV5, hMem))
printErr("Error setting bitmap on clipboard");
else
hMem = NULL; // clipboard now owns the memory
CloseClipboard();
}
if (hMem)
GlobalFree(hMem);

Access violation writing location 0x00BB9000

I'm currently working on PNG formats. And, I'm trying to understand its characteristics:
width, height, bytewidth, bytes per width, RGB pixel (red, green, blue)
Refering to this and this, width is the resolution width of the image in pixels, height is the resolution height of the image in pixels, bytes per width takes only 8 or 16 as value, RGB pixel's three items takes 0 to 255 as integer value.
But, I cannot understand what's a bytewidth?
Because when I try to convert one bmp image to a png one in C++ I get the following error:
Unhandled exception at 0x01038F6C in Project.exe: 0xC0000005: Access violation writing location 0x00BB9000.
I get this when I initialize the output png characteristics to:
width = 1600,
height = 900,
bytewidth = 1,
bytes per width = 1,
RGB pixel (red = 255, green = 255, blue = 255)
using this code:
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_DEPRECATE
#include <png.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
typedef struct _RGBPixel {
uint8_t blue;
uint8_t green;
uint8_t red;
} RGBPixel;
/* Structure for containing decompressed bitmaps. */
typedef struct _RGBBitmap {
RGBPixel *pixels;
size_t width;
size_t height;
size_t bytewidth;
uint8_t bytes_per_pixel;
} RGBBitmap;
/* Returns pixel of bitmap at given point. */
#define RGBPixelAtPoint(image, x, y) \
*(((image)->pixels) + (((image)->bytewidth * (y)) \
+ ((x) * (image)->bytes_per_pixel)))
/* Attempts to save PNG to file; returns 0 on success, non-zero on error. */
int save_png_to_file(RGBBitmap *bitmap, const char *path)
{
FILE *fp = fopen(path, "wb");
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_uint_32 bytes_per_row;
png_byte **row_pointers = NULL;
if (fp == NULL) return -1;
/* Initialize the write struct. */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return -1;
}
/* Initialize the info struct. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return -1;
}
/* Set up error handling. */
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return -1;
}
/* Set image attributes. */
png_set_IHDR(png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* Initialize rows of PNG. */
bytes_per_row = bitmap->width * bitmap->bytes_per_pixel;
row_pointers = (png_byte **)png_malloc(png_ptr, bitmap->height * sizeof(png_byte *));
for (y = 0; y < bitmap->height; ++y) {
uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel);
row_pointers[y] = (png_byte *)row;
for (x = 0; x < bitmap->width; ++x) {
RGBPixel color = RGBPixelAtPoint(bitmap, x, y);
*row++ = color.red;
*row++ = color.green; /************* MARKED LINE ***************/
*row++ = color.blue;
}
}
/* Actually write the image data. */
png_init_io(png_ptr, fp);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
/* Cleanup. */
for (y = 0; y < bitmap->height; y++) {
png_free(png_ptr, row_pointers[y]);
}
png_free(png_ptr, row_pointers);
/* Finish writing. */
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 0;
}
int main()
{
RGBBitmap rgbbitmap;
rgbbitmap.height = 1600;
rgbbitmap.width = 900;
rgbbitmap.bytes_per_pixel = 1;
rgbbitmap.bytewidth = 1;
RGBPixel rgbpixel;
rgbpixel.blue = 255;
rgbpixel.green = 255;
rgbpixel.red = 255;
rgbbitmap.pixels = &rgbpixel;
save_png_to_file(&rgbbitmap, "abc.bmp");
return 0;
}
I'm actually getting that error on the marked line.
Any brilliant suggestion, please?
After setting
rgbbitmap.bytes_per_pixel = 3; // was 1 which is probably wrong
Surely
uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel);
must be
uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel*bitmap->width);
, i.e. each row must hold enough space for all bytes in that row ;-), which is 3 bytes per pixel times pixels in a row. You actually compute that value, bytes_per_row, but for some reason fail to use it.
This
uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel);
looks suspicious - I don't know how png_malloc() works, but I guess you're allocating sizeof(uint8_t) * BytesPerPixel (which is probably sizeof(uint8_t) * 4) instead of sizeof(uint8_t) * NumberOfPixels (which should be considerably larger).
I think your rgbbitmap.bytes_per_pixel = 1 is wrong.
If your work in RGB8 your bytes_per_pixel = 3, in RGBA8 is 4 and so on.

C++ How to create a bitmap file

I am trying to figure out how to create a bitmap file in C++ VS. Currently I have taken in the file name and adding the ".bmp" extension to create the file. I want to know how I could change the pixels of the file by making it into different colors or patterns (ie. like a checkerboard) This is my function that I have and I believe that I have to send 3 different Bytes at a time in order to establish the color of the pixel.
void makeCheckerboardBMP(string fileName, int squaresize, int n) {
ofstream ofs;
ofs.open(fileName + ".bmp");
writeHeader(ofs, n, n);
for(int row = 0; row < n; row++) {
for(int col = 0; col < n; col++) {
if(col % 2 == 0) {
ofs << 0;
ofs << 0;
ofs << 0;
} else {
ofs << 255;
ofs << 255;
ofs << 255;
}
}
}
}
void writeHeader(ostream& out, int width, int height){
if (width % 4 != 0) {
cerr << "ERROR: There is a windows-imposed requirement on BMP that the width be a
multiple of 4.\n";
cerr << "Your width does not meet this requirement, hence this will fail. You can fix
this\n";
cerr << "by increasing the width to a multiple of 4." << endl;
exit(1);
}
BITMAPFILEHEADER tWBFH;
tWBFH.bfType = 0x4d42;
tWBFH.bfSize = 14 + 40 + (width*height*3);
tWBFH.bfReserved1 = 0;
tWBFH.bfReserved2 = 0;
tWBFH.bfOffBits = 14 + 40;
BITMAPINFOHEADER tW2BH;
memset(&tW2BH,0,40);
tW2BH.biSize = 40;
tW2BH.biWidth = width;
tW2BH.biHeight = height;
tW2BH.biPlanes = 1;
tW2BH.biBitCount = 24;
tW2BH.biCompression = 0;
out.write((char*)(&tWBFH),14);
out.write((char*)(&tW2BH),40);
}
These are the two functions I am using for my code (one greyscale, one RGB saving).
Might give you a hint whats going wrong.
Note: they are done to work, not to be efficient.
void SaveBitmapToFile( BYTE* pBitmapBits, LONG lWidth, LONG lHeight,WORD wBitsPerPixel, LPCTSTR lpszFileName )
{
RGBQUAD palette[256];
for(int i = 0; i < 256; ++i)
{
palette[i].rgbBlue = (byte)i;
palette[i].rgbGreen = (byte)i;
palette[i].rgbRed = (byte)i;
}
BITMAPINFOHEADER bmpInfoHeader = {0};
// Set the size
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
// Bit count
bmpInfoHeader.biBitCount = wBitsPerPixel;
// Use all colors
bmpInfoHeader.biClrImportant = 0;
// Use as many colors according to bits per pixel
bmpInfoHeader.biClrUsed = 0;
// Store as un Compressed
bmpInfoHeader.biCompression = BI_RGB;
// Set the height in pixels
bmpInfoHeader.biHeight = lHeight;
// Width of the Image in pixels
bmpInfoHeader.biWidth = lWidth;
// Default number of planes
bmpInfoHeader.biPlanes = 1;
// Calculate the image size in bytes
bmpInfoHeader.biSizeImage = lWidth* lHeight * (wBitsPerPixel/8);
BITMAPFILEHEADER bfh = {0};
// This value should be values of BM letters i.e 0x4D42
// 0x4D = M 0×42 = B storing in reverse order to match with endian
bfh.bfType = 'B'+('M' << 8);
// <<8 used to shift ‘M’ to end
// Offset to the RGBQUAD
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + sizeof(RGBQUAD) * 256;
// Total size of image including size of headers
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;
// Create the file in disk to write
HANDLE hFile = CreateFile( lpszFileName,GENERIC_WRITE, 0,NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if( !hFile ) // return if error opening file
{
return;
}
DWORD dwWritten = 0;
// Write the File header
WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
// Write the bitmap info header
WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL );
// Write the palette
WriteFile( hFile, &palette[0], sizeof(RGBQUAD) * 256, &dwWritten, NULL );
// Write the RGB Data
if(lWidth%4 == 0)
{
WriteFile( hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwWritten, NULL );
}
else
{
char* empty = new char[ 4 - lWidth % 4];
for(int i = 0; i < lHeight; ++i)
{
WriteFile( hFile, &pBitmapBits[i * lWidth], lWidth, &dwWritten, NULL );
WriteFile( hFile, empty, 4 - lWidth % 4, &dwWritten, NULL );
}
}
// Close the file handle
CloseHandle( hFile );
}
void SaveBitmapToFileColor( BYTE* pBitmapBits, LONG lWidth, LONG lHeight,WORD wBitsPerPixel, LPCTSTR lpszFileName )
{
BITMAPINFOHEADER bmpInfoHeader = {0};
// Set the size
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
// Bit count
bmpInfoHeader.biBitCount = wBitsPerPixel;
// Use all colors
bmpInfoHeader.biClrImportant = 0;
// Use as many colors according to bits per pixel
bmpInfoHeader.biClrUsed = 0;
// Store as un Compressed
bmpInfoHeader.biCompression = BI_RGB;
// Set the height in pixels
bmpInfoHeader.biHeight = lHeight;
// Width of the Image in pixels
bmpInfoHeader.biWidth = lWidth;
// Default number of planes
bmpInfoHeader.biPlanes = 1;
// Calculate the image size in bytes
bmpInfoHeader.biSizeImage = lWidth* lHeight * (wBitsPerPixel/8);
BITMAPFILEHEADER bfh = {0};
// This value should be values of BM letters i.e 0x4D42
// 0x4D = M 0×42 = B storing in reverse order to match with endian
bfh.bfType = 'B'+('M' << 8);
// <<8 used to shift ‘M’ to end
// Offset to the RGBQUAD
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
// Total size of image including size of headers
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;
// Create the file in disk to write
HANDLE hFile = CreateFile( lpszFileName,GENERIC_WRITE, 0,NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
if( !hFile ) // return if error opening file
{
return;
}
DWORD dwWritten = 0;
// Write the File header
WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
// Write the bitmap info header
WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL );
// Write the palette
//WriteFile( hFile, &palette[0], sizeof(RGBQUAD) * 256, &dwWritten, NULL );
// Write the RGB Data
if(lWidth%4 == 0)
{
WriteFile( hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwWritten, NULL );
}
else
{
char* empty = new char[ 4 - lWidth % 4];
for(int i = 0; i < lHeight; ++i)
{
WriteFile( hFile, &pBitmapBits[i * lWidth], lWidth, &dwWritten, NULL );
WriteFile( hFile, empty, 4 - lWidth % 4, &dwWritten, NULL );
}
}
// Close the file handle
CloseHandle( hFile );
}
Given your writeHeader is properly implemented this is almost correct. You need to fix 2 issues though:
You are writing one int per color channel. This should be one byte instead. You need to cast the literals to unsigned char.
Scanlines in bitmaps need to be DWORD-aligned. After your inner loop over col you need to write additional bytes to account for this, unless the size in bytes of the row is a multiple of four.
You need to force the output to be written in binary format, not text, this is chosen when you open your file/create your stream and to output all the values as bytes, not integers, this can be done in a number of ways possibly the easiest being write chr(0) or chr(255) - you also need to start your file with a header section - there are a number of formats that make this too long to go into in an answer here - some of them are down to preference as much as anything. There is a good summary in Wikipedia.
Basically you have to inform the receiving applications which format you are using, the number of rows, columns and how the colours are stored.

Lib Png: Png24 To Png32 and Vice-Versa

I recently solved the Bitmap problem in my last post. Now I'm back with another Image problem. This time it's PNG.
I'm using LibPng to read and write PNG files. I have a struct that holds BGRA information of each Pixel. I know that the pixels are stored up right and in RGBA format. So I specified swapping of the B and the R. That works fine. I think I'm somehow flipping the image but I'm not quite sure.
My problem comes in when I try to convert 24 bit PNG to 32 bit PNG and vice-versa. Currently, I can do 24 to 24 and 32 to 32 just fine.
Can you guys look over my code and tell me what I'm doing wrong when attempting to convert from 24 to 32?
The below code works for loading and writing the same PNG back to the disk. I made sure to include everything in this one file so that you guys can compile it and see if necessary.
#include <iostream>
#include <vector>
#include <fstream>
#include <stdexcept>
#include "Libraries/LibPng/Include/png.h"
typedef union RGB
{
uint32_t Color;
struct
{
unsigned char B, G, R, A;
} RGBA;
} *PRGB;
std::vector<RGB> Pixels;
uint32_t BitsPerPixel, width, height;
int bitdepth, colortype, interlacetype, channels;
void ReadFromStream(png_structp PngPointer, png_bytep Data, png_size_t Length) //For reading using ifstream rather than FILE*
{
std::ifstream *Stream = (std::ifstream*)png_get_io_ptr(PngPointer);
Stream->read((char*)Data, Length);
}
void WriteToStream(png_structp PngPointer, png_bytep Data, png_size_t Length) //For writing using ofstream rather than FILE*
{
std::ofstream *Stream = (std::ofstream*)png_get_io_ptr(PngPointer);
Stream->write((char*)Data, Length);
}
void Load(const char* FilePath)
{
std::fstream hFile(FilePath, std::ios::in | std::ios::binary);
if (!hFile.is_open()){throw std::invalid_argument("File Not Found.");}
unsigned char Header[8] = {0};
hFile.read(reinterpret_cast<char*>(&Header), sizeof(Header));
if (png_sig_cmp(Header, 0, 8))
{
hFile.close();
throw std::invalid_argument("Error: Invalid File Format. Required: Png.");
}
png_structp PngPointer = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!PngPointer)
{
hFile.close();
throw std::runtime_error("Error: Cannot Create Read Structure.");
}
png_infop InfoPointer = png_create_info_struct(PngPointer);
if (!InfoPointer)
{
hFile.close();
png_destroy_read_struct(&PngPointer, nullptr, nullptr);
throw std::runtime_error("Error: Cannot Create InfoPointer Structure.");
}
png_infop EndInfo = png_create_info_struct(PngPointer);
if (!EndInfo)
{
hFile.close();
png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
throw std::runtime_error("Error: Cannot Create EndInfo Structure.");
}
if (setjmp(png_jmpbuf(PngPointer)))
{
hFile.close();
png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
throw std::runtime_error("Error: Cannot Set Jump Pointer.");
}
png_set_sig_bytes(PngPointer, sizeof(Header));
png_set_read_fn(PngPointer, reinterpret_cast<void*>(&hFile), ReadFromStream);
png_read_info(PngPointer, InfoPointer);
//This is where I start getting the info and storing it..
channels = png_get_channels(PngPointer, InfoPointer);
png_get_IHDR(PngPointer, InfoPointer, &width, &height, &bitdepth, &colortype, &interlacetype, nullptr, nullptr);
png_set_strip_16(PngPointer);
png_set_packing(PngPointer);
switch (colortype)
{
case PNG_COLOR_TYPE_GRAY:
{
png_set_expand(PngPointer);
break;
}
case PNG_COLOR_TYPE_GRAY_ALPHA:
{
png_set_gray_to_rgb(PngPointer);
break;
}
case PNG_COLOR_TYPE_RGB:
{
png_set_bgr(PngPointer);
BitsPerPixel = 24;
break;
}
case PNG_COLOR_TYPE_RGBA:
{
png_set_bgr(PngPointer);
BitsPerPixel = 32;
break;
}
default: png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr); throw std::runtime_error("Error: Png Type not supported."); break;
}
//Store the new data.
png_read_update_info(PngPointer, InfoPointer);
channels = png_get_channels(PngPointer, InfoPointer);
png_get_IHDR(PngPointer, InfoPointer, &width, &height, &bitdepth, &colortype, &interlacetype, nullptr, nullptr);
Pixels.resize(width * height);
std::vector<unsigned char*> RowPointers(height);
unsigned char* BuffPos = reinterpret_cast<unsigned char*>(Pixels.data());
//Set the row pointers to my Pixels vector. This way, the image is stored upright in my vector<BGRA> Pixels.
//I think this is flipping it for some reason :S
for (size_t I = 0; I < height; ++I)
{
RowPointers[I] = BuffPos + (I * width * ((BitsPerPixel > 24) ? 4 : 3));
}
png_read_image(PngPointer, RowPointers.data()); //Get the pixels as BGRA and store it in my struct vector.
png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
hFile.close();
std::cout<<"Loading Parameters..."<<std::endl;
std::cout<<"Bits: "<<BitsPerPixel<<std::endl;
std::cout<<"Depth: "<<bitdepth<<std::endl;
std::cout<<"CType: "<<colortype<<std::endl;
}
void Save(const char* FilePath)
{
std::fstream hFile(FilePath, std::ios::out | std::ios::binary);
if (!hFile.is_open()) {throw std::invalid_argument("Cannot open file for writing.");}
png_structp PngPointer = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!PngPointer)
{
hFile.close();
throw std::runtime_error("Error: Cannot Create Write Structure.");
}
png_infop InfoPointer = png_create_info_struct(PngPointer);
if (!InfoPointer)
{
hFile.close();
png_destroy_write_struct(&PngPointer, nullptr);
throw std::runtime_error("Error: Cannot Create InfoPointer Structure.");
}
if (setjmp(png_jmpbuf(PngPointer)))
{
hFile.close();
png_destroy_write_struct(&PngPointer, &InfoPointer);
throw std::runtime_error("Error: Cannot Set Jump Pointer.");
}
std::cout<<"\nSaving Parameters..."<<std::endl;
std::cout<<"Bits: "<<BitsPerPixel<<std::endl;
std::cout<<"Depth: "<<bitdepth<<std::endl;
std::cout<<"CType: "<<colortype<<std::endl;
//This is where I set all the Information..
png_set_IHDR (PngPointer, InfoPointer, width, height, bitdepth, BitsPerPixel == 24 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
std::vector<unsigned char*> RowPointers(height);
unsigned char* BuffPos = reinterpret_cast<unsigned char*>(Pixels.data());
//Set the Row pointers to my vector<BGRA> Pixels. It should have been stored upright already.
//I think this flips it upside down :S?
for (size_t I = 0; I < height; ++I)
{
RowPointers[I] = BuffPos + (I * width * ((BitsPerPixel > 24) ? 4 : 3));
}
png_set_bgr(PngPointer); //My struct vector holds BGRA and PNG requires RGBA so swap them..
png_set_write_fn(PngPointer, reinterpret_cast<void*>(&hFile), WriteToStream, nullptr);
png_set_rows(PngPointer, InfoPointer, RowPointers.data());
png_write_png(PngPointer, InfoPointer, PNG_TRANSFORM_IDENTITY, NULL);
png_destroy_write_struct(&PngPointer, &InfoPointer);
hFile.close();
}
void SetBitsPerPixel(uint32_t BPP)
{
BitsPerPixel = BPP;
bitdepth = (BPP > 24 ? 8 : 6);
channels = (BPP > 24 ? 4 : 3);
colortype = (BPP > 24 ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB);
}
int main()
{
Load("C:/Images/Png24.png");
SetBitsPerPixel(32);
Save("C:/Images/Output/Png32.png");
}
I load (24 bit PNG made with MS-Paint):
When I save it back as 24, it saves flawlessly. When I attempt to save it back as 32, it looks like:
Try doing the following modifications to your source code. Not tested!
In your Load function change this:
case PNG_COLOR_TYPE_RGB:
{
png_set_bgr(PngPointer);
BitsPerPixel = 24;
break;
}
to this:
case PNG_COLOR_TYPE_RGB:
{
png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
png_set_bgr(PngPointer);
BitsPerPixel = 32;
break;
}
In your Load and Save functions change this:
for (size_t I = 0; I < height; ++I)
{
RowPointers[I] = BuffPos + (I * width * ((BitsPerPixel > 24) ? 4 : 3));
}
to this:
size_t BytesPerLine = width << 2;
unsigned char *ptr = BuffPos;
for (size_t I = 0; I < height; ++I, ptr += BytesPerLine)
RowPointers[I] = ptr;
In your Save function change this:
png_write_png(PngPointer, InfoPointer, PNG_TRANSFORM_IDENTITY, NULL);
to this:
png_write_png(PngPointer, InfoPointer, BitsPerPixel == 24 ? PNG_TRANSFORM_STRIP_FILLER : PNG_TRANSFORM_IDENTITY, NULL);
and in your SetBitsPerPixel function change this line:
bitdepth = (BPP > 24 ? 8 : 6);
to this:
bitdepth = (BPP >= 24 ? 8 : 6);
Actually, I'm not sure why you're doing this, since, as far as I know, 24 and 32 bpp images should have a bitdepth of 8 bits.
NOTE: This modifications are intended for 24 and 32 bpp images, so if you want to use indexed or greyscale images you may need to do some extra modifications. The point is that you should always save pixels in a BGRA buffer (no matter if you load indexed, greyscale or RGB images with no alpha channel).