SaveImage BMP RGB

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Markus2

Betreff: SaveImage BMP RGB

BeitragMo, Apr 07, 2008 22:47
Antworten mit Zitat
Benutzer-Profile anzeigen
Speichert ein Image als Bitmap Datei
als 24 Bit also RGB ohne Farbtabelle und ohne komprimierung
also ganz einfach Smile

Code: [AUSKLAPPEN]

Strict

'MR 08.04.2008

'1280 x 720 HD          = 2.764.800 Bytes je Bild = ca. 2.63 Mega Byte
'1920 x 1080 FULL HD

Test()
End

Function Test()
 
 Local img:TImage=LoadImage("100_0644.JPG")' "test.bmp")
 
 SaveImageBMPRGB(img,0,"testblitzmax.bmp")

End Function

'http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html

'The BITMAPFILEHEADER:
'start    size    name stdvalue purpose
'1       2       bfType          19778 must always be set To 'BM' to declare that this is a .bmp-file.
'3       4       bfSize          ?? specifies the size of the file in bytes.
'7       2       bfReserved1    0 must always be set To zero.
'9       2       bfReserved2    0 must always be set To zero.
'11    4       bfOffBits       1078 (128 Farben*8+14+40  )specifies the offset from the beginning of the file To the bitmap data.

'The BITMAPINFOHEADER:
'start    size    name          stdvalue    purpose
'15    4       biSize          40          specifies the size of the BITMAPINFOHEADER structure, in bytes.
'19    4       biWidth       100       specifies the width of the image, in pixels.
'23    4       biHeight       100       specifies the height of the image, in pixels.
'27    2       biPlanes       1          specifies the number of planes of the target device, must be set To zero.
'29    2       biBitCount       8          specifies the number of bits per pixel.
'31    4       biCompression    0          Specifies the Type of compression, usually set To zero (no compression).
'35    4       biSizeImage    0          specifies the size of the image data, in bytes. If there is no compression, it is valid To set this member To zero.
'39    4       biXPelsPerMeter 0          specifies the the horizontal pixels per meter on the designated targer device, usually set To zero.
'43    4       biYPelsPerMeter 0          specifies the the vertical pixels per meter on the designated targer device, usually set To zero.
'47    4       biClrUsed       0          specifies the number of colors used in the bitmap, If set To zero the number of colors is calculated using the biBitCount member.
'51    4       biClrImportant    0          specifies the number of color that are 'important' for the bitmap, if set to zero, all colors are important.

'The RGBQUAD array:
'The following table shows a single RGBQUAD structure:
'start    size    name       stdvalue    purpose
'1       1       rgbBlue    0-255      specifies the blue part of the color.
'2       1       rgbGreen    0-255      specifies the green part of the color.
'3       1       rgbRed       0-255      specifies the red part of the color.
'4       1       rgbReserved 0          must always be set To zero.

Function SaveImageBMPRGB:Int(img:TImage,frame:Int=0,name:String)
 
 Local pix:TPixmap=LockImage(img,frame,True,False)
 pix=ConvertPixmap(pix,PF_RGB888)

 'Byte   =1 Byte
 'Short   =2 Byte
 'Int   =4 Byte

 'z.B. 640x480x3=921600
 Local datasize:Int=(PixmapWidth(pix)*PixmapHeight(pix))*3 'RGB = 3 Byte je Pixel
 
 '14+40+921600=921654
 Local filelen:Int=14+40+datasize 'Anzahl Bytes ganze Datei

 Local x:Int
 Local y:Int

 Local S:TStream=OpenFile(name,False,True)
 If s Then
  '----------------------------------------------------
  'BITMAPFILEHEADER
  WriteShort s,19778 'BM
  WriteInt s,filelen 'gesamt länge in Bytes
  WriteShort s,0   'reserve
  WriteShort s,0    'reserve
  WriteInt s,54 '(14+40) 'bfOffBits  BITMAPFILEHEADER+BITMAPINFOHEADER
  '----------------------------------------------------
  'BITMAPINFOHEADER
  WriteInt s,40  'biSize 'Länge BITMAPINFOHEADER in Bytes
  WriteInt s,PixmapWidth(pix) 'biWidth
  WriteInt s,PixmapHeight(pix) 'biHeight
  WriteShort s,1 'biPlanes
  WriteShort s,24 'biBitCount RGB=24 Bit
  WriteInt s,0 'biCompression
  WriteInt s,datasize 'biSizeImage
  WriteInt s,0 'biXPelsPerMeter
  WriteInt s,0 'biYPelsPerMeter
  WriteInt s,0 'biClrUsed
  WriteInt s,0 'biClrImportant
  '----------------------------------------------------
  'RGBQUAD
  Local colred:Byte
  Local colgreen:Byte
  Local colblue:Byte
  Local RGB:Int
  For y=PixmapHeight(pix)-1 To 0 Step -1
  For x=0 To PixmapWidth(pix)-1
   RGB=ReadPixel(pix,x,y)
   colblue=(RGB & $000000FF:Int) 'Blue
   colgreen=Int((RGB & $0000FF00:Int) / $100:Int) 'Green
   colred=Int((RGB & $00FF0000:Int) / $10000:Int) 'Red
   WriteByte s,colblue  'B
   WriteByte s,colgreen 'G
   WriteByte s,colred   'R 
  Next
  Next
  '----------------------------------------------------
  CloseFile(s)
 EndIf
 If pix Then UnlockImage(img,frame)

 Return True

End Function
 

Laias

BeitragMi, Apr 09, 2008 19:14
Antworten mit Zitat
Benutzer-Profile anzeigen
Sieht echt Cool aus!
Die ganzen Header einzeln zu schreiben war bestimmt nicht leicht.
Gibt es eigentlich irgendwo ein verzeichniss von den ganzen headern im i-net?

mfg

Laias
http://laias.wordpress.com

BtbN

BeitragMi, Apr 09, 2008 19:22
Antworten mit Zitat
Benutzer-Profile anzeigen
Du weist aber, dass es ne Funktion namens SavePixmapPNG gibt?
Für BMP scheint es zwar nichts zu geben(beim überfliegen der doks nix gesehen), aber ich würde PNG eh vorziehen.

Suco-X

Betreff: ....

BeitragMi, Apr 09, 2008 19:52
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich denke dass es dennoch ein gutes Beispiel für den ein oder anderen ist. Eine Beschreibung des Formats findest du z.b. Hier , Laias. Musst halt nur wissen wie du die Datentypen der Variablen zu bmx konvertieren kannst bzw. was das passende äquivalent ist.
Mfg Suco
Intel Core 2 Quad Q8300, 4× 2500 MHz, 4096 MB DDR2-Ram, GeForce 9600GT 512 MB

Markus2

BeitragMi, Apr 09, 2008 21:33
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich brauchte es aber gerade in dieser Art und wollte es nicht vorenthalten .
Falls ihr das wo einbaut macht euch bitte auch selber Gedanken zu der Funktion .
Diese bfOffBits sind irgendwie Bytes , warum auch immer ...

Infos habe ich hier drüber bekommen:
http://www.wotsit.org/

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group