Save image | developer.brewmp.com Save image | developer.brewmp.com

Developer

Save image

Forums:

I've loaded a bmp file
IBitmap* pSampleBitmap = ISHELL_LoadBitmap(pApplet->m_pIShell, "test.bmp" );

and I need to save it via IBitmap.
Is there a way to do this expect for accessing IDIB and forming bmp header and body?
Please help!

just do it yourself, use BMPHEADER etc from msdn, fill in the values and write it out.
http://local.wasp.uwa.edu.au/~pbourke/dataformats/bmp/

just do it yourself, use BMPHEADER etc from msdn, fill in the values and write it out.
http://local.wasp.uwa.edu.au/~pbourke/dataformats/bmp/

Hi,
I formed the header and infoheader using data from IDib structure. However, the problem is in image pixel data accessing.
when I access pIDib->pBmp or &pIDib->pBmp I access some data which is not the same as in original bmp. I think the problem in offset for accessing image pixel data via IDib.
The image I've loaded is 32X32 and has nPitch=-32,i.e. it is a bottom-up image.
I still can't form the image with is quite identical to image which I load.
Can anybody help me?

Hi,
I formed the header and infoheader using data from IDib structure. However, the problem is in image pixel data accessing.
when I access pIDib->pBmp or &pIDib->pBmp I access some data which is not the same as in original bmp. I think the problem in offset for accessing image pixel data via IDib.
The image I've loaded is 32X32 and has nPitch=-32,i.e. it is a bottom-up image.
I still can't form the image with is quite identical to image which I load.
Can anybody help me?

sorry, but this is not exactly what I am looking for.
I need to form a file using IDIB/IBitmap pointer and save it.
I use
IFILE_Write(pMyFile, &pIDib->pBmp, datasize)
to write pixel data in the output file. The problem is that the output file differs from original.
Is something wrong in the way I access pBmp buffer?
In the thread you mentioned there is no sample of saving a file via IDIB/IBitmap pointer :(
Please help to find a solution!
p.s. I attached two images - 1 is original, 2nd - is what I saved programmatically.
They are different => I need to make them equal...

sorry, but this is not exactly what I am looking for.
I need to form a file using IDIB/IBitmap pointer and save it.
I use
IFILE_Write(pMyFile, &pIDib->pBmp, datasize)
to write pixel data in the output file. The problem is that the output file differs from original.
Is something wrong in the way I access pBmp buffer?
In the thread you mentioned there is no sample of saving a file via IDIB/IBitmap pointer :(
Please help to find a solution!
p.s. I attached two images - 1 is original, 2nd - is what I saved programmatically.
They are different => I need to make them equal...

Allocate a buffer, intialize a windows style header, add appropriate header information based on the image information, add the image data and then save the buffer using the IFILE_Write function.

Allocate a buffer, intialize a windows style header, add appropriate header information based on the image information, add the image data and then save the buffer using the IFILE_Write function.

be aware their might be a load conversion going on that'll change format, hence why i linked that thread as it shows hows to access the RGB elements.

be aware their might be a load conversion going on that'll change format, hence why i linked that thread as it shows hows to access the RGB elements.

I am trying to save image in following way:
pSampleBitmap = ISHELL_LoadBitmap(pApplet->m_pIShell, MOTO_PICTURES"/sun.bmp" );
if (!pSampleBitmap)
{
SM_DBGPRINTF0("[TEST]ISHELL_LoadBitmap failed");
return EFAILED;
}
IBITMAP_QueryInterface(pSampleBitmap, AEECLSID_DIB, (void**)&pIDib);
IFile* pMyFile = IFILEMGR_OpenFile(pFileMgr, "mytest.bmp", _OFM_CREATE);
//form header - 14 bytes size
IFILE_Write(pMyFile, bHeader, sizeof(bHeader));
//form info header - 40 bytes size
IFILE_Write(pMyFile, bInfoHeader, sizeof(bInfoHeader));
if(pIDib->cntRGB > 0)
{
//in my case cntRGB=256 => I write paletter in a file
IFILE_Write(pMyFile, &pIDib->pRGB, pIDib->cntRGB);
}
// write pixel data
IFILE_Write(pMyFile, &pIDib->pBmp,filesize - 54 - pIDib->cntRGB);
IFILE_Release(pMyFile);
//
After the file is saved I compare it to original and I've discovered that there are differences and the newly created file is not displayed correctly.
header and infoheader in both files coincide.
But data written from pRGB and pBmp differ
What may be wrong? Any ideas?

I am trying to save image in following way:
pSampleBitmap = ISHELL_LoadBitmap(pApplet->m_pIShell, MOTO_PICTURES"/sun.bmp" );
if (!pSampleBitmap)
{
SM_DBGPRINTF0("[TEST]ISHELL_LoadBitmap failed");
return EFAILED;
}
IBITMAP_QueryInterface(pSampleBitmap, AEECLSID_DIB, (void**)&pIDib);
IFile* pMyFile = IFILEMGR_OpenFile(pFileMgr, "mytest.bmp", _OFM_CREATE);
//form header - 14 bytes size
IFILE_Write(pMyFile, bHeader, sizeof(bHeader));
//form info header - 40 bytes size
IFILE_Write(pMyFile, bInfoHeader, sizeof(bInfoHeader));
if(pIDib->cntRGB > 0)
{
//in my case cntRGB=256 => I write paletter in a file
IFILE_Write(pMyFile, &pIDib->pRGB, pIDib->cntRGB);
}
// write pixel data
IFILE_Write(pMyFile, &pIDib->pBmp,filesize - 54 - pIDib->cntRGB);
IFILE_Release(pMyFile);
//
After the file is saved I compare it to original and I've discovered that there are differences and the newly created file is not displayed correctly.
header and infoheader in both files coincide.
But data written from pRGB and pBmp differ
What may be wrong? Any ideas?

File saving problem
I am trying to save image in following way:
pSampleBitmap = ISHELL_LoadBitmap(pApplet->m_pIShell, MOTO_PICTURES"/sun.bmp" );
if (!pSampleBitmap)
{
SM_DBGPRINTF0("[TEST]ISHELL_LoadBitmap failed");
return EFAILED;

IBITMAP_QueryInterface(pSampleBitmap, AEECLSID_DIB, (void**)&pIDib);
IFile* pMyFile = IFILEMGR_OpenFile(pFileMgr, "mytest.bmp", _OFM_CREATE);
//form header - 14 bytes size
IFILE_Write(pMyFile, bHeader, sizeof(bHeader));
//form info header - 40 bytes size
IFILE_Write(pMyFile, bInfoHeader, sizeof(bInfoHeader));
if(pIDib->cntRGB > 0)
{
//in my case cntRGB=256 => I write paletter in a file
IFILE_Write(pMyFile, &pIDib->pRGB, pIDib->cntRGB);

// write pixel data
IFILE_Write(pMyFile, &pIDib->pBmp,filesize - 54 - pIDib->cntRGB);
IFILE_Release(pMyFile);
//
After the file is saved I compare it to original and I've discovered that there are differences and the newly created file is not displayed correctly.
header and infoheader in both files coincide.
But data written from pRGB and pBmp differ
What may be wrong? Any ideas?

File saving problem
I am trying to save image in following way:
pSampleBitmap = ISHELL_LoadBitmap(pApplet->m_pIShell, MOTO_PICTURES"/sun.bmp" );
if (!pSampleBitmap)
{
SM_DBGPRINTF0("[TEST]ISHELL_LoadBitmap failed");
return EFAILED;

IBITMAP_QueryInterface(pSampleBitmap, AEECLSID_DIB, (void**)&pIDib);
IFile* pMyFile = IFILEMGR_OpenFile(pFileMgr, "mytest.bmp", _OFM_CREATE);
//form header - 14 bytes size
IFILE_Write(pMyFile, bHeader, sizeof(bHeader));
//form info header - 40 bytes size
IFILE_Write(pMyFile, bInfoHeader, sizeof(bInfoHeader));
if(pIDib->cntRGB > 0)
{
//in my case cntRGB=256 => I write paletter in a file
IFILE_Write(pMyFile, &pIDib->pRGB, pIDib->cntRGB);

// write pixel data
IFILE_Write(pMyFile, &pIDib->pBmp,filesize - 54 - pIDib->cntRGB);
IFILE_Release(pMyFile);
//
After the file is saved I compare it to original and I've discovered that there are differences and the newly created file is not displayed correctly.
header and infoheader in both files coincide.
But data written from pRGB and pBmp differ
What may be wrong? Any ideas?

Not sure why are you writing two headers, all you have to do is get the standard windows header, fillup the information, and then put the image data pBmp pointer in the IDIB structure and the dump everything to a file.

Not sure why are you writing two headers, all you have to do is get the standard windows header, fillup the information, and then put the image data pBmp pointer in the IDIB structure and the dump everything to a file.

there are two consecutive headers in a BMP thats why.
when you say differ, in what way? there may be some internal conversion between RGB formats

there are two consecutive headers in a BMP thats why.
when you say differ, in what way? there may be some internal conversion between RGB formats

Windows Bitmap header
typedef struct _BitmapHeader /**** BMP file info structure ****/
{
unsigned char m_actype[2]; /* Magic identifier */
unsigned int m_iTotalSize; /* File size in bytes */
unsigned short int reserved1, reserved2; /* Ignore these... */
unsigned int m_iOffset; /* Offset to image data, bytes */
unsigned int m_iSize; /* Size of info header */
int m_iWidth; /* Width of image */
int m_iHeight; /* Height of image */
unsigned short m_iPlanes; /* Number of color planes */
unsigned short m_iBitCount; /* Number of bits per pixel */
unsigned int m_iCompression; /* Type of compression to use */
unsigned int m_iSizeImage; /* Size of image data */
int m_iXPelsPerMeter; /* X pixels per meter */
int m_iYPelsPerMeter; /* Y pixels per meter */
unsigned int m_iClrUsed; /* Number of colors used */
unsigned int m_iClrImportant; /* Number of important colors */
BMPBitmapHeader;

Windows Bitmap header
typedef struct _BitmapHeader /**** BMP file info structure ****/
{
unsigned char m_actype[2]; /* Magic identifier */
unsigned int m_iTotalSize; /* File size in bytes */
unsigned short int reserved1, reserved2; /* Ignore these... */
unsigned int m_iOffset; /* Offset to image data, bytes */
unsigned int m_iSize; /* Size of info header */
int m_iWidth; /* Width of image */
int m_iHeight; /* Height of image */
unsigned short m_iPlanes; /* Number of color planes */
unsigned short m_iBitCount; /* Number of bits per pixel */
unsigned int m_iCompression; /* Type of compression to use */
unsigned int m_iSizeImage; /* Size of image data */
int m_iXPelsPerMeter; /* X pixels per meter */
int m_iYPelsPerMeter; /* Y pixels per meter */
unsigned int m_iClrUsed; /* Number of colors used */
unsigned int m_iClrImportant; /* Number of important colors */
BMPBitmapHeader;

correct header.
Header
The header consists of the following fields. Note that we are assuming short int of 2 bytes, int of 4 bytes, and long int of 8 bytes. Further we are assuming byte ordering as for typical (Intel) machines. The header is 14 bytes in length.
typedef struct {
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned int offset; /* Offset to image data, bytes */
HEADER;
The useful fields in this structure are the type field (should be 'BM') which is a simple check that this is likely to be a legitimate BMP file, and the offset field which gives the number of bytes before the actual pixel data (this is relative to the start of the file). Note that this struct is not a multiple of 4 bytes for those machines/compilers that might assume this, these machines will generally pad this struct by 2 bytes to 16 which will unalign the future fread() calls - be warned.
Information
The image info data that follows is 40 bytes in length, it is described in the struct given below. The fields of most interest below are the image width and height, the number of bits per pixel (should be 1, 4, 8 or 24), the number of planes (assumed to be 1 here), and the compression type (assumed to be 0 here).
typedef struct {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
INFOHEADER;

correct header.
Header
The header consists of the following fields. Note that we are assuming short int of 2 bytes, int of 4 bytes, and long int of 8 bytes. Further we are assuming byte ordering as for typical (Intel) machines. The header is 14 bytes in length.
typedef struct {
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned int offset; /* Offset to image data, bytes */
HEADER;
The useful fields in this structure are the type field (should be 'BM') which is a simple check that this is likely to be a legitimate BMP file, and the offset field which gives the number of bytes before the actual pixel data (this is relative to the start of the file). Note that this struct is not a multiple of 4 bytes for those machines/compilers that might assume this, these machines will generally pad this struct by 2 bytes to 16 which will unalign the future fread() calls - be warned.
Information
The image info data that follows is 40 bytes in length, it is described in the struct given below. The fields of most interest below are the image width and height, the number of bits per pixel (should be 1, 4, 8 or 24), the number of planes (assumed to be 1 here), and the compression type (assumed to be 0 here).
typedef struct {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
INFOHEADER;

Could you please tell me what's the difference? In memory/disk when you write two struct consecutively it becomes the flat memory layout, so I don't see any difference, infact one single block write is more efficient than two small write. In addition I have validate it and it works for me.

Could you please tell me what's the difference? In memory/disk when you write two struct consecutively it becomes the flat memory layout, so I don't see any difference, infact one single block write is more efficient than two small write. In addition I have validate it and it works for me.

ther are different sections to a BMP file, each with different kinds of information, sticking to the spec allows you to use the standard windows definitions.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bit...
you can write it out in one go by wrapping them in a struct, which is the typical way to do it.
theres not a lot of info in the fileinfoheader you need, if you know its a bmp, so you can also skip it, rather than just having it sitting in memory. similar to stripping png headers on j2me.
keeping the structs seperate, keeps its neat and tidy, theres no reason to glob it alltogether in one giant struct.

ther are different sections to a BMP file, each with different kinds of information, sticking to the spec allows you to use the standard windows definitions.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bit...
you can write it out in one go by wrapping them in a struct, which is the typical way to do it.
theres not a lot of info in the fileinfoheader you need, if you know its a bmp, so you can also skip it, rather than just having it sitting in memory. similar to stripping png headers on j2me.
keeping the structs seperate, keeps its neat and tidy, theres no reason to glob it alltogether in one giant struct.

brewee wrote:
p.s. I attached two images - 1 is original, 2nd - is what I saved programmatically.
They are different => I need to make them equal...
the only difference is the later one is shifted up one line, so there is probably some error in the calculation of the addres for the scanline buffer.
the nuber of important colours is set to 256 in t78, and 0 in sun

brewee wrote:
p.s. I attached two images - 1 is original, 2nd - is what I saved programmatically.
They are different => I need to make them equal...
the only difference is the later one is shifted up one line, so there is probably some error in the calculation of the addres for the scanline buffer.
the nuber of important colours is set to 256 in t78, and 0 in sun

Thank you!

Thank you!