Difference between revisions of "SaveIcon32"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 46: Line 46:
 
lastsource& = {{Cl|_SOURCE}}
 
lastsource& = {{Cl|_SOURCE}}
 
{{Cl|_SOURCE}} 0
 
{{Cl|_SOURCE}} 0
'{{Cl|IF...THEN|IF}} ((x& * 3) {{Cl|MOD}} 4) {{Cl|THEN}} padder$ = {{Cl|SPACE$}}(4 - ((x& * 3) {{Cl|MOD}} 4)) 'not used in 32 X 32
 
 
{{Cl|FOR...NEXT|FOR}} py& = y& - 1 {{Cl|TO}} 0 {{Cl|STEP}} -1 ' read JPG image pixel color data
 
{{Cl|FOR...NEXT|FOR}} py& = y& - 1 {{Cl|TO}} 0 {{Cl|STEP}} -1 ' read JPG image pixel color data
 
   r$ = ""
 
   r$ = ""
Line 53: Line 52:
 
     {{Cl|IF...THEN|IF}} bytesperpixel& = 1 {{Cl|THEN}} r$ = r$ + {{Cl|CHR$}}(c&) {{Cl|ELSE}} r$ = r$ + {{Cl|LEFT$}}({{Cl|MKL$}}(c&), 3)
 
     {{Cl|IF...THEN|IF}} bytesperpixel& = 1 {{Cl|THEN}} r$ = r$ + {{Cl|CHR$}}(c&) {{Cl|ELSE}} r$ = r$ + {{Cl|LEFT$}}({{Cl|MKL$}}(c&), 3)
 
   {{Cl|NEXT}} px&
 
   {{Cl|NEXT}} px&
   d$ = d$ + r$ '+ padder$
+
   d$ = d$ + r$  
 
{{Cl|NEXT}} py&
 
{{Cl|NEXT}} py&
a$ = {{Cl|STRING$}}(bp% * x& * y& \ 8, 0) 'totally black {{Cl|AND (boolean)|AND}} mask
+
a$ = {{Cl|STRING$}}(bp% * y& * x& \ 8, 0) 'totally black {{Cl|AND (boolean)|AND}} mask
 
d$ = d$ + a$
 
d$ = d$ + a$
 
{{Cl|_SOURCE}} lastsource&
 
{{Cl|_SOURCE}} lastsource&
Line 73: Line 72:
 
: [[SPACE$]](2) gives the icon entry header the 32 X 32 dimensions as spaces are [[CHR$]](32). Smaller icons would require padding.  
 
: [[SPACE$]](2) gives the icon entry header the 32 X 32 dimensions as spaces are [[CHR$]](32). Smaller icons would require padding.  
 
: In the header, ???? is later replaced with size data. If the image is 24/32 BPP, the size of the image data will triple with '''bp%'''.
 
: In the header, ???? is later replaced with size data. If the image is 24/32 BPP, the size of the image data will triple with '''bp%'''.
 +
<center>'''Note: Icon sizes that are not multiples of 32 require AND mask padding to multiples of 4 bytes!'''</center>
  
  

Revision as of 16:43, 6 November 2011

The following Icon creation procedure was adapted from Galleon's SAVEIMAGE sup-procedure that creats 8 BPP or 32 bit bitmaps:

DO LINE INPUT "Use 8 bit(256 colors) or 32 bit image? ", bit$ mode% = VAL(bit$) LOOP UNTIL mode% = 8 OR mode% = 32 IF mode% = 8 THEN mode% = 256 SCREEN _NEWIMAGE(640, 480, mode%) DO LOCATE 10, 10: LINE INPUT "Enter the image file name to convert to an icon: ", BMP$ CLS LOOP UNTIL _FILEEXISTS(BMP$) img& = _LOADIMAGE(BMP$) IF img& = -1 OR img& = 0 THEN LOCATE 15, 25: PRINT "Image failed to load!": END CLS , _RGB(255, 255, 255) IF mode% = 256 THEN _COPYPALETTE (img&) _PUTIMAGE (0, 0)-(31, 31), img&, 0 LOCATE 20, 10: PRINT "Do you want to make an icon out of the image? (Y/N)" K$ = INPUT$(1) IF UCASE$(K$) <> "Y" THEN END SaveFile$ = LEFT$(BMP$, INSTR(BMP$, ".")) + "ico" SaveIcon32 img&, SaveFile$ END '----------------------------------------------------------------------------------- SUB SaveIcon32 (image AS LONG, filename AS STRING) bytesperpixel& = _PIXELSIZE(image&) IF bytesperpixel& = 0 THEN PRINT "Text modes unsupported!": END IF bytesperpixel& = 1 THEN bpp& = 8: bp% = 1 ELSE bpp& = 24: bp% = 3 x& = 32 '_WIDTH(image&) 'not to exceed 128 X 128 y& = 32 '_HEIGHT(image&) 'Icon, Entry & BMP headers: 32 x 32 colors size - 22 offset b$= MKI$(0)+MKI$(1)+MKI$(1)+SPACE$(2)+CHR$(0)+CHR$(0)+MKI$(0)+MKI$(0)+"????"+MKL$(22) _ +MKL$(40)+MKL$(x&)+MKL$(2 * y&)+MKI$(1)+MKI$(bpp&)+MKL$(0)+MKL$(x& * y& * bp%)+MKL$(0)+MKL$(0)+MKL$(0)+MKL$(0) 'header 2 * height planes BPP XOR size IF bytesperpixel& = 1 THEN FOR c& = 0 TO 255 ' read BGR color settings from image + 1 byte spacer(CHR$(0)) cv& = _PALETTECOLOR(c&, image&) ' color attribute to read. b$ = b$ + CHR$(_BLUE32(cv&)) + CHR$(_GREEN32(cv&)) + CHR$(_RED32(cv&)) + CHR$(0) 'spacer byte NEXT END IF lastsource& = _SOURCE _SOURCE 0 FOR py& = y& - 1 TO 0 STEP -1 ' read JPG image pixel color data r$ = "" FOR px& = 0 TO x& - 1 c& = POINT(px&, py&) IF bytesperpixel& = 1 THEN r$ = r$ + CHR$(c&) ELSE r$ = r$ + LEFT$(MKL$(c&), 3) NEXT px& d$ = d$ + r$ NEXT py& a$ = STRING$(bp% * y& * x& \ 8, 0) 'totally black AND mask d$ = d$ + a$ _SOURCE lastsource& 'MID$(b$, 43, 4) = MKL$(LEN(d$)) 'raw data size of XOR mask(32 * 32 * bp%) with AND mask b$ = b$ + d$ ' total file data bytes to create file MID$(b$, 15, 4) = MKL$(LEN(b$) - 22) 'size of image data minus Icon and Entry headers(Entry) IF LCASE$(RIGHT$(filename$, 4)) <> ".ico" THEN ext$ = ".ico" f& = FREEFILE OPEN filename$ + ext$ FOR OUTPUT AS #f&: CLOSE #f& ' erases an existing file OPEN filename$ + ext$ FOR BINARY AS #f& PUT #f&, , b$ CLOSE #f& END SUB

Code by Ted Weissgerber
Explanation: The icons created only have a black AND mask for simplicity so the background is changed to white using CLS.
SPACE$(2) gives the icon entry header the 32 X 32 dimensions as spaces are CHR$(32). Smaller icons would require padding.
In the header, ???? is later replaced with size data. If the image is 24/32 BPP, the size of the image data will triple with bp%.
Note: Icon sizes that are not multiples of 32 require AND mask padding to multiples of 4 bytes!


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page