BMPlib v1.0 [BETA]

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Rangu2057
Posts: 87
Joined: Mon Jul 23, 2007 8:37 am
Location: wilmington, NC

BMPlib v1.0 [BETA]

Post by Rangu2057 »

this is a small basic lib for displaying BMP images on the psp, its currently in beta stages

I havent had a lot of time on my hand these days so its hard to actually add more stuff to it

If anyone has any other info specific to displaying BMP images please let me know

Two more things, can anyone help me out with coding a basic server for receiving messages from a client, and also i was thinking is it worth trying to code another pnglib, as the current one is greatly complex and i can never seem to get my image to print to the screen, and end up with around 2000 errors and warnings in the png.h file

Code: Select all

/************************************************************************************
* BMPLib.h	-	DIB / BMP library by Bernard Jacobs made @ 2 - 26 - 08, 3:12	*
*************************************************************************************/

#ifndef BMPLib		/* research " 24 bit image data, indexed color data, rgb"	*/
#define BMPLib		/* SIDE NOTE - this piece of code is best used to handle uncompressed BMP files */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define compression

#define none 0		/* no compression 		*/
#define BI_RLE8 1		/* 8-bit run length encoding	*/
#define BI_RLE4 2		/* 4-bit run length encoding	*/
#define BI_BITFIELDS 3	/* RGB bitmap with RGB mask	*/


#endif /* compression */

#define magic1 0x42	/* magic numbers used to identify file as BMP */
#define magic2 0x4D	/* both are ASCII code points for "B" and "M" */


/******************************
* Private BMPLib functions 	*
*******************************/

extern int Init_BMPLib(void); 			/* starts BMPLib	*/
extern int LoadBMP(const char *file);		/* loads BMP file	*/


typedef struct {					/* stores general information about the BMP file 					*/
unsigned int magic;				/* magic number used to identify the BMP file	 					*/
unsigned int Sze;					/* size of BMP file in bytes				 					*/
unsign short int reserved1;			/* reserved, actual value depends on application that made image 			*/
unsign short int reserved2;			/* reserved, actual value depends on application that made image 			*/
unsigned int offSet;				/* the offset, starting address of the byte where bitmap data can be found 	*/
}HEADER;

typedef struct {			/* shows detailed information about the bitmap image 	*/
unsigned int size;		/* header size in bytes						*/
signed int width;			/* bitmap width in pixels					*/
signed int height;		/* bitmap height in pixels					*/
unsigned short int planes;	/* # of color planes, must be set to 1			*/
unsigned short int bits;	/* # of bits per pixel, which is color depth of image	*/
unsigned int compression;	/* compression type						*/
unsigned int imageSze;		/* size of image in bytes					*/
int xresolution, yresolution;	/* pixels per meter						*/
unsigned int colors;		/* number of the colors						*/
unsigned int important;		/* very important colors					*/
}INFOHEADER;

typedef struct {			/* 
unsigned char rgbBlue;		/* Blue value 	*/
unsigned char rgbGreen;		/* Green value 	*/
unsigned char rgbRed;		/* Red value 	*/
unsigned char rgbReserved;	/* Reserved		*/
}RGBQUAD;
the questions of today are awnswered by the blood and bullets of tomorrow! ---EagleEye--- (Socom FTB2)
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

Can you post the .c file please?
Image
Upgrade your PSP
Rangu2057
Posts: 87
Joined: Mon Jul 23, 2007 8:37 am
Location: wilmington, NC

Post by Rangu2057 »

its not quite finished yet, but itll be up soon
the questions of today are awnswered by the blood and bullets of tomorrow! ---EagleEye--- (Socom FTB2)
Rangu2057
Posts: 87
Joined: Mon Jul 23, 2007 8:37 am
Location: wilmington, NC

Post by Rangu2057 »

oik heres what i got so far, since this is my friends computer i cant try to compile it right now


BMPLib.c

Code: Select all

/************************************
BMPlib.c by Bernard			*
*************************************/


FILE *fopen(const char *filename, const char *mode)

#include <cstdio.h>


loadBMP&#40;char *file&#41; &#123;
	// file handle used in all file operations
	FILE *in;

	// open the file for reading in binary mode
	in = fopen&#40;file,"rb"&#41;;

	size_t fread&#40;void *ptr, size_t size, size_t nelem, FILE *strem&#41;;

		BITMAPHEADER bmfh;

	fread&#40;&bmfh,sizeof&#40;BITMAPFILEHEADER&#41;, 1, in&#41;;
	WORD bftype;
	DWORD bfSize;
	WORD bfReserved1;
	WORD bfReserved2;
	DWORD bfOffBits;

		BITMAPINFOHEADER bmih;

	fread&#40;&bmih, sizeof&#40;BITMAPINFOHEADER&#41;, 1, in&#41;;
	DWORD biSize;
	LONG biWidth;
	LONG biHeight;
	WORD biPlanes;
	WORD biBitCount;
	DWORD biCompression;
	DWORD biSizeImage;
	LONG biXPelsPerMeter;
	LONG biYPelsPerMeter;
	DWORD biClrImportant;

	// set color #'s 
	numColours=1 << bmih.biBitCount;

	// load pallete for 8 bits per pixel
	if&#40;bmih.biBitCount == 8&#41;
		&#123;
		colours=new RGBQUAD&#91;numColours&#93;;
		fread&#40;colours, sizeof&#40;RGBQUAD&#41;, numcolours, in&#41;; 
		&#125;

	DWORD size;
	size = bmfh.bfSize-bmfh.bfOffBits;
	BYTE *tempPixelData;
	tempPixelData=new BYTE&#91;size&#93;;

	if&#40;tempPixelData == NULL&#41;
		&#123;
		fclose&#40;in&#41;;
		return false;
		&#125;

	fread&#40;tempPixelData, sizeof&#40;BYTE&#41;,size, in&#41;;

	// byteWidth is the width of actual image in bytes
	// padWidth is the width of image plus extra padding
	LONG byteWidth, padWidth;

	// Initially set both to the width of the image
	byteWidth = padWidth = &#40;LONG&#41;&#40;&#40;float&#41;width*&#40;float&#41;bpp/8.0&#41;;

	// add any extra space to bring each line to a DWORD boundary
	while&#40;padWidth%4!=0&#41;
		&#123;
		padWidth++;
		&#125;

	DWORD diff;
	int offset;
	LONG height;

	height = bmih.biHeight;
	// set diff to the actual image size &#40;no padding&#41;
	diff = height*byteWidth;
	// allocate memory for the image
	pixelData = new BYTE&#91;diff&#93;;
	if&#40;pixelData==NULL&#41;
		&#123;
		fclose&#40;in&#41;;
		return false;
		&#125;
	// bitmap is inverted, so the padding needs to be removed
	// and the image reversed
	// Here you can start from the back of the file or the front,
	// after the header.  The only problem is that some programs
	// will pad not only the data, but also the file size to be
	// divisible by 4 bytes

	if&#40;height > 0&#41;
		&#123;
		int j == size-3;
		offset = padWidth - byteWidth;
		for&#40;int i=0;i<size;i+=3&#41;
			&#123;
			if&#40;&#40;i=1&#41;%padWidth==0&#41;
				&#123;
				int i+= offset;
				&#125;
			*&#40;pixelData+j+2&#41;=*&#40;tempPixelData+i&#41;;
			*&#40;pixelData+j+1&#41;=*&#40;tempPixelData+i+1&#41;;	
			*&#40;pixelData+j&#41;=*&#40;tempPixelData+i+2&#41;;
			j++;
			&#125;
		&#125; else &#123;
			height=height*-1;
			offset=0;
			do &#123;
				memcpy&#40;&#40;pixelData+&#40;offset*byteWidth&#41;&#41;,
					&#40;tempPixelData+&#40;offset*padWidth&#41;&#41;,
					byteWidth&#41;;
				offset++;
			&#125; while&#40;offset<height&#41;;
		&#125;
the questions of today are awnswered by the blood and bullets of tomorrow! ---EagleEye--- (Socom FTB2)
CpuWhiz
Posts: 42
Joined: Mon Jun 04, 2007 1:30 am

Post by CpuWhiz »

Normally people compile and test their code before releasing something. Looking at the first line of code in your source file shows you left out a simicolon, and why is that line even there? Also at the end of the source file you do nothing with the image data and the function doesn't even end. Now find the 6 line comment and start reading, this is a reformated copy of the example code from this page: http://www.gamedev.net/reference/articl ... le1966.asp. If you want to use someone's code and they don't mind, you should probobly also credit them (esp. if you are showing off "your" code). This won't compile, much less display a bitmap on the PSP screen.

You may be wondering why I am hashing on you. It's because you can't just splice someone's code together, make a few formatting changes and the like and hope it works. You have to understand how the code works and what it's doing, every line, every variable, every call. If you can't do that then how are you going to modify it to fit your needs. You need to go back over the article (and maybe even your basic C/C++ materials) and google any function you don't know. Learn how and what the code does and why. Ask somewhere else (this forum is not the place for these kinds of questions, and most will just flame you) about any syntax or anything else you don't get.

Edit: Sorry, the link was wrong because of the period.
Post Reply