bitmap -> raster
bitmap -> raster
What is the easiest way to get a 1 bit image into an array? For either Java or C/C++? I have found many ways, Java Advanced Imaging is one, but it is more complicated then I need. I just need to get an array from the file. Does Java have a native class that does this?
dib.cpp
dib.h
i use this when dealing with bmp's
and you can get all you need is in RGBQUAD * lprgb;
hope it helps
Code: Select all
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"
#include "Dib.h"
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstdlib>
#include "string.h"
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Dib::Dib()
{
lpbminfo =NULL;
lpbminfoHeader = NULL;
lprgb = NULL;
lpDibBits = NULL;
}
Dib::Dib(const char * filename)
{
LoadBitmapFile(filename);
}
DWORD Dib::GetDibSizeImage()
{
if(lpbminfoHeader->biSizeImage==0)
{
DWORD byteWidth = (DWORD)GetDibWidth();
DWORD byteHeight = (DWORD)GetDibHeight();
DWORD imageSize = byteWidth * byteHeight;
return imageSize;
}
else
return lpbminfoHeader->biSizeImage;
}
UINT Dib::GetDibWidth()
{
return (UINT)lpbminfoHeader->biWidth;
}
UINT Dib::GetDibHeight()
{
return (UINT)lpbminfoHeader->biHeight;
}
UINT Dib::GetDibNumColours()
{
if((lpbminfoHeader->biClrUsed==0)&&(lpbminfoHeader->biBitCount < 9))
return (1<<lpbminfoHeader->biBitCount);
else
return (int)lpbminfoHeader->biClrUsed;
}
UINT Dib::GetDibBitCount()
{
return lpbminfoHeader->biBitCount;
}
LPBITMAPINFOHEADER Dib::GetDibInfoHeaderPtr()
{
return lpbminfoHeader;
}
LPBITMAPINFO Dib::GetDibInfoPtr()
{
return lpbminfo;
}
RGBQUAD* Dib::GetDibRGBTablePtr()
{
return lprgb;
}
BYTE* Dib::GetDibBitsPtr()
{
return lpDibBits;
}
void Dib::DrawBits(HDC hDC, int x, int y)
{
StretchDIBits(hDC,x,y,lpbminfo->bmiHeader.biWidth,lpbminfo->bmiHeader.biHeight,0,0,lpbminfo->bmiHeader.biWidth,lpbminfo->bmiHeader.biHeight,lpDibBits,lpbminfo,DIB_RGB_COLORS,SRCCOPY);
}
void Dib::DrawBits(HDC hDC, RECT *r)
{
DWORD dwWidth, dwHeight;
dwWidth = r->right - r->left;
dwHeight = r->bottom - r->top;
StretchDIBits(hDC,r->left,r->right,dwWidth,dwHeight,0,0,dwWidth,dwHeight,lpDibBits,lpbminfo,DIB_RGB_COLORS,SRCCOPY);
}
void Dib::DrawRawBits(HDC hDC, RECT *r)
{
r->left = 10;
r->right=200;
r->top = 10;
r->bottom = 200;
int bits = lpbminfo->bmiHeader.biBitCount;
char * bitsS = (char *)&bits;
char info[100] = "Bit Count = ";
char temp[60];
sprintf(temp, "%d", bits);
strcat(info, temp);
sprintf(temp, "%d", lpbminfo->bmiHeader.biHeight);
strcat(info, "\nHeight = ");
strcat(info, temp);
sprintf(temp, "%d", lpbminfo->bmiHeader.biWidth);
strcat(info, "\nWidth = ");
strcat(info, temp);
sprintf(temp, "%d", lpbminfo->bmiHeader.biSize);
strcat(info, "\nSize = ");
strcat(info, temp);
DrawText(hDC,info,strlen(info), r, DT_LEFT);
}
Dib::LoadBitmapFile(const char * filename)
{
FILE * f = fopen(filename, "rb");
fseek(f,0,SEEK_END);
DWORD filelength = ftell(f);
fseek(f,0,SEEK_SET);
BITMAPFILEHEADER bmFileHeader;
fread((char * ) &bmFileHeader, sizeof(bmFileHeader),1,f);
if(bmFileHeader.bfType != 0x4d42)
{
MessageBox(NULL,"Not a bitmap phool",NULL, MB_OK);
lpbminfo = NULL;
lpbminfoHeader = NULL;
lprgb = NULL;
lpDibBits = NULL;
numColours = 0;
}
else
{
DWORD dibSize = filelength - sizeof(bmFileHeader);
BYTE * pDib = (BYTE * ) malloc(dibSize);
fread((char * ) pDib, dibSize,1,f);
fclose(f);
lpbminfo = (LPBITMAPINFO)pDib;
lpbminfoHeader = (LPBITMAPINFOHEADER)pDib;
lprgb = (RGBQUAD*) (pDib + lpbminfoHeader->biSize);
numColours = GetDibNumColours();
lpbminfoHeader->biSizeImage = GetDibSizeImage();
if(lpbminfoHeader->biClrUsed == 0)
{
lpbminfoHeader->biClrUsed = numColours;
}
DWORD clrTableSize = numColours * sizeof(RGBQUAD);
lpDibBits = pDib + lpbminfoHeader->biSize+clrTableSize;
}
}
int Dib::getHeight()
{
return lpbminfo->bmiHeader.biHeight;
}
int Dib::getWidth()
{
return lpbminfo->bmiHeader.biWidth;
}
Dib::~Dib()
{
if(lpbminfo != NULL)
{
delete lpbminfo;
lpbminfo = NULL;
}
if(lpDibBits != NULL)
{
delete lpDibBits;
lpDibBits = NULL;
}
lpbminfoHeader = NULL;
lprgb = NULL;
}
void Dib::DrawHistogram(HDC hDC, int x, int y)
{
int size = 255+1;
int* rr = new int[size];
int* gg = new int[size];
int* bb = new int[size];
double c1= lpbminfo->bmiHeader.biWidth;
double c2= lpbminfo->bmiHeader.biHeight;
gray =new int* [c2];
colors = new int*[c2];
for(int z=0;z<c2;z++)
{
*(gray+z)=new int[c1];
*(colors+z)=new int[c1*3];
}
for(int c=0; c<size; c++)
{
rr[c] = 0;
gg[c] = 0;
bb[c] = 0;
}
for (int i=x;i<lpbminfo->bmiHeader.biWidth+x;i++)
{
for (int j=y;j<lpbminfo->bmiHeader.biHeight+y;j++)
{
COLORREF cc = GetPixel(hDC,i,j);
int r = GetRValue(cc);
int g = GetGValue(cc);
int b = GetBValue(cc);
rr[r] +=1;
gg[g] +=1;
bb[b] +=1;
gray[j-y][i-x] = (int)(r*0.3 + g*0.59 + b*0.11);
colors[j-y][(i-x)*3] = r;
colors[j-y][(i-x)*3+1] = g;
colors[j-y][(i-x)*3+2] = b;
}
}
//(c.R*0.3 + c.G*0.59+ c.B*0.11)
for(int k=0; k<size+1; k++)
{
for(int l=0; l<rr[k]/10; l++)
{
SetPixel(hDC,100+k,800-l,RGB(255,0,0));
}
SetPixel(hDC,k+100,801,RGB(0,0,0));
for(int l1=0; l1<gg[k]/10; l1++)
{
SetPixel(hDC,100+k+300,800-l1,RGB(0,255,0));
}
SetPixel(hDC,k+100+300,801,RGB(0,0,0));
for(int l2=0; l2<bb[k]/10; l2++)
{
SetPixel(hDC,100+k+600,800-l2,RGB(0,0,255));
}
SetPixel(hDC,k+100+600,801,RGB(0,0,0));
}
}
int** Dib::getGray()
{
return gray;
}
int** Dib::getColors()
{
return colors;
}
Code: Select all
// Dib.h: interface for the Dib class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DIB_H__4CA4486B_CB76_437C_B438_62E92611F784__INCLUDED_)
#define AFX_DIB_H__4CA4486B_CB76_437C_B438_62E92611F784__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
class Dib
{
//typedef struct tagBITMPINFO
public:
Dib();
Dib(const char * filename);
virtual ~Dib();
LoadBitmapFile(const char * filename);
DWORD GetDibSizeImage();
UINT GetDibWidth();
UINT GetDibHeight();
UINT GetDibNumColours();
void DrawBits(HDC hDC, int x, int y);
void DrawBits(HDC hDC, RECT *r);
void DrawRawBits(HDC hDC, RECT *r);
int** getGray();
int** getColors();
UINT GetDibBitCount();
LPBITMAPINFOHEADER GetDibInfoHeaderPtr();
LPBITMAPINFO GetDibInfoPtr();
RGBQUAD* GetDibRGBTablePtr();
BYTE* GetDibBitsPtr();
void DrawHistogram(HDC hDC, int x, int y);
int getWidth();
int getHeight();
protected:
LPBITMAPINFO lpbminfo;
LPBITMAPINFOHEADER lpbminfoHeader;
RGBQUAD * lprgb;
BYTE* lpDibBits;
UINT numColours;
int** gray;
int** colors;
};
#endif // !defined(AFX_DIB_H__4CA4486B_CB76_437C_B438_62E92611F784__INCLUDED_)
and you can get all you need is in RGBQUAD * lprgb;
hope it helps
MAY THE CHEESE BE WITH YOU!
{PK} Law