My taks is to restore an mp3 file, wich is coded bit-per-bit in a PNG file. I got the right bits from the PNG RGB data (per pixel) in a vector. I'm using C++.
I have to go through the png file and read the RGB data of a pixel: then I have 3 decimal values. From binary representation of the decimal values, I need the least smallest local value. The 11 pixels shows on 33 bits the length of the mp3. Then i decode all of the binary data from the pixels, and put in a vector;
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <vector>
#include <math.h>
#include <iostream>
#include <fstream>
#define PNG_DEBUG 3
#include <png.h>
void abort_(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
abort();
}
void itob(short n, std::vector<int> &bin)
{
int d = n;
if (n > 1)
{
d = n % 2;
itob(n / 2, bin);
}
bin.push_back(d);
}
void btoi(unsigned int& n, std::vector<int> bin)
{
n = 0;
int k = 32;
for(int i = 0; i < bin.size() ; i++){
if(bin[i] == 1){
long int num = pow(2,k);
n += num;
}
k--;
}
}
int x, y;
int width, height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
void read_png_file()
{
unsigned char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen("image.png", "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", "image.png");
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
abort_("[read_png_file] File %s is not recognized as a PNG file", "image.png");
/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
abort_("[read_png_file] png_create_read_struct failed");
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
abort_("[read_png_file] png_create_info_struct failed");
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
png_read_image(png_ptr, row_pointers);
fclose(fp);
}
void process_file(void)
{
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGBA)
abort_("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGB "
"(lacks the alpha channel)");
if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGB)
abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGB (%d) (is %d)",
PNG_COLOR_TYPE_RGBA, png_get_color_type(png_ptr, info_ptr));
printf("width: %d\nheight: %d\n", width, height);
int mHeader = 33; unsigned int mSize = 0;
std::vector<int> mSizeByBites;
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
png_byte* ptr = &(row[x*3]);
if(mHeader == 0){ break; }
mHeader-=3;
std::vector<int> b;
itob(ptr[0], b);
mSizeByBites.push_back(b[b.size()-1]);
b.clear();
itob(ptr[1], b);
mSizeByBites.push_back(b[b.size()-1]);
b.clear();
itob(ptr[2], b);
mSizeByBites.push_back(b[b.size()-1]);
b.clear();
}
if(mHeader == 0){ break; }
}
for(int i =0; i<mSizeByBites.size(); i++){
printf("%d", mSizeByBites[i]);
}
btoi(mSize, mSizeByBites);
printf(" = %i\n", mSize);
std::vector<int> mDataBaBites;
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
if(mSize <= 0){ break; }
png_byte* ptr = &(row[x*3]);
std::vector<int> b;
itob(ptr[0], b);
mDataBaBites.push_back(b[b.size()-1]);
b.clear();
mSize--;
if(mSize <= 0){ break; }
itob(ptr[1], b);
mDataBaBites.push_back(b[b.size()-1]);
b.clear();
mSize--;
if(mSize <= 0){ break; }
itob(ptr[2], b);
mDataBaBites.push_back(b[b.size()-1]);
b.clear();
mSize--;
if(mSize <= 0){ break; }
printf("%i\n", mSize);
}
if(mSize<=0){ break; }
}
std::ofstream output("result.mp3", std::ios::out | std::ios::binary);
printf("[D] Writing to file start: %li\n", mDataBaBites.size());
output.write( (char*)(&mDataBaBites[0]), mDataBaBites.size() );
output.close();
}
int main(int argc, char **argv)
{
read_png_file();
process_file();
return 0;
}
Now I have no clue, how to write it in a file, wich i can play as an mp3. I tried to convert the bits to hexa.
What is the correct format of an mp3 file? How can I write the bits in the correct format?
Try this:
#include <fstream> //For std::min
std::ofstream mp3File( "restored.mp3", std::ios::out | std::ios::binary );
//Assuming rgbData is a char* with the mp3 data,
//and rgbDataSize is its size in bytes
mp3File.write( rgbData, rgbDataSize );
mp3File.close();
Update: When we (programmers) say "binary representation" we almost always mean bytes, not bits. From your description of the decoding process, I gather you should compare the 3 RGB components for each pixel and keep the minimum as the decoded byte. To do that:
#include <algorithm>
//...
std::vector<char> mDataBaBites;
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
png_byte red = row[x*3];
png_byte green = row[x*3 + 1];
png_byte blue = row[x*3 + 2];
png_byte minByte = std::min( std::min(red,green), blue );
mDataBaBites.push_back( minByte );
mSize -= 3;
}
if(mSize<=0){ break; }
}
std::ofstream output("result.mp3", std::ios::out | std::ios::binary);
printf("[D] Writing to file start: %li\n", mDataBaBites.size());
output.write( (char*)(&mDataBaBites[0]), mDataBaBites.size() );
output.close();
Update 2:
std::ofstream output("result.mp3", std::ios::out | std::ios::binary);
printf("[D] Writing to file start: %li\n", mDataBaBites.size());
for( int i=0; i<mDataBaBites.size(); i+=8 ){
char decodedByte = 0;
for( int j=0; j<8; j++ )
decodedByte |= (mDataBaBites[i+j] << j);
output.write( (char*)(&mDataBaBites[0]), 1 );
}
output.close();
If this doesn't work either, you might want to clarify the decoding process definition (which is its source? is there some formal definition?)
Related
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
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;
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
}
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;
}
i need some help with haar transformation, i have to apply it on an image.
My math is bad, my english not all that awesome and i find it hard to understand from articles on the internet. I found this page http://www.cs.ucf.edu/~mali/haar/haar.cpp where the haar transformation is applied on 2d matrix. I suppose if i give image pixels matrix in there, it should work?
im confused about this stuff, could someone enlighten me a bit please?
Thank you!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct {
unsigned char red,green,blue;
} PPMPixel;
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
#define CREATOR "SUDIPTAARNAB"
#define RGB_COMPONENT_COLOR 255
static PPMImage *readPPM(const char *filename)
{
char buff[16];
PPMImage *img;
FILE *fp;
int c, rgb_comp_color;
//open PPM file for reading
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//read image format
if (!fgets(buff, sizeof(buff), fp)) {
perror(filename);
exit(1);
}
//check the image format
if (buff[0] != 'P' || buff[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
exit(1);
}
//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//check for comments
c = getc(fp);
while (c == '#') {
while (getc(fp) != '\n') ;
c = getc(fp);
}
ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
exit(1);
}
//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}
//check rgb component depth
if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
exit(1);
}
while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
}
fclose(fp);
return img;
}
void writePPM(const char *filename, PPMImage *img)
{
FILE *fp;
//open file for output
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//write the header file
//image format
fprintf(fp, "P6\n");
//comments
fprintf(fp, "# Created by %s\n",CREATOR);
//image size
fprintf(fp, "%d %d\n",img->x,img->y);
// rgb component depth
fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);
// pixel data
fwrite(img->data, 3 * img->x, img->y, fp);
fclose(fp);
}
void imageDivide(const char *filename,PPMImage *img)
{
FILE *fp = fopen(filename,"rb");
FILE *filePtr;
filePtr = fopen ("floatArray.txt","w");
int width = 288;
int height = 352;
int i,j,m,k,l,i1,j1,l1,n1;
int *sum;
float *mean,*var;
unsigned char buff[(288*352)];
unsigned char image[288][352];
size_t n = fread( buff, sizeof(buff[0]), sizeof(buff), fp );
fclose(fp);
for(i =0; i < height; i++)
{
for(j = 0; j < width;j++)
{
image[j][i] = buff[(i*width)+j];
}
}
unsigned char dividedimage[(288*352)/(8*8)][8][8];
mean=(float *)malloc(sizeof(float)*1584);
var=(float *)malloc(sizeof(float)*1584);
sum=(int *)malloc(sizeof(int)*1584);
for(i = 0; i < height/8; i++)
{
for(j = 0; j < width/8;j++)
{
for(k = i*8, l = 0; k < (i*8)+8; k++,l++)
{
for(m = j*8, n = 0; m < (j*8)+8; m++,n++)
{
dividedimage[(i*(width/8))][n][l] = image[m][k];
}
}
}
}
printf("\n no of grids::%d i=%d j=%d,k=%d,m=%d n=%d",(i*(width/8)),i,j,k,m,n);
for(i1=0;i1<(i*(width/8));i1++)
{
sum[i1]=0;
printf("\nprinting info of %dth grid::\n",i1+1);
for(n1=0;n1<n;n1++)
{
for(l1=0;l1<n;l1++)
{
// printf("%5d",dividedimage[i1][j1][l1]);
sum[i1]=sum[i1]+(int)dividedimage[i1][j1][l1];
}
printf("\n");
}
mean[i1]=sum[i1]/64;
// printf("\n sum of intensities of grid %d is ::%d and mean is %f\n",i1+1,sum[i1],mean[i1]);
//printf()
}
for(i1=0;i1<(i*(width/8));i1++)
{
var[i1]=0;
printf("\nprinting info of %dth grid::\n",i1+1);
for(n1=0;n1<n;n1++)
{
for(l1=0;l1<n;l1++)
{
printf("%5d",dividedimage[i1][j1][l1]);
//sum[i1]=sum[i1]+(int)dividedimage[i1][j1][l1];
var[i1]=var[i1]+pow(((int)dividedimage[i1][j1][l1]-mean[i1]),2);
}
printf("\n");
}
var[i1]=var[i1]/64;
printf("\n variance of grid %d is ::%f\n",i1+1,var[i1]);
}
//printf()
for (i = 0; i < 1584; i++) {
// y[i] = var[i1]);
printf("\n%f",var[i]);
fprintf (filePtr, "%5f\n", var[i]);
}
haar1d(var,1584);
}
/** The 1D Haar Transform **/
void haar1d(float *vec, int n)
{
int i=0;
int w=n;
FILE *filePtr;
filePtr = fopen ("1dhaarwavelet.txt","w");
float *vecp ;
vecp=(float *)malloc(sizeof(float)*n);
for(i=0;i<n;i++)
vecp[i] = 0;
while(w>1)
{
w/=2;
for(i=0;i<w;i++)
{
vecp[i] = (vec[2*i] + vec[2*i+1])/sqrt(2.0);
vecp[i+w] = (vec[2*i] - vec[2*i+1])/sqrt(2.0);
}
for(i=0;i<(w*2);i++)
vec[i] = vecp[i];
}
// delete [] vecp;
printf("\nthe 1d haarwavelet trasform is::");
for (i = 0; i < n; i++) {
printf("\n%f",vec[i]);
fprintf (filePtr, "%5f\n", vec[i]);
}
}
/** A Modified version of 1D Haar Transform, used by the 2D Haar Transform function **/
void haar1(float *vec, int n, int w)
{
int i=0;
float *vecp = (float *)malloc(sizeof(float)*n);
for(i=0;i<n;i++)
vecp[i] = 0;
w/=2;
for(i=0;i<w;i++)
{
vecp[i] = (vec[2*i] + vec[2*i+1])/sqrt(2.0);
vecp[i+w] = (vec[2*i] - vec[2*i+1])/sqrt(2.0);
}
for(i=0;i<(w*2);i++)
vec[i] = vecp[i];
// delete [] vecp;
}
/** The 2D Haar Transform **/
void haar2(float **matrix, int rows, int cols)
{
float *temp_row = (float *)malloc(sizeof(float)*cols);
float *temp_col = (float *)malloc(sizeof(float)*rows);
int i=0,j=0;
int w = cols, h=rows;
while(w>1 || h>1)
{
if(w>1)
{
for(i=0;i<h;i++)
{
for(j=0;j<cols;j++)
temp_row[j] = matrix[i][j];
haar1(temp_row,cols,w);
for(j=0;j<cols;j++)
matrix[i][j] = temp_row[j];
}
}
if(h>1)
{
for(i=0;i<w;i++)
{
for(j=0;j<rows;j++)
temp_col[j] = matrix[j][i];
haar1(temp_col, rows, h);
for(j=0;j<rows;j++)
matrix[j][i] = temp_col[j];
}
}
if(w>1)
w/=2;
if(h>1)
h/=2;
}
// delete [] temp_row;
// delete [] temp_col;
}
int main(){
char filein[100],fileout[100];
PPMImage *image1,*image2;
int m,i,j;
printf("\nEnter the input file name::");
gets(filein);
image1 = readPPM(filein);
imageDivide(filein,image1);
// printf("\nEnter the output file name::");
// gets(fileout);
// writePPM(fileout,image1);
float **mat = (float **)malloc(sizeof(float*)*4);
for(m=0;m<4;m++)
mat[m] = (float *)malloc(sizeof(float)*4);
mat[0][0] = 5; mat[0][1] = 6; mat[0][2] = 1; mat[0][3] = 2;
mat[1][0] = 4; mat[1][1] = 2; mat[1][2] = 5; mat[1][3] = 5;
mat[2][0] = 3; mat[2][1] = 1; mat[2][2] = 7; mat[2][3] = 1;
mat[3][0] = 6; mat[3][1] = 3; mat[3][2] = 5; mat[3][3] = 1;
haar2(mat,4,4);
printf("\nafter 2d haarwavelet::\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(" %f ",mat[i][j]);
}
printf("\n");
}
printf("Press any key...");
getchar();
}
If you're trying to do object detection using Haar features, pay a look at OpenCV:
http://opencv.willowgarage.com/documentation/cpp/object_detection.html
There is an example at the end of the URL you point to.
Look inside the main() function.
the 2D variant takes a float** and two parameters, height and width
the float** points to rows of grayscale pixels
each row is a float*, a pointer to the first pixel in the row
each float value is the intensity value of the pixel
in the example code, the dimensions are 4x4.
This is where the memory is allocated:
float **mat = new float*[4];
for(int m=0;m<4;m++)
mat[m] = new float[4];
This is where the pixel values are set:
mat[0][0] = 5; mat[0][1] = 6; mat[0][2] = 1; mat[0][3] = 2;
mat[1][0] = 4; mat[1][1] = 2; mat[1][2] = 5; mat[1][3] = 5;
mat[2][0] = 3; mat[2][1] = 1; mat[2][2] = 7; mat[2][3] = 1;
mat[3][0] = 6; mat[3][1] = 3; mat[3][2] = 5; mat[3][3] = 1;
This is where the haar2 function is called:
haar2(mat,4,4);
All you need to do is provide the data as needed by the function (float**) with the right dimensions. You probably want to store the results to an output file that you can open in an image viewing application.
Look for the PGM format for a really easy solution. Note that the results of the haar function will give you floating point values, which you may have to compress down to 8 bit to view the image.