How do I run the following code - c++

I have a relatively big C++ project that I'm trying to figure out how to run.
There are 3 code files as outlined below and 2 .wav audio files - test.wav and poolIR.wav.
The program is supposed to convolve the test file with the impulse response and produce a third output file. The 2 audio files must be passed to the program at runtime and I am new to C/C++, so I'm trying to see how I should do it. Can anyone help me out please. Below is the code.
I am using WINDOWS 7 and I have gcc installed.
convolve.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <math.h>
#include "CWav.h"
using namespace std;
#define DEBUG_MODE
/* Test tone frequency in Hz */
#define FREQUENCY 440.0
/* Test tone duration in seconds */
#define DURATION 2.0
/* Standard sample rate in Hz */
#define SAMPLE_RATE 44100.0
/* Standard sample size in bits */
#define BITS_PER_SAMPLE 16
/* Standard sample size in bytes */
#define BYTES_PER_SAMPLE (BITS_PER_SAMPLE/8)
/* Number of channels */
#define MONOPHONIC 1
#define STEREOPHONIC 2
#define SIZE 8
#define PI 3.141592653589793
#define TWO_PI (2.0 * PI)
#define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
void outputToFile(double data[], CWav* original , int numberOfSamples, char* outputFile);
void four1(double data[], int nn, int isign);
void writeWaveFileHeader(int channels, int numberSamples, int bitsPerSample, double sampleRate, FILE *outputFile);
size_t fwriteIntLSB(int data, FILE *stream);
void getSignal(CWav *input, double x[]);
size_t fwriteShortLSB(short int data, FILE *stream);
void outputToFile(double data[], CWav* original , int numberOfSamples, char* outputFile);
// The four1 FFT from Numerical Recipes in C,
// p. 507 - 508.
// Note: changed float data types to double.
// nn must be a power of 2, and use +1 for
// isign for an FFT, and -1 for the Inverse FFT.
// The data is complex, so the array size must be
// nn*2. This code assumes the array starts
// at index 1, not 0, so subtract 1 when
// calling the routine (see main() below).
void four1(double data[], int nn, int isign)
{
unsigned long n, mmax, m, j, istep, i;
double wtemp, wr, wpr, wpi, wi, theta;
double tempr, tempi;
n = nn << 1;
j = 1;
for (i = 1; i < n; i += 2) {
if (j > i) {
SWAP(data[j], data[i]);
SWAP(data[j+1], data[i+1]);
}
m = nn;
while (m >= 2 && j > m) {
j -= m;
m >>= 1;
}
j += m;
}
mmax = 2;
while (n > mmax) {
istep = mmax << 1;
theta = isign * (6.28318530717959 / mmax);
wtemp = sin(0.5 * theta);
wpr = -2.0 * wtemp * wtemp;
wpi = sin(theta);
wr = 1.0;
wi = 0.0;
for (m = 1; m < mmax; m += 2) {
for (i = m; i <= n; i += istep) {
j = i + mmax;
tempr = wr * data[j] - wi * data[j+1];
tempi = wr * data[j+1] + wi * data[j];
data[j] = data[i] - tempr;
data[j+1] = data[i+1] - tempi;
data[i] += tempr;
data[i+1] += tempi;
}
wr = (wtemp = wr) * wpr - wi * wpi + wr;
wi = wi * wpr + wtemp * wpi + wi;
}
mmax = istep;
}
}
void writeWaveFileHeader(int channels, int numberSamples, int bitsPerSample,
double sampleRate, FILE *outputFile)
{
/* Calculate the total number of bytes for the data chunk */
int dataChunkSize = channels * numberSamples * (bitsPerSample / 8);
/* Calculate the total number of bytes for the form size */
int formSize = 36 + dataChunkSize;
/* Calculate the total number of bytes per frame */
short int frameSize = channels * (bitsPerSample / 8);
/* Calculate the byte rate */
int bytesPerSecond = (int)ceil(sampleRate * frameSize);
/* Write header to file */
/* Form container identifier */
fputs("RIFF", outputFile);
/* Form size */
fwriteIntLSB(formSize, outputFile);
/* Form container type */
fputs("WAVE", outputFile);
/* Format chunk identifier (Note: space after 't' needed) */
fputs("fmt ", outputFile);
/* Format chunk size (fixed at 16 bytes) */
fwriteIntLSB(16, outputFile);
/* Compression code: 1 = PCM */
fwriteShortLSB(1, outputFile);
/* Number of channels */
fwriteShortLSB((short)channels, outputFile);
/* Output Sample Rate */
fwriteIntLSB((int)sampleRate, outputFile);
/* Bytes per second */
fwriteIntLSB(bytesPerSecond, outputFile);
/* Block alignment (frame size) */
fwriteShortLSB(frameSize, outputFile);
/* Bits per sample */
fwriteShortLSB(bitsPerSample, outputFile);
/* Sound Data chunk identifier */
fputs("data", outputFile);
/* Chunk size */
fwriteIntLSB(dataChunkSize, outputFile);
}
/******************************************************************************
*
* function: fwriteIntLSB
*
* purpose: Writes a 4-byte integer to the file stream, starting
* with the least significant byte (i.e. writes the int
* in little-endian form). This routine will work on both
* big-endian and little-endian architectures.
*
* internal
* functions: none
*
* library
* functions: fwrite
*
******************************************************************************/
size_t fwriteIntLSB(int data, FILE *stream)
{
unsigned char array[4];
array[3] = (unsigned char)((data >> 24) & 0xFF);
array[2] = (unsigned char)((data >> 16) & 0xFF);
array[1] = (unsigned char)((data >> 8) & 0xFF);
array[0] = (unsigned char)(data & 0xFF);
return fwrite(array, sizeof(unsigned char), 4, stream);
}
/******************************************************************************
*
* function: fwriteShortLSB
*
* purpose: Writes a 2-byte integer to the file stream, starting
* with the least significant byte (i.e. writes the int
* in little-endian form). This routine will work on both
* big-endian and little-endian architectures.
*
* internal
* functions: none
*
* library
* functions: fwrite
*
******************************************************************************/
size_t fwriteShortLSB(short int data, FILE *stream)
{
unsigned char array[2];
array[1] = (unsigned char)((data >> 8) & 0xFF);
array[0] = (unsigned char)(data & 0xFF);
return fwrite(array, sizeof(unsigned char), 2, stream);
}
void getSignal(CWav *input, double x[]) {
for ( int i = 0; i < input->mySignalSize; i++ )
x[i] = ((double)input->my_signal[i])/32678.0;
}
void outputToFile(double output_signal[], CWav* original , int numberOfSamples, char* outputFile) {
/* Open a binary output file stream for writing */
FILE *outputFileStream = fopen(outputFile, "wb");
/* Write the WAVE file header */
writeWaveFileHeader(original->myChannels, numberOfSamples, original->myBitsPerSample,
original->mySampleRate, outputFileStream);
int i;
float maxValInResult = -1.0;
for (i = 0; i < numberOfSamples; i++ )
if ( output_signal[i] > maxValInResult )
maxValInResult = output_signal[i];
float maxValInInput = -1.0;
for (i = 0; i < numberOfSamples; i++ )
if (original->my_signal[i] > maxValInInput )
maxValInInput = original->my_signal[i];
for (i = 0; i < numberOfSamples; i++ )
fwriteShortLSB((short)(output_signal[i] / maxValInResult * maxValInInput), outputFileStream);
/* Close the output file stream */
fclose(outputFileStream);
}
//convolve inputfile IRfile outputfile as inputs to main
int main(int argc, char* argv[])
{
if (argc != 4) {
cout << "USAGE: ./convolve inputfile IRfile outputfile" << endl;
//system("PAUSE");
return 0;
}
cout << argv[0] << endl;
cout << "Input File:" << argv[1] << endl;
cout << "IRfile:" << argv[2] << endl;
cout << "outputfile File:" <<argv[3] << endl;
char *outputFilename = argv[3];
/* Create the sine wave test tone, using the specified
frequency, duration, and number of channels, writing to
a .wav file with the specified output filename */
//createTestTone(FREQUENCY, DURATION, MONOPHONIC, BITS_PER_SAMPLE, SAMPLE_RATE, outputFilename);
CWav *inputSignal = new CWav();
inputSignal->readInput(argv[1]);
//manipulate(inputSignal, 2);
CWav *impulse = new CWav();
impulse->readInput(argv[2]);
cout << "Input Signal: " << inputSignal->mySignalSize << ", Impulse Size: " << impulse->mySignalSize << endl;
double h[impulse->mySignalSize];
double x[inputSignal->mySignalSize];
getSignal(impulse, h);
getSignal(inputSignal, x);
int sizeH = impulse->mySignalSize;
int sizeX = inputSignal->mySignalSize;
cout << "SIZES(H,X)" << endl;
cout << sizeH << endl;
cout << sizeX << endl;
int maxSize = 0;
if(sizeX >= sizeH) {
maxSize = sizeX;
}
else {
maxSize = sizeH;
}
cout << "maxSize: " << maxSize << endl;
int power = 0;
int pow2 = 0;
pow2 = (int) log2(maxSize) + 1;
pow2 = pow(2,pow2);
cout << "POW :" << pow2 << endl;
int i = 0;
int doublePow2 = 2 * pow2;
//set hComplex with 0's
double hComplex[doublePow2];
double *xComplex = new double[doublePow2];
//set hComplex , xComplex with 0's
for(i = 0; i < doublePow2; i++) {
hComplex[i] = 0.0;
xComplex[i] = 0.0;
}
//padding the complex number with 0 and the real number with original value for h
for(i = 0; i < sizeH; i++) {
hComplex[2*i] = h[i];
}
//padding the complex number with 0 and the real number with original value for x
for(i = 0; i < sizeX; i++) {
xComplex[2*i] = x[i];
}
four1(hComplex, pow2, 1);
four1(xComplex, pow2, 1);
double *yComplex = new double[doublePow2];
for(i = 0; i < pow2 ; i++) {
yComplex[i*2] = xComplex[i] * hComplex[i] - xComplex[i+1] * hComplex[i+1];
yComplex[i*2+1] = xComplex[i+1] * hComplex[i] + xComplex[i] * hComplex[i+1];
}
four1(yComplex-1, pow2, -1);
outputToFile(yComplex, inputSignal, pow2, outputFilename);
}
CWav.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include "CWav.h"
using namespace std;
void CWav::readInput(char *filename)
{
ifstream inFile( filename, ios::in | ios::binary);
//printf("Reading wav file...\n"); // for debugging only
inFile.seekg(4, ios::beg);
inFile.read( (char*) &myChunkSize, 4 ); // read the ChunkSize
inFile.seekg(16, ios::beg);
inFile.read( (char*) &mySubChunk1Size, 4 ); // read the SubChunk1Size
inFile.seekg(20, ios::beg);
inFile.read( (char*) &myFormat, sizeof(short) ); // read the file format. This should be 1 for PCM
//inFile.seekg(22, ios::beg);
inFile.read( (char*) &myChannels, sizeof(short) ); // read the # of channels (1 or 2)
//inFile.seekg(24, ios::beg);
inFile.read( (char*) &mySampleRate, sizeof(int) ); // read the samplerate
//inFile.seekg(28, ios::beg);
inFile.read( (char*) &myByteRate, sizeof(int) ); // read the byterate
//inFile.seekg(32, ios::beg);
inFile.read( (char*) &myBlockAlign, sizeof(short) ); // read the blockalign
//inFile.seekg(34, ios::beg);
inFile.read( (char*) &myBitsPerSample, sizeof(short) ); // read the bitspersample
inFile.seekg(40, ios::beg);
inFile.read( (char*) &myDataSize, sizeof(int) ); // read the size of the data
// read the data chunk
myData = new char[myDataSize];
inFile.seekg(44, ios::beg);
inFile.read(myData, myDataSize);
inFile.close(); // close the input file
my_signal = NULL;
if ( myBitsPerSample == 8 )
{
mySignalSize = myDataSize;
my_signal = new short[mySignalSize];
for ( int i = 0; i < myDataSize; i++ )
my_signal[i] = (short)( (unsigned char) myData[i] );
}
else if ( myBitsPerSample == 16 ){
mySignalSize = myDataSize / 2;
my_signal = new short[mySignalSize];
short val;
for ( int i = 0; i < myDataSize; i+=2 )
{
val = (short)( (unsigned char) myData[i] );
val += (short)( (unsigned char) myData[i+1] ) * 256;
my_signal[i/2] = val;
}
}
}
CWav.h:
#ifndef CWavH
#define CWavH
class CWav
{
private:
char* myData;
public:
int myChunkSize;
int mySubChunk1Size;
short myFormat;
short myChannels;
int mySampleRate;
int myByteRate;
short myBlockAlign;
short myBitsPerSample;
int myDataSize;
short *my_signal;
int mySignalSize;
public:
void readInput(char *filename);
};
#endif
Then I have 2 audio files.
poolIR.wav AND test.wav

C++ is not a scripting language, you have to compile it first.
To compile the code using gcc try the following
(assuming that all files are in the same directory):
g++ convolve.cpp CWav.cpp -o convolve.exe
That should generate an executable file named convolve.exe.
The -o flag specifies the output file name (convolve.exe).
See the gcc documentation for details.

Related

MPI C++ Runtime Error: signal 11 (Segmentation fault) with multi-dimensional array creation

Making Mandelbrot with MPI
So I've made a Mandelbrot generator and everything worked fine. Now I'm throwing in a speedup from MPI. Process 0 generates a file name mbrot.ppm and adds the appropriate metadata, then divides up the workload into chunks.
Each process receives the chunk's starting and ending positions and gets to work calculating its portion of the Mandelbrot set. To write to the mbrot.ppm file, each process saves its data in an array so it doesn't write to the file before the previous process finishes.
My Problem
Its a runtime error that says:
Primary job terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
--------------------------------------------------------------------------
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node Lenovo exited on signal 11 (Segmentation fault).
I believe it comes from the line int data[3][xrange][yrange]; (line 120) since the print statement after this line never executes. Would there be an obvious reason I'm missing why this multi-dimensional array is causing me problems?
Full Code
#include <iostream>
#include <mpi.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#define MCW MPI_COMM_WORLD
using namespace std;
struct Complex {
double r;
double i;
};
Complex operator + (Complex s, Complex t) {
Complex v;
v.r = s.r + t.r;
v.i = s.i + t.i;
return v;
};
Complex operator * (Complex s, Complex t) {
Complex v;
v.r = s.r * t.r - s.i * t.i;
v.i = s.r * t.i + s.i * t.r;
return v;
};
int rcolor(int iters) {
if (iters == 255) return 0;
return 32 * (iters % 8);
};
int gcolor(int iters) {
if (iters == 255) return 0;
return 32 * (iters % 8);
};
int bcolor(int iters) {
if (iters == 255) return 0;
return 32 * (iters % 8);
};
int mbrot(Complex c, int maxIters) {
int i = 0;
Complex z;
z = c;
while (i < maxIters && z.r * z.r + z.i * z.i < 4) {
z = z * z + c;
i++;
}
return i;
};
int main(int argc, char * argv[]) {
int rank, size;
MPI_Init( & argc, & argv);
MPI_Comm_rank(MCW, & rank);
MPI_Comm_size(MCW, & size);
if (size < 2) {
printf("Not an MPI process if only 1 process runs.\n");
exit(1);
}
if (size % 2 != 0) {
printf("Please use a even number\n");
exit(1);
}
Complex c1, c2, c;
char path[] = "brot.ppm";
int DIM;
int chunk[4];
c1.r = -1;
c1.i = -1;
c2.r = 1;
c2.i = 1;
if (rank == 0) { //start the file
ofstream fout;
fout.open(path);
DIM = 2000; // pixel dimensions
fout << "P3" << endl; // The file type .ppm
fout << DIM << " " << DIM << endl; // dimensions of the image
fout << "255" << endl; // color depth
fout.close();
// making dimesions marks
for (int i = 0; i < size; i++) {
chunk[0] = 0; // startX
chunk[1] = DIM; // endX
chunk[2] = (DIM / size) * i; // startY
chunk[3] = (DIM / size) * (i + 1); // endY
MPI_Send(chunk, 4, MPI_INT, i, 0, MCW);
};
};
MPI_Recv(chunk, 4, MPI_INT, 0, 0, MCW, MPI_STATUS_IGNORE);
printf("Process %d recieved chunk\n\t StartX: %d, EndX: %d\n\t StartY: %d, EndY: %d\n", rank, chunk[0], chunk[1], chunk[2], chunk[3]);
// do stuff save in array
// data[3 elements][Xs][Ys]
int xrange = chunk[1] - chunk[0];
int yrange = chunk[3] - chunk[2];
printf("Process %d, x: %d, y: %d\n", rank, xrange, yrange);
int data[3][xrange][yrange];
printf("done\n");
// generate data for mandlebrot
for (int j = chunk[2]; j < chunk[3]; ++j) {
for (int i = chunk[0]; i < chunk[1]; ++i) {
// calculate one pixel of the DIM x DIM image
c.r = (i * (c1.r - c2.r) / DIM) + c2.r;
c.i = (j * (c1.i - c2.i) / DIM) + c2.i;
int iters = mbrot(c, 255);
data[0][i][j] = rcolor(iters);
data[1][i][j] = gcolor(iters);
data[2][i][j] = bcolor(iters);
}
}
printf("here2\n");
// taking turns to write their data to file
for (int k = 0; k < size; k++) {
if (rank == k) {
ofstream fout;
fout.open(path, ios::app);
fout << rank << " was here" << endl;
for (int j = chunk[2]; j < chunk[3]; ++j) {
for (int i = chunk[0]; i < chunk[1]; ++i) {
fout << data[0][i][j] << " " << data[1][i][j] << " " << data[2][i][j] << " ";
}
fout << endl;
}
printf("Process %d done and waiting\n", rank);
} else {
MPI_Barrier(MCW);
}
}
MPI_Finalize();
};
How to Run
$ mpic++ -o mbrot.out mbrot.cpp
$ mpirun -np 4 mbrot.out

LibQREncode qrcode to BMP

I'm creating a qrcode with the library qrencode.h
This creation is working nice but how would one output the qrcode to a BMP file within c++?
At this very moment i have this code:
const char* szSourceSring = QRCODE_TEXT;
unsigned int unWidth, x, y, l, n, unWidthAdjusted, unDataBytes;
unsigned char* pRGBData, *pSourceData, *pDestData;
QRcode* pQRC;
FILE* f;
if (pQRC = QRcode_encodeString(szSourceSring, 4, QR_ECLEVEL_H, QR_MODE_8, 1))
{
unWidth = pQRC->width;
unWidthAdjusted = unWidth * OUT_FILE_PIXEL_PRESCALER * 3;
if (unWidthAdjusted % 4)
unWidthAdjusted = (unWidthAdjusted / 4 + 1) * 4;
unDataBytes = unWidthAdjusted * unWidth * OUT_FILE_PIXEL_PRESCALER;
// Allocate pixels buffer
if (!(pRGBData = (unsigned char*)malloc(unDataBytes)))
{
printf("Out of memory");
}
// Preset to white
memset(pRGBData, 0xff, unDataBytes);
// Prepare bmp headers
BITMAPFILEHEADER kFileHeader;
kFileHeader.bfType = 0x4D42; // "BM"
kFileHeader.bfSize = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
unDataBytes;
kFileHeader.bfReserved1 = 0;
kFileHeader.bfReserved2 = 0;
kFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
BITMAPINFOHEADER kInfoHeader;
kInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
kInfoHeader.biWidth = unWidth * OUT_FILE_PIXEL_PRESCALER;
kInfoHeader.biHeight = -((int)unWidth * OUT_FILE_PIXEL_PRESCALER);
kInfoHeader.biPlanes = 1;
kInfoHeader.biBitCount = 24;
kInfoHeader.biCompression = BI_RGB;
kInfoHeader.biSizeImage = 0;
kInfoHeader.biXPelsPerMeter = 0;
kInfoHeader.biYPelsPerMeter = 0;
kInfoHeader.biClrUsed = 0;
kInfoHeader.biClrImportant = 0;
// Convert QrCode bits to bmp pixels
pSourceData = pQRC->data;
for(y = 0; y < unWidth; y++)
{
pDestData = pRGBData + unWidthAdjusted * y * OUT_FILE_PIXEL_PRESCALER;
for(x = 0; x < unWidth; x++)
{
if (*pSourceData & 1)
{
for(l = 0; l < OUT_FILE_PIXEL_PRESCALER; l++)
{
for(n = 0; n < OUT_FILE_PIXEL_PRESCALER; n++)
{
*(pDestData + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_B;
*(pDestData + 1 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_G;
*(pDestData + 2 + n * 3 + unWidthAdjusted * l) = PIXEL_COLOR_R;
}
}
}
pDestData += 3 * OUT_FILE_PIXEL_PRESCALER;
pSourceData++;
}
}
// Output the bmp file
/*if (((f = fopen(OUT_FILE, "r")) != NULL))
{*/
f = fopen(OUT_FILE, "wb");
fwrite(&kFileHeader, sizeof(BITMAPFILEHEADER), 14, f);
fwrite(&kInfoHeader, sizeof(BITMAPINFOHEADER), 40, f);
fwrite(pRGBData, sizeof(unsigned char), unDataBytes, f);
fclose(f);
/* }
else
{
printf("Unable to open file");
}
*/
// Free data
free(pRGBData);
QRcode_free(pQRC);
}
else
{
printf("NULL returned");
}
But somehow this creates a BMP with corrupt headers. Whenever i'm opening the bmp file it says:
"BMP Image has unsupported header size"
What am i doing wrong?
And is it possible to save to png instead of BMP?
I have access to the libPNG library
Here is a code example which dumps a 24 bpp bmp file created from a QR-Code. The error you see is probably not caused by the QR-Code library, but rather something in the bmp file code.
The bmp file created by this example works fine with the image viewer packaged with my Windows 8.1. If you also do not see the error, you could check for differences in each binary output to pinpoint the problem. If you want.
This question is tagged "C++" and "C++11", so this example uses the C++ std library for file output, and doesn't use malloc. (But almost equally bad -- I use new and delete in some container code, where a std::vector member is preferred...don't tell anyone). Also, this example writes each piece of data directly to the file, instead of using a file-sized intermediate buffer, like pDestData.
#include <iostream>
#include <fstream>
// A fake (or "somewhat limited") QR Code data container
struct Qrc {
int dimsize; // the width and height
unsigned char* data; // buffer which contains the elements
Qrc() {
static const unsigned int bin[] = { // encodes an important secret message
0xfc8b7d7f,0xa801a83,0xd6e54d76,0xaa9eb2ed,0x43ed05db,0xb8786837,0x55555fe0,
0x5a4c807f,0xcf315c00,0x6e8019ce,0xc7819e0d,0xd4857ba8,0x4ac5e347,0xf6f349ba,
0xd433ccdd,0x2998361e,0x4453fab3,0x526d9085,0x81f38924,0xb4da0811,0x84b3131a,
0x9639915e,0x3b74a4ff,0x42aa0c11,0x4127be16,0x1f4350,0xff620296,0xad54de1,
0xd38c2272,0xa3f76155,0x5366a7ab,0x9bdd2257,0x300d5520,0x85842e7f,0 };
dimsize = 33;
data = new unsigned char[dimsize * dimsize];
auto p = data;
auto endp = p + dimsize * dimsize;
for(unsigned int b : bin) {
for(int i=0; i<32; ++i) {
if(p == endp) break;
*(p++) = b & (1 << i) ? 255 : 0;
} } }
Qrc(const Qrc&) = delete;
Qrc& operator = (const Qrc&) = delete;
~Qrc() { delete [] data; }
};
struct BIH { // a private definition of BITMAPINFOHEADER
unsigned int sz;
int width, height;
unsigned short planes;
short bits;
unsigned int compress, szimage;
int xppm, yppm;
unsigned int clrused, clrimp;
};
void SaveBmp(const char* filename, const Qrc& qrc) {
// Asker's Qrc struct delivered as a pointer, from a C API, but this example doesn't mimic that.
std::ofstream ofs(filename, std::ios_base::out | std::ios_base::binary);
if(!ofs) {
std::cout << "Writing " << filename << " failed\n";
return;
}
const int side_len = qrc.dimsize; // width and height of the (square) QR Code
const int pixel_side_len = 4; // QRC element's size in the bmp image (in pixels)
const int bmp_line_bytes = side_len * pixel_side_len * 3;
const int bmp_line_pad_bytes = (4 - bmp_line_bytes % 4) % 4; // bmp line data padding size
const int bmp_data_size = side_len * (bmp_line_bytes + bmp_line_pad_bytes);
BIH bih = { sizeof(bih) };
bih.width = side_len * pixel_side_len; // element count * element size
bih.height = -side_len * pixel_side_len; // negative height => data begins at top of image
bih.planes = 1;
bih.bits = 24;
const int header_size = sizeof(bih) + 14; // size of the bmp file header
const int filesize = header_size + bmp_data_size; // size of the whole file
ofs.write("BM", 2);
ofs.write(reinterpret_cast<const char*>(&filesize), 4);
ofs.write("\0\0\0\0", 4); // 2x 16-bit reserved fields
ofs.write(reinterpret_cast<const char*>(&header_size), 4);
ofs.write(reinterpret_cast<const char*>(&bih), sizeof(bih));
// pixel colors, as Blue, Green, Red char-valued triples
// the terminating null also makes these usable as 32bpp BGRA values, with Alpha always 0.
static const char fg_color[] = "\0\0\0";
static const char bg_color[] = "\xff\xff\xff";
auto pd = qrc.data;
// send pixel data directly to the bmp file
// QRC elements are expanded into squares
// whose sides are "pixel_side_len" in length.
for(int y=0; y<side_len; ++y) {
for(int j=0; j<pixel_side_len; ++j) {
auto pdj = pd;
for(int x=0; x<side_len; ++x) {
for(int i=0; i<pixel_side_len; ++i) {
// *pdj will be 0 or 255 (from "fake" Qrc)
// Using "*pdj & 1" here, just to match asker's code
// without knowing why this was done.
ofs.write(*pdj & 1 ? fg_color : bg_color, 3);
}
++pdj;
}
if(bmp_line_pad_bytes) {
ofs.write("\0\0\0", bmp_line_pad_bytes);
}
}
pd += side_len;
}
}
int main() {
SaveBmp("MyQrCode.bmp", Qrc());
}

variable length bit packing in C

I am trying to write c functions ReadInt32, WriteInt32 for packing bits sequentially in a stream buffer with size optimisation in mind.
But my functions do not work as expected, I don't find the same values back after writing them in the buffer. I need a little bit of help to point what and where I have misunderstood.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BITSET(x,n) (x | ( 1 << n))
#define BITCLEAR(x,n) (x & ~(1 << n))
#define ISBITSET(x,n) ((x & (1 << n)) != 0)
/**
#param nbits The number of bits to write
#param val the integer value to write
#param bufptr a pointer on the buffer to write in
#param nbitswritten the number of bits already written in the previous write.
#return the number of bits actually written.
*/
int WriteInt32(int nbits, int val, uint8_t* bufptr, int nbitswritten)
{
uint8_t* p=(uint8_t*)&val;
uint8_t* ptr = &(*(bufptr+nbitswritten));
for (int i=0; i<nbits; i++)
{
int bpos = (i&0x7);
*(ptr+(i>>3)) = ISBITSET(*(p+(i>>3)), bpos) ? (BITSET(*(ptr+(i>>3)), bpos)) : (BITCLEAR(*(ptr+(i>>3)), bpos));
}
return (nbitswritten + nbits);
}
int ReadInt32(int nbits, int& val, uint8_t* ptr, int nbitsread)
{
val = 0;
uint8_t* p = &(*(ptr+nbitsread));
for (int i=0; i<nbits; i++)
{
uint8_t ch = *(p+(i>>3));
int bpos = (i&0x7);
val = (ISBITSET(ch, bpos) ? BITSET(val, bpos) : BITCLEAR(val, bpos));
}
return (nbitsread+nbits);
}
int main (int argc, const char * argv[])
{
int value = 289; // for example I want to encode this value on 10 bits
unsigned char buf[50];
// packing
int nbitswritten = 0;
nbitswritten = WriteInt32 (10, value, buf, 50, nbitswritten);
// unpacking - read from buffer
int nbitsread = 0;
int rvalue;
nbitsread = ReadInt32(10, rvalue, buf, nbitsread);
if ( value == rvalue)
printf("encoding & decoding ok\n");
else
printf("encoding or decoding failed\n");
return 0;
};
Thank you
Olivier
This is an answer to my own question. How to write/read an integer encoded in smaller size than its natural size.
If it can help someone. That's good. ;)
exemple:
struct dd
{
short var1:5; // This variable is encoded on 5 bits
int var2:11; // This variable is encoded on 11 bits
uint8_t var3:3; // This variable is encoded on 3 bits
};
uint8_t buffer[100];
bzero(buffer, 100);
int nbitswritten = 0;
struct dd vardd = { 16, 452, 3 };
// write stuff
nbitswritten = WriteBits(5, vardd.var1, buffer, 100, nbitswritten);
nbitswritten = WriteBits(11, vardd.var2, buffer, 100, nbitswritten);
nbitswritten = WriteBits(3, vardd.var3, buffer, 100, nbitswritten);
// read back
int v1, v2, v3;
int nbitsread = 0;
struct dd result;
nbitsread = ReadInt(5, v1, buffer, 100, nbitsread);
nbitsread = ReadInt(11, v2, buffer, 100, nbitsread);
nbitsread = ReadInt(3, v3, buffer, 100, nbitsread);
result.var1 = (short)v1;
result.var2 = v2;
result.var3 = (uint8_t)v3;
Actually the Read functions comes from the Torque Engine library!!
This is my slightly modified version :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BITSET(x,n) (x | ( 1 << n))
#define BITCLEAR(x,n) (x & ~(1 << n))
#define ISBITSET(x,n) ((x & (1 << n)) != 0)
/**
#param nbits The number of bits to write
#param val the value to write
#param bufptr the buffer to write to/in
#param buflen the maximum buffer length in byte/octet
#param nbitswritten the number of bits in bufptr already written.
#return the number of bits written;
*/
int WriteBits(int nbits, const void * val, uint8_t * bufptr, int buflen, int nbitswritten)
{
if (!bufptr || !val || !nbits || !buflen)
return 0;
if ( nbitswritten >= buflen<<8L )
return 0;
if ( nbits+nbitswritten>buflen<<8L )
return 0;
uint8_t* p=(uint8_t*)val;
int n = nbitswritten;
for (int i=0; i<nbits; i++)
{
int sbpos = (i&0x7);
int dbpos = (n&0x7);
*(bufptr+(n>>3)) = ISBITSET(*(p+(i>>3)), sbpos) ? (BITSET(*(bufptr+(n>>3)), dbpos)) : (BITCLEAR(*(bufptr+(n>>3)), dbpos));
n++;
}
nbitswritten = n;
return (n);
}
int ReadBits(int nbits, void * val, uint8_t * bufptr, int buflen, int nbitsread)
{
if (!nbits)
return 0;
uint8_t * stPtr = (bufptr + (nbitsread >> 3));
int byteCount = (nbits + 7) >> 3;
uint8_t * ptr = (TUint8*)val;
int downShift = nbitsread & 0x7;
int upShift = 8 - downShift;
uint8_t curB = *stPtr;
const uint8_t *stEnd = bufptr + buflen;
while(byteCount--)
{
stPtr++;
uint8_t nextB = stPtr < stEnd ? *stPtr : 0;
*ptr++ = (curB >> downShift) | (nextB << upShift);
curB = nextB;
}
nbitsread += nbits;
return nbitsread;
}
int ReadInt(int nbits, int &val, uint8_t * bufptr, int buflen, int nbitsread)
{
int ret = ReadBits(nbits, &val, bufptr, buflen, nbitsread);
if (nbits != 32) {
val &= (1<<nbits)-1;
}
return ret;
}
Olive

Write small bmp on console via C++

I'm trying to read painted 16x16 bmp, but there is no 1 pixel = 3 bits (RGB). Even if first 4-5 lines is white and the rest is black the document is still full of 255 255 255 for each pixel.
In my case I need to show this image in console by analyzing RGB layers of each pixel but have a lot of trouble with it.
int main()
{
FILE* f = fopen("image.bmp", "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char * data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for (int i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
unsigned int * byteData = new unsigned int[size];
for (int i = 0; i <= size; i++)
{
byteData[i] = (int) data[i];
}
for (int i = 0; i <= size / 3; i++)
{
cout << i << ".\t" << byteData[i] << "\t" << byteData[i + 1] << "\t" << byteData[i + 2] << endl;
}
cout << endl;
cout << "=======================" << endl;
for (int j = 0; j < width; j++)
{
cout << j + 1 << ".\t";
for (int i = 0; i < height; i++)
{
//if ((int)data[j * width + i] >= 100 && (int)data[j * width + i + 1] >= 100 && (int)data[j * width + i + 2] >= 100)
if (((int) data[j * width + i] + (int) data[j * width + i + 1] + (int) data[j * width + i + 2]) / 3 <= 170)
cout << " ";
else cout << "*";
}
cout << endl;
}
getchar();
return 0;
}
As I think problem with byte sequenses and reading memory frome garbage, but if you could explain where is leak?
The solution is next: bmp should be created with 24 bpp and top-down row order.
Correct code is for 16x16 bitmap:
#include <iostream>
using namespace std;
unsigned char* readBMP(char* filename);
int main()
{
unsigned char * data = readBMP("winLogo.bmp");
int size = 16*16*3;
unsigned int * byteData = new unsigned int[size];
for (int i = 0; i <= size; i++)
{
byteData[i] = (int)data[i];
}
int k = 0;
//uncomment to write a line of RGB values
//for (int i = 0; i < size; i += 3)
//{
// cout << k+1 << ".\t" << byteData[i] << "\t" << byteData[i + 1] << "\t" << byteData[i + 2] << endl;
// k++;
//}
for (int i = 0; i < size; i+= 3)
{
if (i % 16*3 == 0)
{
cout << endl << (i/(16*3))+1 << ".\t";
}
else {};
if (byteData[i] >= 200 && byteData[i + 1] >= 200 && byteData[i + 2] >= 200)
cout << " ";
else
cout << "#";
}
getchar();
return 0;
}
unsigned char* readBMP(char* filename)
{
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = -3 * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
//BGR -> RGB
for (i = 0; i < size; i += 3)
{
unsigned char tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
return data;
}

Finding a small bmp file in a bigger bmp file

I want to find a small bmp file from another bigger bmp file (the bigger one is captured from screen and called Sample.bmp , the small bmp file is called Button.bmp . Thing is the when comparing the images the file can't be found anywhere.
the compare code :
for (int i=0;i<SCREEN_WIDTH-width;++i)
{
for (int j=0;j<SCREEN_HEIGHT-height;++j)
{
boolean isequal = true;
for(int qqq=i;qqq<i+width;++qqq)
{
for (int kkk=j;kkk<j+height;++kkk)
{
if (PI[qqq][kkk]!=NPI[qqq-i][kkk-j]) isequal = false;
if (isequal == false)
{
qqq = i + width + 1;
kkk = j + height + 1;
}
}
}
if (isequal==true)
{
MidX = i;
MidY = j;
return;
}
}
}
note : Screen_width and Screen_height are for the bigger image and width and height are for the smaller one
Full Code:
void readBMP()
{
int i;
FILE* f = fopen("Sample.bmp", "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for(int qq=0;qq<SCREEN_WIDTH;++qq)
for (int kk=0;kk<SCREEN_HEIGHT;++kk)
{
PI[qq][kk][0] = data[kk * width + qq];
PI[qq][kk][1] = data[kk * width + qq + 1];
PI[qq][kk][2] = data[kk * width + qq + 2];
}
}
void FindImageInScreen(char* FileName)
{
FILE* f = fopen(FileName, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for(int qq=0;qq<width;++qq)
for (int kk=0;kk<height;++kk)
{
NPI[qq][kk][0] = data[kk * width + qq];
NPI[qq][kk][1] = data[kk * width + qq + 1];
NPI[qq][kk][2] = data[kk * width + qq + 2];
}
for (int i=0;i<SCREEN_WIDTH-width;++i)
{
for (int j=0;j<SCREEN_HEIGHT-height;++j)
{
boolean isequal = true;
for(int qqq=i;qqq<i+width;++qqq)
{
for (int kkk=j;kkk<j+height;++kkk)
{
if (PI[qqq][kkk][0]!=NPI[qqq-i][kkk-j][0]) isequal = false;
if (isequal == false)
{
qqq = i + width + 1;
kkk = j + height + 1;
}
}
}
if (isequal==true)
{
MidX = i;
MidY = j;
return;
}
}
}
MidX = -1;
MidY = -1;
return;
}
definition of arrays (added because of request) , This is before functions execute :
PI = new unsigned int**[SCREEN_WIDTH];
for (int i=0;i<SCREEN_WIDTH;++i)
PI[i] = new unsigned int*[SCREEN_HEIGHT];
for (int i=0;i<SCREEN_WIDTH;++i)
for (int j=0;j<SCREEN_HEIGHT;++j)
PI[i][j] = new unsigned int[3];
NPI = new unsigned int**[SCREEN_WIDTH];
for (int i=0;i<SCREEN_WIDTH;++i)
NPI[i] = new unsigned int*[SCREEN_HEIGHT];
for (int i=0;i<SCREEN_WIDTH;++i)
for (int j=0;j<SCREEN_HEIGHT;++j)
NPI[i][j] = new unsigned int[3];
The First function executes then the second. and sorry for some bad programming because I did thousands of changes to make it work!
PI[qq][kk][0] = data[kk * width + qq];
From how PI and NPI are filled in, it appears that they are 3-dimensional arrays (it would help if you included their definition in the code sample). But
if (PI[qqq][kkk]!=NPI[qqq-i][kkk-j]) isequal = false;
which is only indexing 2 dimensions of each. PI[a][b] is the address of the array containing PI[a][b][0..2] and will certainly never match the address of NPI[x][y], so this statement is always returning false I expect.
Lets get you started. Here is a better LoadBMP.
Yours, among other thing, read the size, and uses SCREEN_HEIGHT anyway.
Using this for loading both images is probably easier.
#include <vector>
#include <cstdio>
#include <string>
using namespace std;
typedef unsigned char UC;
struct RGB { UC r,g,b; };
bool operator == ( const RGB& p1, const RGB& p2 ) { return p1.r==p2.r && p1.g==p2.g && p1.b==p2.b; }
struct BMP
{
int width;
int height;
vector<RGB> pixels;
RGB& Pix(int x,int y) { return pixels[ y*width + x ]; }
};
void LoadBMP( BMP& bmp, const char* filename )
{
FILE* f = fopen(filename, "rb");
UC info[54];
fread(info, 1, 54, f); // read the 54-byte header
// extract image height and width from header
bmp.width = *(int*) (info+18);
bmp.height = *(int*) (info+22);
// scanlines are always multiple of 4, padded with 0-3 bytes
int scanlinesize = 3*bmp.width;
while( scanlinesize % 4 ) ++scanlinesize;
int size = scanlinesize * bmp.height;
UC* data = new UC[size];
fread(data, 1, size, f);
fclose(f);
bmp.pixels.clear();
bmp.pixels.reserve(bmp.height*bmp.width);
for(int yy=0;yy<bmp.height;++yy)
{
UC* p = data+scanlinesize*yy;
for (int xx=0;xx<bmp.width;++xx)
{
RGB rgb;
rgb.b = *p++;
rgb.g = *p++;
rgb.r = *p++;
bmp.pixels.push_back(rgb);
}
}
delete[] data;
}