Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Created January 31, 2022 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbintz/de4367d95aedce06b8d31b109c31973c to your computer and use it in GitHub Desktop.
Save johnbintz/de4367d95aedce06b8d31b109c31973c to your computer and use it in GitHub Desktop.
Patch for libicns 0.8.1 to add 128x128 PNG extraction support

This got libicns working well enough to extract 128x128 PNG images from a macOS resource file that just contained icons. It's not the cleanest fix, but it works to extract icons on my machine.

diff -ruN libicns-0.8.1/src/icns_element.c libicns-0.8.1-modified/src/icns_element.c
--- libicns-0.8.1/src/icns_element.c 2012-06-13 19:42:06.000000000 -0400
+++ libicns-0.8.1-modified/src/icns_element.c 2022-01-31 08:07:50.871844474 -0500
@@ -565,6 +565,7 @@
switch(iconType)
{
case ICNS_1024x1024_32BIT_ARGB_DATA:
+ case ICNS_128x128_32BIT_ARGB_DATA:
case ICNS_256x256_32BIT_ARGB_DATA:
case ICNS_512x512_32BIT_ARGB_DATA:
error = icns_image_to_jp2(imageIn,&newDataSize,&newDataPtr);
diff -ruN libicns-0.8.1/src/icns.h libicns-0.8.1-modified/src/icns.h
--- libicns-0.8.1/src/icns.h 2012-06-13 19:42:06.000000000 -0400
+++ libicns-0.8.1-modified/src/icns.h 2022-01-31 08:07:34.272082774 -0500
@@ -96,6 +96,7 @@
#define ICNS_512x512_32BIT_ARGB_DATA 0x69633039 // "ic09"
#define ICNS_256x256_32BIT_ARGB_DATA 0x69633038 // "ic08"
+#define ICNS_128x128_32BIT_ARGB_DATA 0x69633037 // "ic07"
#define ICNS_128X128_32BIT_DATA 0x69743332 // "it32"
#define ICNS_128X128_8BIT_MASK 0x74386D6B // "t8mk"
diff -ruN libicns-0.8.1/src/icns_image.c libicns-0.8.1-modified/src/icns_image.c
--- libicns-0.8.1/src/icns_image.c 2012-06-13 19:42:06.000000000 -0400
+++ libicns-0.8.1-modified/src/icns_image.c 2022-01-31 08:36:18.419892020 -0500
@@ -99,7 +99,7 @@
}
// We used the jp2 processor for these two, so we're done!
- if( (iconType == ICNS_256x256_32BIT_ARGB_DATA) || (iconType == ICNS_512x512_32BIT_ARGB_DATA) || (iconType == ICNS_1024x1024_32BIT_ARGB_DATA) ) {
+ if( (iconType == ICNS_128x128_32BIT_ARGB_DATA) || (iconType == ICNS_256x256_32BIT_ARGB_DATA) || (iconType == ICNS_512x512_32BIT_ARGB_DATA) || (iconType == ICNS_1024x1024_32BIT_ARGB_DATA) ) {
memcpy(imageOut,&iconImage,sizeof(icns_image_t));
if(iconElement != NULL) {
free(iconElement);
@@ -413,6 +413,7 @@
switch(iconType)
{
// 32-Bit Icon Image Data Types ( > 256px )
+ case ICNS_128x128_32BIT_ARGB_DATA:
case ICNS_256x256_32BIT_ARGB_DATA:
case ICNS_512x512_32BIT_ARGB_DATA:
case ICNS_1024x1024_32BIT_ARGB_DATA:
diff -ruN libicns-0.8.1/src/icns_utils.c libicns-0.8.1-modified/src/icns_utils.c
--- libicns-0.8.1/src/icns_utils.c 2012-06-13 19:42:06.000000000 -0400
+++ libicns-0.8.1-modified/src/icns_utils.c 2022-01-31 08:09:30.897405349 -0500
@@ -51,6 +51,7 @@
case ICNS_512x512_32BIT_ARGB_DATA:
return 22;
case ICNS_256x256_32BIT_ARGB_DATA:
+ case ICNS_128x128_32BIT_ARGB_DATA:
return 21;
case ICNS_128X128_8BIT_MASK:
return 20;
@@ -120,6 +121,8 @@
return ICNS_NULL_MASK;
case ICNS_256x256_32BIT_ARGB_DATA:
return ICNS_NULL_MASK;
+ case ICNS_128x128_32BIT_ARGB_DATA:
+ return ICNS_NULL_MASK;
// 32-bit image types - 8-bit mask type
case ICNS_128X128_32BIT_DATA:
@@ -240,6 +243,15 @@
iconInfo.iconPixelDepth = 8;
iconInfo.iconBitDepth = 32;
break;
+ case ICNS_128x128_32BIT_ARGB_DATA:
+ iconInfo.isImage = 1;
+ iconInfo.isMask = 0;
+ iconInfo.iconWidth = 128;
+ iconInfo.iconHeight = 128;
+ iconInfo.iconChannels = 4;
+ iconInfo.iconPixelDepth = 8;
+ iconInfo.iconBitDepth = 32;
+ break;
case ICNS_128X128_32BIT_DATA:
iconInfo.isImage = 1;
iconInfo.isMask = 0;
@@ -610,10 +622,14 @@
}
break;
case 128:
+ return ICNS_256x256_32BIT_ARGB_DATA;
+
+ /*
if(iconInfo.isImage == 1 || iconInfo.iconBitDepth == 32)
return ICNS_128X128_32BIT_DATA;
if(iconInfo.isMask == 1 || iconInfo.iconBitDepth == 8)
return ICNS_128X128_8BIT_MASK;
+ */
break;
case 256:
return ICNS_256x256_32BIT_ARGB_DATA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment