Маленько на тему программирования
http://www.gamedev.ru/code/forum/?id=39676
http://www.cplusplus.com/forum/beginner/4307/
http://www.cplusplus.com/forum/general/6500/
Вот тут готовый код - вывод в файл
http://delphi.about.com/od/graphics/l/aa101803a.htm
Рисование
http://www.realcoding.net/teach/Delphi7_prof/Glava10/Index11.html
Var line : pByteArray;
For i:=0 to Imagel.Picture.Bitmap.Height — 1 do
Begin
Line := Imagel.Picture.Bitmap.ScanLine[i];
For j:=0 to Imagel.Picture.Bitmap.Width * 3 - 1 do
Line^[j] := 255 - Line^[j];
End;
This is just typed-in; it may have errors:
| |||||||||||||||||||
Did you ever dream of writing your own bitmap image? A 24 bit Bitmap file is relatevely simple to write. We should have an understading of BMP's header and should know how to write the data part. This article shows how. | |||||||||||||||||||
What is a header?Let's learn the header of BMP. A header is a place where the picture's basic information is stored. For example, colors used, width of the picture, height of the picture etc. The following is the header structure of a standard 24 bit BMP file. bfType1 : Byte ; (* "B" *)bfType2 : Byte ; (* "M" *) bfSize : LongInt ; (* Size of File. Zero is acceptable *) bfReserved1 : Word ; (* Zero *) bfReserved2 : Word ; (* Zero *) bfOffBits : LongInt ; (* Offset to beginning of BitMap *) biSize : LongInt ; (* Number of Bytes in Structure *) biWidth : LongInt ; (* Width of BitMap in Pixels *) biHeight : LongInt ; (* Height of BitMap in Pixels *) biPlanes : Word ; (* Planes in target device = 1 *) biBitCount : Word ; (* Bits per Pixel 1, 4, 8, or 24 *) biCompression : LongInt ; (* BI_RGB = 0, BI_RLE8, BI_RLE4 *) biSizeImage : LongInt ; (* Size of Image Part (often ignored) *) biXPelsPerMeter : LongInt ; (* Always Zero *) biYPelsPerMeter : LongInt ; (* Always Zero *) biClrUsed : LongInt ; (* Number of Colors used in Palette *) biClrImportant : LongInt ; (* Number of Colors that are Important *) In a 24 bit BMP, the header is immediately followed by the picture's data. The art of writing a binary fileUsually the basic unit of a binary file is byte. The data type, 'word' (please see the header) needs 2 bytes to store and LongInt needs 4 bytes. So the above header needs a total of 54 bytes storage space. We can now write files byte-wise. But using a structure is very easy and standard practice in writing these type of files.Few elementary things regarding the binary file The picture below is the screen-shot of a BMP opened in a Hex Editor. Hex Editor is a tool for viewing any file in its elementary form. There are two panes. On the left side, there is numbers in Hex and in right, its equivalent ASCII. The ASCII character 'B' is the equivalent of the Hex number 42 (Number 66 in our normal decimal system and is not seen in Hex Editor). The bytes which cannot be represented in ASCII is shown as a dot (.) in the right pane. One who dosen't know how to convert Hex to decimal (though it is simple) can use the Windows calculator in Scientific mode. ![]() Important: Long Integer (4 bytes) is represented in a Hex file from Right to Left (As Arabic is written). For example, the Hex representation of Long Intger (0A) (00) (00) (00) is actually (00) (00) (00) (0A). In the above example, width and height of the HelloWorld.BMP is both 10 pixels. 10 in decimal is (0A) or (00) (00) (00) (0A) in Hexadecimal. As we all know, the zeroes in the left side will not alter the value of a number! And in a hex Editor it is seen in reverse! The reverse rule and zero padding is applicable for data type 'word' also (2 bytes). Zeroes are padded to maintain the size of the data type. By now, we undestood the secret of Hex editing. The Data Part The data of a BMP is stored as Pixels(Picture Elements). A Pixel is a combination of 3 bytes.1 byte for Blue, 1 byte for Green and 1 byte for Red. The maximum value for each is 'FF' in Hex (255 in Decimal). Pure Red is Blue - 00, Green - 00 and Red in FF. In other words, (00) (00) (FF) will represent a red pixel. In our demonstration we are writing a pale magenta BMP. So, it would be, (FF) (CC) (FF). The first FF for Blue, CC for Green and the last FF for Red. Few things to remember 1) Unfortunately, BMP data is not written from left to right -> left to right etc (like we read a book), insted it is written from right to left -> right to left etc from Bottom to Top. That means writing of pixel data starts from Bottom Right and ends in Top Left. In our example the problem will not reflect, since all the 100 (10 x 10) pixels are same pale magenta! 2) The number of bytes of the Picture Data in one each row should be a multiple of 4. We have to padd extra zeroes to kame it a multiple of 4. In our case, the picture's width is 10 pixels. So find the remainder of the division 10 / 4. The remainder is 2. So TWO zero bytes should be padded with each row's data. The following is the program source you can use to test your own bitmap image format. Place a TButton on the Form and paste the code below...
|
case VK_F2:
{
static OPENFILENAME ofn;
static char szFile[256];
static char szFileTitle[256];
static char CustomFilter;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrCustomFilter = (LPSTR)CustomFilter;
ofn.Flags=OFN_OVERWRITEPROMPT;
ofn.lpstrTitle="Сохрание карты";
char szFilter[256]="Файлы IRKLAB MAP EDITOR \0*.bmp";
ofn.lpstrFilter=szFilter;
szFile[0]='\0';
if(GetSaveFileName(&ofn))
{
strcat(szFile,".bmp");
char str[512];
strcpy(str,"Путь:\t");
strcat(str,szFile);
strcat(str,"\n\nИмя созранёной карты:\t");
strcat(str,szFileTitle);
HANDLE hFile;
DWORD RW;
// Объявим нужные структуры
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
BYTE Palette [1024];
// Пусть у нас будет картинка размером 52 x 40 пикселей
int Width = 52;
int Height = 40;
memset (Palette, 0, 1024);// В палитре у нас нули
RGBTRIPLE color[2080];
int t1=0;
int t2=39;
// Дальше с примудростями.
for(i=0;i
{
for(j=0;j
{
color[a[j]].rgbtRed = b1[t1][t2];
color[a[j]].rgbtGreen = b2[t1][t2];
color[a[j]].rgbtBlue = b3[t1][t2];
t1++;
}
t1=0;
t2--;
}
// Заполним их
memset (&bfh, 0, sizeof(bfh));
bfh.bfType = 0x4D42;// Обозначим, что это bmp 'BM'
bfh.bfOffBits = sizeof(bfh) + sizeof(bih) + 1024;// Палитра занимает 1Kb, но мы его испоьзовать не будем
memset (&bih, 0, sizeof(bih));
bih.biSize = sizeof(bih);// Так положено
bih.biBitCount = 24;// 16 бит на пиксель
bih.biCompression = BI_RGB;// Без сжатия
bih.biHeight = Height;
bih.biWidth = Width;
bih.biPlanes = 1;
// Должно быть 1
// А остальные поля остаются 0
hFile = CreateFile (szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
// Запишем заголовки
WriteFile (hFile, &bfh, sizeof (bfh), &RW, NULL);
WriteFile (hFile, &bih, sizeof (bih), &RW, NULL);
// Запишем палитру
WriteFile (hFile, Palette, 1024, &RW, NULL);
for (i = 0; i < Height; i++)
{
for (j = 0; j < Width; j++)
{
WriteFile (hFile, &color[a[j]], sizeof(color[a[j]]), &RW, NULL);
}
// Выровняем по границе
WriteFile (hFile, Palette, Width % 4, &RW, NULL);
}
CloseHandle(hFile);
}
}
break; case VK_F3:
{
int Width = 52;
int Height = 40;
static OPENFILENAME ofn;
static char szFile[256];
static char szFileTitle[256];
static char CustomFilter;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrCustomFilter = (LPSTR)CustomFilter;
ofn.Flags=OFN_EXPLORER|OFN_CREATEPROMPT|OFN_ALLOWMULTISELECT;
ofn.lpstrTitle="Открытие карты";
char szFilter[256]="Файлы IRKLAB MAP EDITOR \0*.bmp";
ofn.lpstrFilter=szFilter;
szFileTitle[0]='\0';
szFile[0]='\0';
if(GetOpenFileName(&ofn))
{
char str[512];
strcpy(str,"Список имен файлов:\t");
for (int i=0;i<255;i++)
{
if(szFile=='\0' && szFile[i+1]=='\0')
break;
if(szFile=='\0') szFile='\n';
}
strcat(str,szFile);
strcat(str,"\n\nИмя карты:\t");
strcat(str,szFileTitle);
}
HBITMAP hbm;// дескриптор битмапа
BITMAP bm;// битмап
COLORREF pix;// цвет
hbm = (HBITMAP) LoadImage (NULL, szFile,
IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION); // загрузка битмап из файла
GetObject (hbm, sizeof(bm), &bm); // получение объекта из битмапа
HDC hdc = CreateCompatibleDC (0); // создание DC для извлечения информации по цветам
SelectObject (hdc, hbm);// выбор объекта битмапа
for (i=0;i
{
for(j=0;j
{
pix = GetPixel (hdc,i, j);
b1
b2[j]=(GetGValue(pix));
b3[j]=(GetBValue(pix));
}
}
ReleaseDC (0, hdc);// освобождение дескриптора
DeleteObject(hbm);// удаление объекта битмапа
}
break;
Комментариев нет:
Отправить комментарий