BMP Tutor - Ref-06 Padding Calculations

Category: BMP Tutor Series (API)

Date: 02-16-2022

Return to Index


 
'Per MSDN, the row (scanline) image data in a bitmap file must end on a Long
'(4 byte) boundary.  This snippet shows how to calculate the number of
'padding bytes needed to pad each row of data.  The bytes added are not
'used for display and need not be set to any particular values. The bytes
'are simply place holders required to meet API bimap reading requirements.
 
 
'Primary Code:
'The number of bytes per pixel for 24bit bitmaps is 3. A Long boundary
'is 4 bytes. Here are 6 different methods of calculating the padding
'(number of bytes) that must be added to ech row of pixel data.
 
1.   While (w*3 + padding) Mod 4 : Incr padding : Wend
 
2.   Padding = 4 - w*3 Mod 4
     If Padding = 4 Then padding = 0
 
3.   padding  = (4 - (w*3) Mod 4) Mod 4
 
4.   padding = Choose ((w*3 Mod 4)+1, 0, 3, 2, 1)
 
5.   padding = ((w*3 + 3) AND Not 3) - w*3
 
6.   padding = 3 - (address + 3) AND 3
 
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as DWord
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"100x100", 50,10,100,20
   Control Add Button, hDlg, 200,"99x99", 50,40,100,20
   Control Add Button, hDlg, 300,"98x98", 50,70,100,20
   Control Add Button, hDlg, 400,"97x97", 50,100,100,20
   Control Add Button, hDlg, 500,"96x96", 50,140,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local padding As Long
   Select Case CB.Msg
      Case %WM_Command
         Select Case CB.Ctl
            Case 100 : MsgBox Str$((4 - (100*3) Mod 4) Mod 4)
            Case 200 : MsgBox Str$((4 - (99*3) Mod 4) Mod 4)
            Case 300 : MsgBox Str$((4 - (98*3) Mod 4) Mod 4)
            Case 400 : MsgBox Str$((4 - (97*3) Mod 4) Mod 4)
            Case 500 : MsgBox Str$((4 - (96*3) Mod 4) Mod 4)
         End Select
   End Select
End Function
 
'gbs_00517
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm