Difference between revisions of "Creating Icons from Bitmaps"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
(Created page with 'The following program can create single icons from square bitmaps up to 128 X 128. {{CodeStart}} '' '' {{Cl|CONST}} dat = 14~& {{Cl|DIM}} Dword {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|…')
 
imported>Odin
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
The following program can create single icons from square bitmaps up to 128 X 128.
+
The following program can create a single icon from square bitmaps up to 128 X 128. Best to use sizes 32 X 32 or 64 X 64.
  
  
Line 5: Line 5:
 
{{Cl|CONST}} dat = 14~&
 
{{Cl|CONST}} dat = 14~&
 
{{Cl|DIM}} Dword {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
 
{{Cl|DIM}} Dword {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
{{Cl|DIM}} siz {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
+
{{Cl|DIM}} size {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
 
{{Cl|DIM}} wide {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
 
{{Cl|DIM}} wide {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
 
{{Cl|DIM}} high {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
 
{{Cl|DIM}} high {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}
Line 21: Line 21:
 
{{Cl|CLOSE}} 2
 
{{Cl|CLOSE}} 2
 
{{Cl|OPEN}} ICO$ {{Cl|FOR...NEXT|FOR}} {{Cl|BINARY}} {{Cl|AS}} 2
 
{{Cl|OPEN}} ICO$ {{Cl|FOR...NEXT|FOR}} {{Cl|BINARY}} {{Cl|AS}} 2
{{Cl|GET (graphics statement)|GET}} 1, 1 + 2, siz 'skip "BM" in bitmap
+
{{Cl|GET}} 1, 1 + 2, size 'skip "BM" in bitmap
siz = siz - 14 '14 bytes not used
+
size = size - 14 '14 bytes not used
  
 
Word = 0
 
Word = 0
PUT 2, 1, Word 'reserved
+
{{Cl|PUT}} 2, 1, Word 'reserved
 
Word = 1
 
Word = 1
PUT 2, , Word 'resource id 1 as icon, 2 as cursor
+
{{Cl|PUT}} 2, , Word 'resource id 1 as icon, 2 as cursor
PUT 2, , Word 'icon count in resource
+
{{Cl|PUT}} 2, , Word 'icon count in resource
{{Cl|GET (graphics statement)|GET}} 1, 1 + dat + 4, wide 'skip header size in BMP
+
{{Cl|GET}} 1, 1 + dat + 4, wide 'skip header size in BMP
 
{{Cl|SELECT CASE}} wide
 
{{Cl|SELECT CASE}} wide
 
   {{Cl|CASE}} {{Cl|IS}} < {{Cl|&H}}100: Byte = wide '< 256
 
   {{Cl|CASE}} {{Cl|IS}} < {{Cl|&H}}100: Byte = wide '< 256
Line 35: Line 35:
 
   {{Cl|CASE ELSE}}: {{Cl|PRINT}} "bitmap is larger than 256 pixels.": {{Cl|END}}
 
   {{Cl|CASE ELSE}}: {{Cl|PRINT}} "bitmap is larger than 256 pixels.": {{Cl|END}}
 
{{Cl|END SELECT}}
 
{{Cl|END SELECT}}
PUT 2, , Byte 'wideth
+
{{Cl|PUT}} 2, , Byte 'width
{{Cl|GET (graphics statement)|GET}} 1, , high
+
{{Cl|GET}} 1, , high
 
{{Cl|SELECT CASE}} high
 
{{Cl|SELECT CASE}} high
 
   {{Cl|CASE}} {{Cl|IS}} < {{Cl|&H}}100: Byte = high '< 256
 
   {{Cl|CASE}} {{Cl|IS}} < {{Cl|&H}}100: Byte = high '< 256
Line 42: Line 42:
 
   {{Cl|CASE ELSE}}: {{Cl|PRINT}} "bitmap is larger than 256 pixels.": {{Cl|END}}
 
   {{Cl|CASE ELSE}}: {{Cl|PRINT}} "bitmap is larger than 256 pixels.": {{Cl|END}}
 
{{Cl|END SELECT}}
 
{{Cl|END SELECT}}
PUT 2, , Byte 'highght
+
{{Cl|PUT}} 2, , Byte 'height
 
Byte = 0
 
Byte = 0
{{Cl|GET (graphics statement)|GET}} 1, 1 + dat + 14, Word 'number of colors from BPP
+
{{Cl|GET}} 1, 1 + dat + 14, Word 'number of colors from BPP
 
{{Cl|IF...THEN|IF}} Word < 8 {{Cl|THEN}} Byte = 2 ^ Word {{Cl|ELSE}} Byte = 0 '256 colors = 0, 4BPP = 16
 
{{Cl|IF...THEN|IF}} Word < 8 {{Cl|THEN}} Byte = 2 ^ Word {{Cl|ELSE}} Byte = 0 '256 colors = 0, 4BPP = 16
PUT 2, , Byte 'num of colors
+
{{Cl|PUT}} 2, , Byte 'num of colors
 
Byte = 0
 
Byte = 0
 
Dword = 0
 
Dword = 0
PUT 2, , Byte 'reserved as byte
+
{{Cl|PUT}} 2, , Byte 'reserved as byte
PUT 2, , Dword 'reserved 2 hotspots = 0
+
{{Cl|PUT}} 2, , Dword 'reserved 2 hotspots = 0
Dword = siz + (((wide * high) + 7) \ 8)
+
Dword = size + (((wide * high) + 7) \ 8)
PUT 2, , Dword 'size of data
+
{{Cl|PUT}} 2, , Dword 'size of data
 
Dword = 22 '6 byte header + 16
 
Dword = 22 '6 byte header + 16
PUT 2, , Dword 'offset
+
{{Cl|PUT}} 2, , Dword 'offset
  
'copy bitmap header down 14 bytes, palette and image info, but double highght
+
'copy bitmap header down 14 bytes, palette and image info, but double height
 
{{Cl|SEEK}} 1, 1 + dat 'BMP info header size
 
{{Cl|SEEK}} 1, 1 + dat 'BMP info header size
{{Cl|FOR...NEXT|FOR}} Dword = 1 {{Cl|TO}} siz 'actual size of data - 14 header bytes not used
+
{{Cl|FOR...NEXT|FOR}} Dword = 1 {{Cl|TO}} size 'actual BMP data from Header size on
   {{Cl|GET (graphics statement)|GET}} 1, , Byte
+
   {{Cl|GET}} 1, , Byte
 
   {{Cl|IF...THEN|IF}} Dword = 1 + 8 {{Cl|THEN}}
 
   {{Cl|IF...THEN|IF}} Dword = 1 + 8 {{Cl|THEN}}
 
     Word = high * 2 ' 2 times height
 
     Word = high * 2 ' 2 times height
 
     PUT 2, , Word
 
     PUT 2, , Word
 
     Dword = Dword + 1
 
     Dword = Dword + 1
     {{Cl|GET (graphics statement)|GET}} 1, , Byte
+
     {{Cl|GET}} 1, , Byte
 
   {{Cl|ELSE}}
 
   {{Cl|ELSE}}
     PUT 2, , Byte
+
     {{Cl|PUT}} 2, , Byte
 
   {{Cl|END IF}}
 
   {{Cl|END IF}}
 
{{Cl|NEXT}}
 
{{Cl|NEXT}}
  
'add null {{Cl|AND (boolean)|AND}} bitmask for full square image
+
'add null {{Cl|AND}} bitmask for full square image
 
Byte = 0
 
Byte = 0
BitsOver& = wide {{Cl|MOD}} 32
+
BitsOver& = wide {{Cl|MOD}} 32 'pad the AND mask for minimum of 4 byte width
{{Cl|IF...THEN|IF}} BitsOver& {{Cl|THEN}} {{Cl|AND (boolean)|AND}}pad& = (32 - BitsOver&) {{Cl|ELSE}} {{Cl|AND (boolean)|AND}}pad& = 0
+
{{Cl|IF...THEN|IF}} BitsOver& {{Cl|THEN}} ANDpad& = (32 - BitsOver&) {{Cl|ELSE}} ANDpad& = 0
{{Cl|FOR...NEXT|FOR}} Dword = 1 {{Cl|TO}} high * (wide + {{Cl|AND (boolean)|AND}}pad&) \ 8
+
{{Cl|FOR...NEXT|FOR}} Dword = 1 {{Cl|TO}} high * (wide + ANDpad&) \ 8
   PUT 2, , Byte
+
   {{Cl|PUT}} 2, , Byte
 
{{Cl|NEXT}}
 
{{Cl|NEXT}}
 
{{Cl|CLOSE}}
 
{{Cl|CLOSE}}
 
{{Cl|SYSTEM}}
 
{{Cl|SYSTEM}}
 
{{CodeEnd}}
 
{{CodeEnd}}
{{small|Public domain, November 2011, Michael Calkins}}
+
{{small|Adapted from code by Michael Calkins, Public domain, November 2011}}
  
  
 
''See also:''
 
''See also:''
 +
* [[_ICON]], [[$EXEICON]]
 +
* [[SaveIcon32]] {{text|(saves any image to icon)}}
 
* [[Icons and Cursors]]
 
* [[Icons and Cursors]]
 
* [[Bitmaps]]
 
* [[Bitmaps]]

Latest revision as of 03:49, 22 January 2020

The following program can create a single icon from square bitmaps up to 128 X 128. Best to use sizes 32 X 32 or 64 X 64.


CONST dat = 14~& DIM Dword AS _UNSIGNED LONG DIM size AS _UNSIGNED LONG DIM wide AS _UNSIGNED LONG DIM high AS _UNSIGNED LONG DIM Word AS _UNSIGNED INTEGER DIM Byte AS _UNSIGNED _BYTE DO LINE INPUT "Enter existing BMP file name to convert to icon: ", BMP$ LOOP UNTIL _FILEEXISTS(BMP$) DO LINE INPUT "Enter ICO file name to create (must not exist): ", ICO$ LOOP UNTIL _FILEEXISTS(ICO$) = 0 OPEN BMP$ FOR BINARY ACCESS READ AS 1 'BITMAP name OPEN ICO$ FOR OUTPUT AS 2 'destination icon name CLOSE 2 OPEN ICO$ FOR BINARY AS 2 GET 1, 1 + 2, size 'skip "BM" in bitmap size = size - 14 '14 bytes not used Word = 0 PUT 2, 1, Word 'reserved Word = 1 PUT 2, , Word 'resource id 1 as icon, 2 as cursor PUT 2, , Word 'icon count in resource GET 1, 1 + dat + 4, wide 'skip header size in BMP SELECT CASE wide CASE IS < &H100: Byte = wide '< 256 CASE &H100: Byte = 0 CASE ELSE: PRINT "bitmap is larger than 256 pixels.": END END SELECT PUT 2, , Byte 'width GET 1, , high SELECT CASE high CASE IS < &H100: Byte = high '< 256 CASE &H100: Byte = 0 '256 = 0 CASE ELSE: PRINT "bitmap is larger than 256 pixels.": END END SELECT PUT 2, , Byte 'height Byte = 0 GET 1, 1 + dat + 14, Word 'number of colors from BPP IF Word < 8 THEN Byte = 2 ^ Word ELSE Byte = 0 '256 colors = 0, 4BPP = 16 PUT 2, , Byte 'num of colors Byte = 0 Dword = 0 PUT 2, , Byte 'reserved as byte PUT 2, , Dword 'reserved 2 hotspots = 0 Dword = size + (((wide * high) + 7) \ 8) PUT 2, , Dword 'size of data Dword = 22 '6 byte header + 16 PUT 2, , Dword 'offset 'copy bitmap header down 14 bytes, palette and image info, but double height SEEK 1, 1 + dat 'BMP info header size FOR Dword = 1 TO size 'actual BMP data from Header size on GET 1, , Byte IF Dword = 1 + 8 THEN Word = high * 2 ' 2 times height PUT 2, , Word Dword = Dword + 1 GET 1, , Byte ELSE PUT 2, , Byte END IF NEXT 'add null AND bitmask for full square image Byte = 0 BitsOver& = wide MOD 32 'pad the AND mask for minimum of 4 byte width IF BitsOver& THEN ANDpad& = (32 - BitsOver&) ELSE ANDpad& = 0 FOR Dword = 1 TO high * (wide + ANDpad&) \ 8 PUT 2, , Byte NEXT CLOSE SYSTEM

Adapted from code by Michael Calkins, Public domain, November 2011


See also:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page