Sign in to follow this  
echo_00

C/C++ stuct array (BMP mosaic project)

Recommended Posts

Alight, this is giving me a headache. When compiled and run a file name is passed during exectution (bitmap.exe image.bmp), it displays the image in in ASCII on the console screen. It was displaying upside down, i fixed that and I added fopen and fwrite to write the bmp rights side up to a new BMP. Now i'm told i need to import 4 images and write them in a mosaic like this: 4444444 4333334 4322234 4321234 4322234 4333334 4444444 To a new BMP. Sorry for the long into.. Taking it one step at a time, so first thing i need to do is open and hold all 4 bmp images in memcpy?.But for now my question is how do i open 4 bmp images at once? I need to use an array, but the file name is coming from "argv". an array of BMP structs needs to be created(am i correct?). But i'm not clear on the syntax and where to insert the code.
#include <iostream>
#include <cString>
#include <conio.h>
using namespace std;


int main(int argc, char* argv[])
{

	int inventory, counter =0;
	char ch;

	while(1)
	{
		if(!_kbhit())
		{
		  ch = 	getch();
		  cout<<ch<<endl;
		}
	}
	
	return 0;
}



#include <windows.h>
#include <stdio.h>
#include <direct.h>
#include <string.h>

#include <iostream>
#include <cString>
using namespace std;


#define HI(x)	(x>>4)  //( x / 0x10 )
#define LO(x)	(x&0x0F)//( x % 0x10 )


typedef struct tagBMP 
{
	BITMAPFILEHEADER	bmfh;
	BITMAPINFOHEADER	bmih;
	RGBQUAD*			colorTable;
	BYTE*				bmpBits;
} BMP[4];


int numColors, pixelsPerByte;

void LoadBMP( char* filename, BMP* bmp )
{
	FILE *fp;
	numColors = 0;
	pixelsPerByte = 0;


	// Open the file
	if ( ( fp = fopen( filename, "rb" ) ) == NULL )
	{
		cout<< "Could not open file "<< filename <<endl;;
		exit(1);
	}


	fread( &bmp->bmfh, sizeof( bmp->bmfh ), 1, fp );

	if ( bmp->bmfh.bfType !=  'MB' )
	{
		cout<<filename<<"is not a valid bitmap file"<<endl;
		exit(1);
	}

	fread( &bmp->bmih, sizeof( bmp->bmih ), 1, fp );

	switch( bmp->bmih.biBitCount )
	{
	case 1:									// Monochrome
		numColors = 1;
		pixelsPerByte = 8;
		break;
	case 4:									// 16-color bitmap
		numColors = 16;
		pixelsPerByte = 2;
		break;
	case 8:									// 256-color palettized bitmap
		numColors = 256;
		pixelsPerByte = 1;
		break;
	case 16:								// 16-bit color bitmap
		cout<<"ERROR: Bitmap is 16-bit color, not palettized\n";
		fclose(fp);
		exit(1);
	case 24:
		cout<<"ERROR: Bitmap is 24-bit color, not palettized\n"<<endl;
		fclose(fp);
		exit(1);
	default:
		cout<<"Unknown bit count"<<endl;
		fclose(fp);
		exit(1);
	}

	if ( ( bmp->colorTable = new RGBQUAD[ ( numColors * 4 )]) == NULL )
	{
		fclose(fp);
		cout<<"ERROR: Unable to allocate memory for color table\n"<<endl;
		exit(1);
	}

	for ( int i = 0; i < numColors; i++ )
	{
		fread( &bmp->colorTable[i], 4, 1, fp );
	}

	if ( ( bmp->bmpBits =  new BYTE [ ( bmp->bmih.biHeight * bmp->bmih.biWidth ) ] ) == NULL )
	{
		fclose(fp);
		cout<<"ERROR: Unable to allocate memory for pixel bits\n";

		// Prevent memory leak before exiting
		delete []  bmp->colorTable;
		exit(1);
	}

	for ( int y = 0; y < bmp->bmih.biHeight; y++ )
	{
		for ( int x = 0; x < bmp->bmih.biWidth; x++ )
		{
			fread( &bmp->bmpBits[y * bmp->bmih.biWidth + x], 1, 1, fp );
		}
	}

	fclose( fp );

//creates new bmp to write too, and write 
	
	fp = fopen("img002.bmp", "wb");
	fwrite( &bmp->bmfh, sizeof( bmp->bmfh ), 1, fp );
 
	fwrite( &bmp->bmih, sizeof( bmp->bmih ), 1, fp );

	for ( int i = 0; i < numColors; i++ )
	{
		fwrite( &bmp->colorTable[i], 4, 1, fp );
	}
	for ( int y = 0; y < bmp->bmih.biHeight; y++ )
	{
		for ( int x = 0; x < bmp->bmih.biWidth; x++ )
		{
			fwrite( &bmp->bmpBits[y * bmp->bmih.biWidth + x], 1, 1, fp );
		}
	}
	fclose( fp);

//end of bmp write		
}

void DisplayMonoBMP( BMP* bmp )
{
	printf("DisplayMonoBMP");
	// Fill in code if needed
}

void Display16ColorBMP( BMP* bmp )
{
	int HiNum, LoNum;
	int adjWidth = ( bmp->bmih.biWidth / 2 );
	printf("\n");

	for ( int y = 0; y < bmp->bmih.biHeight; y++ )
	{
		for ( int x = 0; x < adjWidth; x++ )
		{
			if(HI( bmp->bmpBits[( y * adjWidth ) + x] )) HiNum =9; else HiNum = 0;
			if(LO( bmp->bmpBits[( y * adjWidth ) + x] )) LoNum =9; else LoNum = 0;
			printf( "%i%i ", HiNum, LoNum  );
		}
		printf("\n");
	}
}

void Display256ColorBMP( BMP* bmp )
{
	printf("\n");
//display flip 
	for ( int y = bmp->bmih.biHeight; y > 0; y-- )
	{
		for ( int x = bmp->bmih.biWidth; x > 0; x-- )
		{
			printf( "%02d ", bmp->bmpBits[( y * bmp->bmih.biWidth ) - x] );
			// +x" to a "-x" 
		}
		printf("\n");
	}
}


void DisplayBMP( BMP* bmp )
{
	switch ( bmp->bmih.biBitCount )
	{
	case 1:
		DisplayMonoBMP( bmp );
		break;
	case 4:
		Display16ColorBMP( bmp );
		break;
	case 8:
		Display256ColorBMP( bmp );
		break;
	default:
		printf( "Cannot display this bitmap" );
		break;
	}
}
	
void main( int argc, char** argv )
{

	int i;
	
	BMP bmp;
	BMP2 bmp;
	BMP3 bmp;
	BMP4 bmp;

	if(argc == 1)
	{
		cout<<"No Files to display\n";		
		return;
	}

	for ( i=1; i < argc; i++ )
	{
		LoadBMP( argv[i], &bmp );
		DisplayBMP( &bmp );	
	}

	if ( bmp.colorTable  )
	{
		delete [] ( bmp.colorTable );
	}

	if ( bmp.bmpBits )
	{
		delete [] ( bmp.bmpBits );
	}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this