AHK functions for some icon conversions?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

AHK functions for some icon conversions?

19 Jul 2018, 03:22

Hi!

Are there functions or scripts in the treasure chest of the AHK community which would convert
- an icon to a 16x16x32 icon
- an icon to a grayscale icon
or similar approaches/examples to convert icon files?

Thank you in advance!
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AHK functions for some icon conversions?

19 Jul 2018, 11:19

Use the command line tool convert.exe from ImageMagick.

Convert an icon to a grayscale icon:

Code: Select all

convert -colorspace gray input.ico output.ico
Convert an png to a multiple sized icon: (48x48,32x32,16x16)

Code: Select all

convert "input.png" -define icon:auto-resize="48,32,16" "output.ico"
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: AHK functions for some icon conversions?

20 Jul 2018, 04:38

@tmplinshi: Thank you, tmplinshi, but this is a third-party tool, my question refers to AHK solutions.

In the meantime I found some scripts which show e.g. how to extract an individual icon from a multi-image icon file, or how to convert an icon into a png file. I also found very helpful and detailed information about the icon resource structure: https://en.wikipedia.org/wiki/ICO_(file_format), but I still miss a description of the specific image data for each individual icon. An icon file has a 6 byte long header followed by image data for individual icons. Image data for each icon have a 16 byte long 'image directory' followed by specific image data. (So the section for the first included icon begins with byte 23 (offset 16) of the icon file.) Where could I find a detailed information about the structure of these specific image data?
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: AHK functions for some icon conversions?

26 Jul 2018, 11:48

Basically I would like to be able, by using only AHK methods (no third-party tools), to extract a single icon from an icon file and to do some conversions––to reduce the pixel size of the icon and to convert the icon to grayscale. (Transparency shouldn't be lost.)

I have found some concepts for some required steps, but not for all-–especially not for pixel-size and grayscale conversions. I suppose that for that an intermediate image format would be required, allowing to do these conversions by simple methods. Which image format this would be?

Thanks in advance for all tips.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK functions for some icon conversions?

26 Jul 2018, 13:49

- These links may be helpful:
graphics: create icons from scratch - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=35758
[converting bitmaps]
[Class][v1/v2] LoadPictureType - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 26#p167826
GDI_GrayscaleBitmap() - Converts GDI bitmap to Greyscale - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/8279 ... greyscale/
- I would imagine that some knowledge of the icon format, to read and write icon files would be useful, and some knowledge of converting (and resizing) bitmaps via the Gdip library.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: AHK functions for some icon conversions?

26 Jul 2018, 15:06

How about use the COM object of ImageMagick.

Code: Select all

#NoEnv
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%

ImageMagick_Init()

im := ComObjCreate("{5630BE5A-3F5F-4BCA-A511-AD6A6386CAC1}")

; Convert test.png to test.ico
im.Convert("test.png", "-define", "icon:auto-resize=48,32,16", "test.ico")

; Convert test.ico to test_gray.ico
im.Convert("test.ico", "-colorspace", "gray", "test_gray.ico")


ImageMagick_Init() {
	EnvSet, MAGICK_CODER_MODULE_PATH, %A_ScriptDir%\im\modules\coders\
	EnvSet, MAGICK_CODER_FILTER_PATH, %A_ScriptDir%\im\modules\filters\
	RegCOM("im\ImageMagickObject.manifest")
}

RegCOM(ManifestFile) {
	VarSetCapacity(actctx, 34, 0)
	NumPut(34, actctx, 0, "UInt")
	NumPut(&ManifestFile, actctx, 8, "UInt")

	hActCtx := DllCall("CreateActCtx", "ptr", &actctx, "ptr")
	if (hActCtx = INVALID_HANDLE_VALUE := -1) {
		throw, "INVALID_HANDLE_VALUE"
	}

	DllCall("ActivateActCtx", "ptr", hActCtx, "ptr*", cookie)
}
Download:
Last edited by tmplinshi on 26 Jul 2018, 16:10, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK functions for some icon conversions?

26 Jul 2018, 15:17

- Any icon viewers written for AutoHotkey would have a lot of the key logic, if anyone has some good recommendations.
- There is this version of IconEx which also links to other versions.
IconEx 1.4v /w DragNDrop + Quiet Extraction support - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=32750
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: AHK functions for some icon conversions?

01 Aug 2018, 07:19

@jeeswg: Thank you for your tips!

I am now able to do a lot of things with images (mainly by using gdip), but I still have not found a complete solution to my actual problem (as described in my IP).

I know how to extract icons, how to resize images, how to convert them e.g. to png. But I still don't know how to change the color depth, how to convert to grayscale, and--probably my greatest problem--how to save an image as an ico file in the specific format I need. Gdip_SaveBitmapToFile() allows to save the image in different file formats, e.g. as bmp or png, but not as ico.*)

I know that one 'kind' of ico files can be created simply by writting the header (6 byte) and the 'image directory' (16 byte) to a file and appending the content of a png file. Such ico files have very individual file sizes, which is not true for another 'kind' of ico files, where e.g. each icon file containing one 16x16 icon with a color depth of 32 bit has a size of 1150 byte. This 'common' format is exactly what I would need!!! And I suppose that in these 'common' ico files the image data is stored as bmp.

But until now I have not found a way how to create such icon files...*)


*) And I am wondering why there are so much difficulties with the ico-file format. Gdip functions are able to save image data as bmp and png, transparency seems not to be a problem, the header and the image directory of an ico file are simply to generate...
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK functions for some icon conversions?

01 Aug 2018, 07:46

What I had in mind was, read the file as binary, get a hold of any bitmap image data and manipulate it, write the file as binary.

Re. colour depth, some links:

Gdip: image binary data to hex string for OCR - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=35339
[MS-EMFPLUS]: PixelFormat Enumeration
https://msdn.microsoft.com/en-us/library/cc230858.aspx
saving bitmap as 24bit bmp - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/9687 ... 24bit-bmp/

Code: Select all

PixelFormatUndefined := 0x00000000
PixelFormat1bppIndexed := 0x00030101
PixelFormat4bppIndexed := 0x00030402
PixelFormat8bppIndexed := 0x00030803
PixelFormat16bppGrayScale := 0x00101004
PixelFormat16bppRGB555 := 0x00021005
PixelFormat16bppRGB565 := 0x00021006
PixelFormat16bppARGB1555 := 0x00061007
PixelFormat24bppRGB := 0x00021808
PixelFormat32bppRGB := 0x00022009
PixelFormat32bppARGB := 0x0026200A
PixelFormat32bppPARGB := 0x000E200B
PixelFormat48bppRGB := 0x0010300C
PixelFormat64bppARGB := 0x0034400D
PixelFormat64bppPARGB := 0x001A400E
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, mikeyww and 346 guests