Alpha blend watermark bmp in image bmp in c++ - c++

I have a task at school to add watermark bmp image into some other bmp image. The task is called alpha blending. I have to insert watermark at specific coordinates which user will set through program parameters on start, as well as alpha value for watermark blending. I am almost succeed, but I am getting small error. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BMP_SIGNATURE_0 0x42
#define BMP_SIGNATURE_1 0x4D
#define BMP_DEPTH 24
#define BMP_HDR_SIZE 24
int isValidBmp(unsigned char *header)
{
if (header == NULL)
return -1;
if ((header[0] == BMP_SIGNATURE_0) && (header[1] == BMP_SIGNATURE_1))
{
if (header[28] != BMP_DEPTH)
return -1;
else
return 0;
}
else
return -1;
}
unsigned long getBmpWidth(unsigned char *header)
{
unsigned long width;
if (header == NULL)
return 0;
width = ((unsigned long)header[21] << 24) |
((unsigned long)header[20] << 16) |
((unsigned long)header[19] << 8) |
(unsigned long)header[18];
return width;
}
unsigned long getBmpHeight(unsigned char *header)
{
unsigned long height;
if (header == NULL)
return 0;
height = ((unsigned long)header[25] << 24) |
((unsigned long)header[24] << 16) |
((unsigned long)header[23] << 8) |
(unsigned long)header[22];
return height;
}
int main(int argc, char *argv[])
{
FILE *fIn, *fOut, *fWaterMark;
unsigned char *mIn, *mOut, *mWaterMark;
unsigned long fsize, zsize;
unsigned long fwidth, fheight, zwidth, zheight;;
fIn = fopen("D:\\Downloads\\image.bmp", "rb");
if (fIn == NULL)
{
printf("ERROR!\n\n");
return 1;
}
fseek(fIn, 0, SEEK_END);
fsize = ftell(fIn);
mIn = (unsigned char *)malloc(fsize);
fseek(fIn, 0, SEEK_SET);
fread(mIn, sizeof(unsigned char), fsize, fIn);
fclose(fIn);
if (isValidBmp(mIn) == -1)
{
printf("ERROR!\n\n");
free(mIn);
return 1;
}
fwidth = getBmpWidth(mIn);
fheight = getBmpHeight(mIn);
if ((fwidth == 0) || (fheight == 0))
{
printf("ERROR!\n\n");
free(mIn);
return 1;
}
fWaterMark = fopen("D:\\Downloads\\watermark.bmp", "rb");
if (fWaterMark == NULL)
{
free(mIn);
printf("ERROR!\n\n");
return 1;
}
fseek(fWaterMark, 0, SEEK_END);
zsize = ftell(fWaterMark);
mWaterMark = (unsigned char *)malloc(zsize);
fseek(fWaterMark, 0, SEEK_SET);
fread(mWaterMark, sizeof(unsigned char), zsize, fWaterMark);
fclose(fWaterMark);
if (isValidBmp(mWaterMark) == -1)
{
printf("ERROR!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}
zwidth = getBmpWidth(mWaterMark);
zheight = getBmpHeight(mWaterMark);
if ((zwidth == 0) || (zheight == 0))
{
printf("ERROR!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}
fOut = fopen("D:\\Downloads\\new_image.bmp", "wb");
if (fOut == NULL)
{
free(mIn);
free(mWaterMark);
printf("ERROR!\n\n");
return 1;
}
mOut = (unsigned char *)malloc(fsize);
fseek(fOut, 0, SEEK_SET);
double alpha = 0.5;
memcpy(mOut, mIn, fsize);
unsigned int index = BMP_HDR_SIZE;
unsigned int x = 200, y = 200;
for (unsigned int i = BMP_HDR_SIZE + x*y; i < x*y + zsize; i++)
{
unsigned char v = ((1 - alpha) * mIn[i]) + mWaterMark[index++];
mOut[i] = v;
}
fwrite(mOut, sizeof(unsigned char), fsize, fOut);
free(mIn);
free(mOut);
fclose(fOut);
return 0;
}
Sample BMP image:

The problem is in your loop over a 2-dimensional array of bitmap points. Change your loop to be a double loop. Also:
You are not using the correct offset for the pixel range.
You need to multiply width times 3 to copy all 3 component colors of a pixels
You should use the rounded off row length to make sure you cover any padding.
Your y is offset from the bottom of the picture, use trueY to have your offset from the top.
It helps to have a different error text for each error to know which error is triggering; I've left it as an exercise to come up with more helpful text.
I voted you up because I think this is an interesting question; I had to dig into the Wikipedia page for BMP files to come up with the final answer.
You were only using half-alpha on the source, but to reproduce that image you shared in your comments, you need to take half-alpha for both the image and the watermark.
Note also to reproduce the image you shared in the comments, x must be 125 and y must be 100. All that said, this code looks like it works:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define BMP_SIGNATURE_0 0x42
#define BMP_SIGNATURE_1 0x4D
#define BMP_DEPTH 24
#define BMP_HDR_SIZE 24
int isValidBmp(unsigned char *header)
{
if (header == NULL)
return -1;
if ((header[0] == BMP_SIGNATURE_0) && (header[1] == BMP_SIGNATURE_1))
{
if (header[28] != BMP_DEPTH)
return -1;
else
return 0;
}
else
return -1;
}
unsigned long getBmpWidth(unsigned char *header)
{
unsigned long width;
if (header == NULL)
return 0;
width = ((unsigned long)header[21] << 24) |
((unsigned long)header[20] << 16) |
((unsigned long)header[19] << 8) |
(unsigned long)header[18];
return width;
}
unsigned long getBmpHeight(unsigned char *header)
{
unsigned long height;
if (header == NULL)
return 0;
height = ((unsigned long)header[25] << 24) |
((unsigned long)header[24] << 16) |
((unsigned long)header[23] << 8) |
(unsigned long)header[22];
return height;
}
unsigned long getPixelOffset(unsigned char *header)
{
unsigned long offset;
if (header == NULL)
return 0;
offset = ((unsigned long)header[13] << 24) |
((unsigned long)header[12] << 16) |
((unsigned long)header[11] << 8) |
(unsigned long)header[10];
return offset;
}
int main(int argc, char *argv[])
{
FILE *fIn, *fOut, *fWaterMark;
unsigned char *mIn, *mOut, *mWaterMark;
unsigned long fsize, zsize;
unsigned long fwidth, fheight, zwidth, zheight;
unsigned long foffset, frow, zoffset, zrow;
fIn = fopen("srcso.bmp", "rb");
if (fIn == NULL)
{
printf("ERROR 1!\n\n");
return 1;
}
fseek(fIn, 0, SEEK_END);
fsize = ftell(fIn);
mIn = (unsigned char *)malloc(fsize);
fseek(fIn, 0, SEEK_SET);
fread(mIn, sizeof(unsigned char), fsize, fIn);
fclose(fIn);
if (isValidBmp(mIn) == -1)
{
printf("ERROR 2!\n\n");
free(mIn);
return 1;
}
fwidth = getBmpWidth(mIn);
fheight = getBmpHeight(mIn);
foffset = getPixelOffset(mIn);
frow = (BMP_DEPTH * fwidth + 31) / 32 * 4;
if ((fwidth == 0) || (fheight == 0))
{
printf("ERROR 3!\n\n");
free(mIn);
return 1;
}
fWaterMark = fopen("wmso.bmp", "rb");
if (fWaterMark == NULL)
{
free(mIn);
printf("ERROR 4!\n\n");
return 1;
}
fseek(fWaterMark, 0, SEEK_END);
zsize = ftell(fWaterMark);
mWaterMark = (unsigned char *)malloc(zsize);
fseek(fWaterMark, 0, SEEK_SET);
fread(mWaterMark, sizeof(unsigned char), zsize, fWaterMark);
fclose(fWaterMark);
if (isValidBmp(mWaterMark) == -1)
{
printf("ERROR 5!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}
zwidth = getBmpWidth(mWaterMark);
zheight = getBmpHeight(mWaterMark);
zoffset = getPixelOffset(mWaterMark);
zrow = (BMP_DEPTH * zwidth + 31) / 32 * 4;
if ((zwidth == 0) || (zheight == 0))
{
printf("ERROR 6!\n\n");
free(mIn);
free(mWaterMark);
return 1;
}
fOut = fopen("new_image.bmp", "wb");
if (fOut == NULL)
{
free(mIn);
free(mWaterMark);
printf("ERROR 7!\n\n");
return 1;
}
mOut = (unsigned char *)malloc(fsize);
fseek(fOut, 0, SEEK_SET);
double alpha = 0.5;
std::copy(mIn, mIn + fsize, mOut);
::free(mIn);
mIn = 0;
unsigned int index = BMP_HDR_SIZE;
unsigned int x = 200, y = 200;
unsigned int trueY = fheight - y - zheight;
for (unsigned int j = 0; j < zheight; ++j) {
for (unsigned int i = 0; i < zwidth*3; ++i) {
const size_t offset = foffset + (j + trueY) * frow + i + x*3;
unsigned char * const offOut = mOut + offset;
unsigned char * const offWM = mWaterMark + zoffset + j * zrow + i;
*offOut *= 1 - alpha;
*offWM *= 1 - alpha;
if ((unsigned int)*offOut + (unsigned int)*offWM < 265)
*offOut += *offWM;
else
*offOut = 255;
}
}
fwrite(mOut, sizeof(unsigned char), fsize, fOut);
fclose(fOut);
::free(mWaterMark);
::free(mOut);
return 0;
}
Note, the if you have an alpha less than .5, you will get strange color artifacts because the algorithm could ping say red but leave blue and green normal making the blue and green seem brighter than they should be. Technically, when the else case happens for the pixel setting, it should really affect the other two pixels by adding more to them to compensate for this effect.

Related

Facing issues trying to decode base64 image

I have a JPEG image, which is represented as a base64 encoded string. I want to save it as a decoded byte array using the Win32 API WriteFile() function.
Because I will use WriteFile(), I need a C string, and I need to know its length, strlen() is bad, because, as I understand, it counts to \0 which could not be the exact end of file. So, I need a function that decodes base64 and returns a char* and outputs the exact byte count.
I have read this answer, and chose code from here (some stuff changed, I marked it):
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char * base64_decode(const unsigned char *src, size_t len,
size_t *out_len)
{
unsigned char dtable[256], *out, *pos, block[4], tmp;
size_t i, count, olen;
int pad = 0;
memset(dtable, 0x80, 256); // CHANGED
for (i = 0; i < sizeof(base64_table) - 1; i++)
dtable[base64_table[i]] = (unsigned char) i;
dtable['='] = 0;
count = 0;
for (i = 0; i < len; i++) {
if (dtable[src[i]] != 0x80)
count++;
}
if (count == 0 || count % 4)
return NULL;
olen = count / 4 * 3;
pos = out = new unsigned char[olen]; // CHANGED
if (out == NULL)
return NULL;
count = 0;
for (i = 0; i < len; i++) {
tmp = dtable[src[i]];
if (tmp == 0x80)
continue;
if (src[i] == '=')
pad++;
block[count] = tmp;
count++;
if (count == 4) {
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
if (pad) {
if (pad == 1)
pos--;
else if (pad == 2)
pos -= 2;
else {
/* Invalid padding */
free(out); // CHANGED
return NULL;
}
break;
}
}
}
*out_len = pos - out;
return out;
}
Usage
unsigned char base[]="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N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hfQhCrJQUniPucfVHkV9QhnqwobIzbpWNOpueQS8rjI4lpy38UIUZYkJVGHRVH74lx5lJs4ep2ZsksoDjctBsEIWr8OZ9pOHqakqv0mG3aC+UuaCW33sd9U3LSTEXY8k/ByELGjbJeFVFWcXmY6GEtLhmPbHO0DS+W3w95WkuhCw2XpymOi0fBx/ZKjq+gQhMxbicuhoEIQqyU//2Q==";
unsigned char *g = base64_decode(base, 2568, &re); // length is appearing when you hover mouse on char[] in Visual Studio
// after call re equals 1921
HANDLE f2 = CreateFile(L"img.jpeg", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD wr2;
WriteFile(f2, g, re, &wr2, 0);
CloseHandle(f2);
The file I am getting is not viewable, the Photos app says it is corrupted. The main problem - it weights 1.87 kb, but should be 2.31 (I download this image from a browser).
What am I doing wrong?
As #IngoLeonhardt pointed out, I should not pass the data:image/jpeg;base64, part to the function. Now it works.

How can reverse a part of an array?

I am writing a program that takes a bitmap file to read into memory. But as I am reading it into memory I am making some changes. First I am inverting the colors of the pixels. I managed to get this working. Now I am trying to flip the image on the Y-Axis. I have tried using two for loops but would end up get segmentation faults and also I didn't like how messy it looked. On my second attempt I found a different approach that's cleaner due to it only using one loop and one condition vs 2 loops and 2 conditions. My code now produces no errors but doesn't perform the intended operation. Is there another algorithm I could possibly try?
Below is some code for part of my program. I am trying to reverse the pixel when I am reading them row by row.
fseek(fin, bfh.offset, SEEK_SET);
Pixel *p = new Pixel[bih.height * bih.width];
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fread(&p[index], 1, sizeof(p[0]), fin);
p[index].blue = 255 - p[index].blue;
p[index].green = 255 - p[index].green;
p[index].red = 255 - p[index].red;
}
uint32_t k = (bih.width * i) - 1;
uint32_t c = 0 + (i * bih.width);
if ( i == 0) {
k = bih.width - 1;
}
while( c < k)
{
temp = p[c];
p[c] = p[k];
p[k] = temp;
c++;
k--;
}
fseek(fin, padding_bytes, SEEK_CUR);
}
fclose(fin);
Below is my whole program if needed.
#include <cstdint>
#include <cstdio>
#pragma pack(push, 2)
struct BitmapFileHeader {
uint16_t type;
uint32_t size;
uint16_t reserved_1;
uint16_t reserved_2;
uint32_t offset;
};
struct BitmapInfoHeader {
uint32_t size;
uint32_t width;
uint32_t height;
uint16_t planes;
uint16_t bitcount;
uint32_t compression;
uint32_t imagesize;
uint32_t x_pixels_per_meter;
uint32_t y_pixels_per_meter;
uint32_t color_used;
uint32_t color_important;
};
#pragma pack(pop)
struct Pixel {
uint8_t blue;
uint8_t green;
uint8_t red;
};
int main(int argc, char* argv[])
{
if(argc != 3) {
printf("Usage : %s input_file output_file\n", argv[0]);
return 1;
}
FILE *fin;
FILE *fout;
BitmapFileHeader bfh;
BitmapInfoHeader bih;
Pixel temp;
fin = fopen(argv[1], "rb");
if (nullptr == fin) {
perror(argv[1]);
return -1;
}
if (sizeof(BitmapFileHeader) != fread(
&bfh,
1,
sizeof(bfh),
fin
)) {
printf("Unable to read bitmap file header. \n");
return -2;
}
if (sizeof(BitmapInfoHeader) != fread(
&bih,
1,
sizeof(bih),
fin
)) {
printf("Unable to read bitmap info header. \n");
return -3;
}
printf("Size of File Header = %lu\n", sizeof(BitmapFileHeader));
int8_t first = (bfh.type >> 8) & 0xff;
int8_t second = bfh.type & 0xff;
if ( (first != 'M') && (second != 'B') ){
printf("Input file is not a Bitmap file. \n");
return -4;
}
printf("File type = %c%c\n", first, second);
printf("File size = %u\n", bfh.size);
printf("File offset = %u\n", bfh.offset);
printf("File width = %u\n", bih.width);
printf("Info size = %u\n", bih.size);
uint32_t padding_bytes = 0;
uint32_t row_bytes_final = bih.width * sizeof(Pixel);
uint32_t row_bytes_initial = row_bytes_final;
do{
uint32_t rem = row_bytes_final % 4;
if (rem != 0) {
row_bytes_final += 1;
}
padding_bytes = row_bytes_final - row_bytes_initial;
} while( (row_bytes_final % 4) != 0);
fseek(fin, bfh.offset, SEEK_SET);
Pixel *p = new Pixel[bih.height * bih.width];
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fread(&p[index], 1, sizeof(p[0]), fin);
p[index].blue = 255 - p[index].blue;
p[index].green = 255 - p[index].green;
p[index].red = 255 - p[index].red;
}
uint32_t k = (bih.width * i) - 1;
uint32_t c = 0 + (i * bih.width);
if ( i == 0) {
k = bih.width - 1;
}
while( (c * bih.width) < (k * bih.width))
{
temp = p[c];
p[c] = p[k];
p[k] = temp;
c++;
k--;
}
fseek(fin, padding_bytes, SEEK_CUR);
}
fclose(fin);
fout = fopen(argv[2], "wb");
if(nullptr == fout) {
perror(argv[2]);
return -5;
}
if( sizeof(BitmapFileHeader) != fwrite(
&bfh,
1,
sizeof(bfh),
fout
)) {
printf("Unable to write bitmap file header.\n");
return -6;
}
if( sizeof(BitmapInfoHeader) != fwrite(
&bih,
1,
sizeof(bih),
fout
)) {
printf("Unable to write bitmap info header.\n");
return -7;
}
fseek(fout, bfh.offset, SEEK_SET);
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fwrite(&p[index], 1, sizeof(p[0]), fout);
}
fseek(fout, padding_bytes, SEEK_CUR);
}
if (padding_bytes > 0) {
fseek(fout, -1, SEEK_CUR);
fputc('\0', fout);
}
fclose(fout);
delete[] p;
return 0;
}
You got the bounds wrong, it should be c = i * bih.width; k = (i + 1) * bih.width - 1;.
You can also use std::reverse to do this:
std::reverse(p + i * bih.width, p + (i + 1) * bih.width); // Exclusive end, so no -1

Output File is missing padding at the end of the file for a Bitmap. Why is this?

In this program I am trying to use C-style files for both reading and writing these files. I am also dynamically allocating the memory onto the heap using new() and delete in order to then write that block of memory to another file. For some reason when I preform a hex hump the files are almost the same. Only the ending bytes are different. Here are my dumps.
This is for my input file. The output should have the same data.
This is my output file. From the dump you can see that its different at the end of the file.
Why does this happen if I use fseek() to skip the padding?
#include <cstdint>
#include <cstdio>
#pragma pack(push, 2)
struct BitmapFileHeader {
uint16_t type;
uint32_t size;
uint16_t reserved_1;
uint16_t reserved_2;
uint32_t offset;
};
struct BitmapInfoHeader {
uint32_t size;
uint32_t width;
uint32_t height;
uint16_t planes;
uint16_t bitcount;
uint32_t compression;
uint32_t imagesize;
uint32_t x_pixels_per_meter;
uint32_t y_pixels_per_meter;
uint32_t color_used;
uint32_t color_important;
};
#pragma pack(pop)
struct Pixel {
uint8_t blue;
uint8_t green;
uint8_t red;
};
int main(int argc, char* argv[])
{
if(argc != 3) {
printf("Usage : %s input_file output_file\n", argv[0]);
return 1;
}
FILE *fin;
FILE *fout;
BitmapFileHeader bfh;
BitmapInfoHeader bih;
fin = fopen(argv[1], "rb");
if (nullptr == fin) {
perror(argv[1]);
return -1;
}
if (sizeof(BitmapFileHeader) != fread(
&bfh,
1,
sizeof(bfh),
fin
)) {
printf("Unable to read bitmap file header. \n");
return -2;
}
if (sizeof(BitmapInfoHeader) != fread(
&bih,
1,
sizeof(bih),
fin
)) {
printf("Unable to read bitmap info header. \n");
return -3;
}
printf("Size of File Header = %lu\n", sizeof(BitmapFileHeader));
int8_t first = (bfh.type >> 8) & 0xff;
int8_t second = bfh.type & 0xff;
if ( (first != 'M') && (second != 'B') ){
printf("Input file is not a Bitmap file. \n");
return -4;
}
printf("File type = %c%c\n", first, second);
printf("File size = %u\n", bfh.size);
printf("File offset = %u\n", bfh.offset);
printf("File width = %u\n", bih.width);
printf("Info size = %u\n", bih.size);
uint32_t padding_bytes = 0;
uint32_t row_bytes_final = bih.width * sizeof(Pixel);
uint32_t row_bytes_initial = row_bytes_final;
do{
uint32_t rem = row_bytes_final % 4;
if (rem != 0) {
row_bytes_final += 1;
}
padding_bytes = row_bytes_final - row_bytes_initial;
} while( (row_bytes_final % 4) != 0);
fseek(fin, bfh.offset, SEEK_SET);
Pixel *p = new Pixel[bih.height * bih.width];
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fread(&p[index], 1, sizeof(p[0]), fin);
}
fseek(fin, padding_bytes, SEEK_CUR);
}
fclose(fin);
fout = fopen(argv[2], "wb");
if(nullptr == fout) {
perror(argv[2]);
return -5;
}
if( sizeof(BitmapFileHeader) != fwrite(
&bfh,
1,
sizeof(bfh),
fout
)) {
printf("Unable to write bitmap file header.\n");
return -6;
}
if( sizeof(BitmapInfoHeader) != fwrite(
&bih,
1,
sizeof(bih),
fout
)) {
printf("Unable to write bitmap info header.\n");
return -7;
}
fseek(fout, bfh.offset, SEEK_SET);
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fwrite(&p[index], 1, sizeof(p[0]), fout);
}
fseek(fout, padding_bytes, SEEK_CUR);
}
fclose(fout);
delete p;
//fseek(fin, bfh.offset, SEEK_SET);
//Pixel p;
//fread(&p, 1, sizeof(p), fin);
//printf("R = %u, G = %u, B = %u\n", p.red, p.green, p.blue);
return 0;
}
Seeking to a position off the end of the file does not automatically pad it. Padding bytes will only be written on a subsequent write.
You can force this last padding to be written after your loop finishes as follows:
if (padding_bytes > 0) {
fseek(fout, -1, SEEK_CUR);
fputc('\0', fout);
}
By the way, you are using the wrong delete for your array. Instead use array-delete (corresponding to the array-alloc):
delete[] p;

Distorted Bitmap File

I am writing a program that copies a .bmp file and write it to another file to manipulate it( mirror and invert colors specifically). The .bmp that is written comes out distorted. Does anyone see any problems? I've been up for hours working on this and my brain is fried at this point. Any help would be greatly appreciated.
#include <cstdint>
#include <cstdio>
#pragma pack(push, 2)
struct BitmapFileHeader {
uint16_t type;
uint32_t size;
uint16_t reserved_1;
uint16_t reserved_2;
uint32_t offset;
};
struct BitmapInfoHeader {
uint32_t size;
uint32_t width;
uint32_t height;
uint16_t planes;
uint16_t bitcount;
uint32_t compression;
uint32_t imagesize;
uint32_t x_pixels_per_meter;
uint32_t y_pixels_per_meter;
uint32_t color_used;
uint32_t color_important;
};
#pragma pack(pop)
struct Pixel {
uint8_t blue;
uint8_t green;
uint8_t red;
};
int main(int argc, char* argv[])
{
if(argc != 3) {
printf("Usage : %s input_file output_file\n", argv[0]);
return 1;
}
FILE *fin;
FILE *fout;
BitmapFileHeader bfh;
BitmapInfoHeader bih;
fin = fopen(argv[1], "rb");
if (nullptr == fin) {
perror(argv[1]);
return -1;
}
if (sizeof(BitmapFileHeader) != fread(&bfh, 1, sizeof(bfh), fin)) {
printf("Unable to read bitmap file header. \n");
return -2;
}
if (sizeof(BitmapInfoHeader) != fread(&bih, 1, sizeof(bih), fin)) {
printf("Unable to read bitmap info header. \n");
return -3;
}
printf("Size of File Header = %lu\n", sizeof(BitmapFileHeader));
int8_t first = (bfh.type >> 8) & 0xff;
int8_t second = bfh.type & 0xff;
if ( (first != 'M') && (second != 'B') ){
printf("Input file is not a Bitmap file. \n");
return -4;
}
printf("File type = %c%c\n", first, second);
printf("File size = %u\n", bfh.size);
printf("File offset = %u\n", bfh.offset);
printf("File width = %u\n", bih.width);
printf("Info size = %u\n", bih.size);
uint32_t padding_bytes = 0;
uint32_t row_bytes_final = bih.width * sizeof(Pixel);
uint32_t row_bytes_initial = row_bytes_final;
do{
uint32_t rem = row_bytes_final % 4;
if (rem != 0) {
row_bytes_final += 1;
}
padding_bytes = row_bytes_final - row_bytes_initial;
} while( (row_bytes_final % 4) != 0);
fseek(fin, bfh.offset, SEEK_SET);
Pixel *p = new Pixel[bih.height * bih.width];
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fread(&p[index], 1, sizeof(Pixel), fin);
}
if (padding_bytes > 0) {
fseek(fin, -1, SEEK_CUR);
fputc('\0', fin);
}
// fseek(fin, padding_bytes, SEEK_CUR);
}
fclose(fin);
fout = fopen(argv[2], "wb");
if(nullptr == fout) {
perror(argv[2]);
return -5;
}
if(sizeof(BitmapFileHeader) != fwrite(&bfh, 1, sizeof(bfh), fout)) {
printf("Unable to write bitmap file header.\n");
return -6;
}
if(sizeof(BitmapInfoHeader) != fwrite(&bih, 1, sizeof(bih), fout)) {
printf("Unable to write bitmap info header.\n");
return -7;
}
fseek(fout, bfh.offset, SEEK_SET);
for (uint32_t i = 0; i < bih.height; i++) {
for (uint32_t j = 0; j < bih.width; j++) {
uint32_t index = i * bih.width + j;
fwrite(&p[index], 1, sizeof(Pixel), fout);
}
if (padding_bytes > 0) {
fseek(fout, -1, SEEK_CUR);
fputc('\0', fout);
}
// fseek(fout, padding_bytes, SEEK_CUR);
}
fclose(fout);
delete[] p;
return 0;
}
for (uint32_t i = 0; i < bih.height; i++){
for (uint32_t j = 0; j < bih.width; j++){
uint32_t index = i * bih.width + j;
fread(&p[index], 1, sizeof(Pixel), fin);
}
//if (padding_bytes > 0) {
//fseek(fin, -1, SEEK_CUR);
//for(int kk=0;kk<padding_bytes;kk++)
//fputc('\0', fin);
//}
fseek(fin, padding_bytes, SEEK_CUR); //ok
}
//.....
for (uint32_t i = 0; i < bih.height; i++){
for (uint32_t j = 0; j < bih.width; j++){
uint32_t index = i * bih.width + j;
fwrite(&p[index], 1, sizeof(Pixel), fout);
}
//if (padding_bytes > 0){
// fseek(fout, -1, SEEK_CUR);
// fputc('\0', fout);
//}
//fseek(fout, padding_bytes, SEEK_CUR);
for (int t = 0; t < padding_bytes; t++) fputc('\0', fout); //ok
}

cudaMemcpy - seems to not work properly

I am trying to copy data from host to device in my GPU greyscale filter program. However, there is some kind of problem because when I try to do so, nothing happens. Probably I have some mistakes in my code but compiler doesn't show any errors. I need to copy variables d_bufferRGB into GPU, process it and return it in d_new_bufferRGB in order to save it with function save_bmp();
EDIT 1: implemented CUDA error checking in main()
It says there is invalid argument in this line cudaMemcpy(d_bufferRGB, bufferRGB, size, cudaMemcpyHostToDevice)
HERE is the code >>>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <cuda_runtime.h>
#include <cuda.h>
#include "device_launch_parameters.h"
#include <iostream>
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true)
{
if (code != cudaSuccess)
{
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
int width, heigth;
long size;
long *d_size;
RGBTRIPLE *bufferRGB, *new_bufferRGB;
RGBTRIPLE *d_bufferRGB, *d_new_bufferRGB;
void load_bmp(RGBTRIPLE **bufferRGB, int *width, int *heigth, const char *file_name)
{
BITMAPFILEHEADER bmp_file_header;
BITMAPINFOHEADER bmp_info_header;
FILE *file;
file = fopen(file_name, "rb");
fread(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file);
fread(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file);
*width = bmp_info_header.biWidth;
*heigth = bmp_info_header.biHeight;
size = (bmp_file_header.bfSize - bmp_file_header.bfOffBits);
std::cout << "velkost nacitanych pixelov je " << size <<'\n';
int x, y;
*bufferRGB = (RGBTRIPLE *)malloc(*width* *heigth * 4);
fseek(file, bmp_file_header.bfOffBits - sizeof(bmp_file_header) - sizeof(bmp_info_header), SEEK_CUR);
for (y = 0; y < *heigth; y++)
{
for (x = 0; x < *width; x++)
{
(*bufferRGB)[(y * *width + x)].rgbtBlue = fgetc(file);
(*bufferRGB)[(y * *width + x)].rgbtGreen = fgetc(file);
(*bufferRGB)[(y * *width + x)].rgbtRed = fgetc(file);
}
for (x = 0; x < (4 - (3 * *width) % 4) % 4; x++)
fgetc(file);
}
fclose(file);
}
void save_bmp(RGBTRIPLE *bufferRGB, const char *new_name, const char *old_name)
{
BITMAPFILEHEADER bmp_file_header;
BITMAPINFOHEADER bmp_info_header;
FILE *file;
file = fopen(old_name, "rb");
fread(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file);
fread(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file);
fclose(file);
file = fopen(new_name, "wb");
fwrite(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file);
fwrite(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file);
fseek(file, bmp_file_header.bfOffBits - sizeof(bmp_file_header) - sizeof(bmp_info_header), SEEK_CUR);
int alligment_x = (4 - (3 * width) % 4) % 4;
unsigned char *to_save = (unsigned char *)malloc((width * 3 + alligment_x)*heigth);
unsigned int index = 0;
int x, y;
for (y = 0; y < heigth; y++)
{
for (x = 0; x < width; x++)
{
to_save[index++] = bufferRGB[(y * width + x)].rgbtBlue;
to_save[index++] = bufferRGB[(y * width + x)].rgbtGreen;
to_save[index++] = bufferRGB[(y * width + x)].rgbtRed;
}
for (x = 0; x < alligment_x; x++)
to_save[index++] = 0;
}
std::cout << "velkost na ulozenie je " << sizeof(&to_save) << '\n';
fwrite(to_save, (width * 3 + alligment_x)*heigth, 1, file);
fclose(file);
free(to_save);
}
__global__ void CUDA_filter_grayscale(const RGBTRIPLE *d_bufferRGB, RGBTRIPLE *d_new_bufferRGB, long *d_size)
{
int idx = blockIdx.x*blockDim.x + threadIdx.x;
BYTE grayscale;
if (idx < *d_size)
{
grayscale = ((d_bufferRGB[idx].rgbtRed + d_bufferRGB[idx].rgbtGreen + d_bufferRGB[idx].rgbtBlue) / 3);
d_new_bufferRGB[idx].rgbtRed = grayscale;
d_new_bufferRGB[idx].rgbtGreen = grayscale;
d_new_bufferRGB[idx].rgbtBlue = grayscale;
}
}
int main()
{
gpuErrchk(cudaMalloc(&d_new_bufferRGB, width*heigth * 4));
gpuErrchk(cudaMalloc(&d_bufferRGB, width*heigth * 4));
gpuErrchk(cudaMalloc(&d_size, sizeof(size)));
load_bmp(&bufferRGB, &width, &heigth, "test.bmp"); //tu je vztvoreny a naplneny smernik *buffer_RGB
gpuErrchk(cudaMemcpy(d_size, &size, sizeof(size), cudaMemcpyHostToDevice));
gpuErrchk(cudaMemcpy(d_bufferRGB, bufferRGB, size, cudaMemcpyHostToDevice));
CUDA_filter_grayscale << <32, 512 >> > (d_bufferRGB, d_new_bufferRGB, d_size); //size of kernel dont bother me for now
gpuErrchk(cudaMemcpy(new_bufferRGB, d_new_bufferRGB, size, cudaMemcpyDeviceToHost));
save_bmp(new_bufferRGB, "filter_grayscale_GPU.bmp", "test.bmp");
}
It's killing my brain for several days, plese help me with this.
So, with significant help obtained from #Robert Crovella i had finished my code. I also made some extra features like dynamic kernel allocation as a free gift for internet users. Code is fully functional for BMP ver. 3 from Microsoft(one can create some in Paint). I've tried to upload some image but it can be max 2MB big, which is not enough for true color depth. When compiling, there is error of null pointer but the program is created and stored in project Debug folder. When you run it with an image in the folder, it works without problem.
The problem with code above are >
1, uninicialised new_bufferRGB
2, load function do not provide variables sooner then I use them
3, mistakes in cudaMemcpy function
SO, HERE IS THE CODE >>>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <cuda_runtime.h>
#include <cuda.h>
#include "device_launch_parameters.h"
#include <iostream>
int width, heigth;
long size;
long *d_size;
RGBTRIPLE *bufferRGB, *new_bufferRGB;
RGBTRIPLE *d_bufferRGB, *d_new_bufferRGB;
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true)
{
if (code != cudaSuccess)
{
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
//if (abort) exit(code);
}
}
void load_bmp(RGBTRIPLE **bufferRGB, int *width, int *heigth, const char *file_name)
{
BITMAPFILEHEADER bmp_file_header;
BITMAPINFOHEADER bmp_info_header;
FILE *file;
file = fopen(file_name, "rb");
fread(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file);
fread(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file);
*width = bmp_info_header.biWidth;
*heigth = bmp_info_header.biHeight;
size = (bmp_file_header.bfSize - bmp_file_header.bfOffBits);
std::cout << "size of loaded pixels is " << size << '\n';
int x, y;
*bufferRGB = (RGBTRIPLE *)malloc(*width* *heigth * 4);
fseek(file, bmp_file_header.bfOffBits - sizeof(bmp_file_header) - sizeof(bmp_info_header), SEEK_CUR);
for (y = 0; y < *heigth; y++)
{
for (x = 0; x < *width; x++)
{
(*bufferRGB)[(y * *width + x)].rgbtBlue = fgetc(file);
(*bufferRGB)[(y * *width + x)].rgbtGreen = fgetc(file);
(*bufferRGB)[(y * *width + x)].rgbtRed = fgetc(file);
}
for (x = 0; x < (4 - (3 * *width) % 4) % 4; x++)
fgetc(file);
}
fclose(file);
}
void save_bmp(RGBTRIPLE *bufferRGB, const char *new_name, const char *old_name)
{
BITMAPFILEHEADER bmp_file_header;
BITMAPINFOHEADER bmp_info_header;
FILE *file;
file = fopen(old_name, "rb");
fread(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file);
fread(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file);
fclose(file);
file = fopen(new_name, "wb");
fwrite(&bmp_file_header, sizeof(BITMAPFILEHEADER), 1, file);
fwrite(&bmp_info_header, sizeof(BITMAPINFOHEADER), 1, file);
fseek(file, bmp_file_header.bfOffBits - sizeof(bmp_file_header) - sizeof(bmp_info_header), SEEK_CUR);
int alligment_x = (4 - (3 * width) % 4) % 4;
unsigned char *to_save = (unsigned char *)malloc((width * 3 + alligment_x)*heigth);
unsigned int index = 0;
int x, y;
for (y = 0; y < heigth; y++)
{
for (x = 0; x < width; x++)
{
to_save[index++] = bufferRGB[(y * width + x)].rgbtBlue;
to_save[index++] = bufferRGB[(y * width + x)].rgbtGreen;
to_save[index++] = bufferRGB[(y * width + x)].rgbtRed;
}
for (x = 0; x < alligment_x; x++)
to_save[index++] = 0;
}
fwrite(to_save, (width * 3 + alligment_x)*heigth, 1, file);
fclose(file);
free(to_save);
}
__global__ void CUDA_filter_grayscale(const RGBTRIPLE *d_bufferRGB, RGBTRIPLE *d_new_bufferRGB, long *d_size)
{
int idx = blockIdx.x*blockDim.x + threadIdx.x;
BYTE grayscale;
if (idx < *d_size)
{
grayscale = ((d_bufferRGB[idx].rgbtRed + d_bufferRGB[idx].rgbtGreen + d_bufferRGB[idx].rgbtBlue) / 3);
d_new_bufferRGB[idx].rgbtRed = grayscale;
d_new_bufferRGB[idx].rgbtGreen = grayscale;
d_new_bufferRGB[idx].rgbtBlue = grayscale;
}
}
int main()
{
// load to have all variables reachable and loaded
load_bmp(&bufferRGB, &width, &heigth, "test.bmp");
// inicialise buffer for copy of proccesed image from device to host
new_bufferRGB = (RGBTRIPLE *)malloc(width* heigth * 4);
//inicializing variables on GPU
gpuErrchk(cudaMalloc(&d_new_bufferRGB, width*heigth * 4));
gpuErrchk(cudaMalloc(&d_bufferRGB, width*heigth * 4));
gpuErrchk(cudaMalloc(&d_size, sizeof(size)));
// copying variables to GPU
gpuErrchk(cudaMemcpy(d_size, &size, sizeof(size), cudaMemcpyHostToDevice));
gpuErrchk(cudaMemcpy(d_bufferRGB, bufferRGB, size, cudaMemcpyHostToDevice));
// find out the kernel size, number of threads depends on your GPU max number of threads
int numbThreads = 1024;
int numbBlocks = (width*heigth) / numbThreads;
if (((width*heigth) % numbThreads)>0) numbBlocks++;
CUDA_filter_grayscale <<<numbBlocks, numbThreads >>> (d_bufferRGB, d_new_bufferRGB, d_size);
//copy result from device to host
gpuErrchk(cudaMemcpy(new_bufferRGB, d_new_bufferRGB, size, cudaMemcpyDeviceToHost));
//save result
save_bmp(new_bufferRGB, "filter_grayscale_GPU.bmp", "test.bmp");
return 0;
}