Add simple NetWM icon-prep utility

This commit is contained in:
Jack Andersen 2016-01-27 13:27:56 -10:00
parent 42c04d1f90
commit ea51d109a9
4 changed files with 178 additions and 1 deletions

View File

@ -26,6 +26,10 @@ elseif(APPLE)
set(PLAT_SRCS platforms/mac/mainicon.icns)
set_source_files_properties(platforms/mac/mainicon.icns PROPERTIES
MACOSX_PACKAGE_LOCATION Resources)
elseif(UNIX)
add_subdirectory(platforms/freedesktop)
declare_wmicon_target()
set(PLAT_SRCS mainicon_netwm.c)
endif()
add_executable(urde WIN32 MACOSX_BUNDLE

View File

@ -0,0 +1,18 @@
include_directories(${LIBPNG_INCLUDE_DIR})
add_executable(mkwmicon mkwmicon.c)
target_link_libraries(mkwmicon ${PNG_LIB} ${ZLIB_LIBRARIES})
macro(declare_wmicon_target)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/Editor/platforms/freedesktop/mainicon_netwm.bin
COMMAND $<TARGET_FILE:mkwmicon>
ARGS ${CMAKE_BINARY_DIR}/Editor/platforms/freedesktop/mainicon_netwm.bin
DEPENDS
${CMAKE_SOURCE_DIR}/Editor/platforms/freedesktop/128x128/apps/urde.png
${CMAKE_SOURCE_DIR}/Editor/platforms/freedesktop/64x64/apps/urde.png
${CMAKE_SOURCE_DIR}/Editor/platforms/freedesktop/48x48/apps/urde.png
${CMAKE_SOURCE_DIR}/Editor/platforms/freedesktop/32x32/apps/urde.png
${CMAKE_SOURCE_DIR}/Editor/platforms/freedesktop/16x16/apps/urde.png
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/Editor/platforms/freedesktop
COMMENT "Generating mainicon_netwm.bin")
bintoc(mainicon_netwm.c ${CMAKE_BINARY_DIR}/Editor/platforms/freedesktop/mainicon_netwm.bin MAINICON_NETWM)
endmacro()

View File

@ -0,0 +1,155 @@
/* clang -o mkwmicon -lpng mkwmicon.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <png.h>
static int CountBits(uint32_t n)
{
int ret = 0;
for (int i=0 ; i<32 ; ++i)
if (((n >> i) & 1) != 0)
++ret;
return ret;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: makewmicon <out.bin>\n");
return 1;
}
FILE* ofp = fopen(argv[1], "wb");
if (!ofp)
{
fprintf(stderr, "'%s' is not able to be opened for writing as a regular file\n", argv[1]);
return 1;
}
png_bytep row = malloc(4 * 128);
static const int DIMS[] =
{
16,
32,
48,
64,
128,
0
};
char command[2048];
for (const int* d = DIMS ; *d != 0 ; ++d)
{
printf("Rendering main icon @%dx%d\n", *d, *d);
fflush(stdout);
snprintf(command, 2048, "%dx%d/apps/urde.png", *d, *d);
FILE* fp = fopen(command, "rb");
if (!fp)
{
fprintf(stderr, "unable to open '%s' for reading\n", command);
fclose(ofp);
return 1;
}
char header[8];
fread(header, 1, 8, fp);
if (png_sig_cmp((png_const_bytep)header, 0, 8))
{
fprintf(stderr, "invalid PNG signature in '%s'\n", command);
fclose(fp);
fclose(ofp);
return 1;
}
png_structp pngRead = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!pngRead)
{
fprintf(stderr, "unable to initialize libpng\n");
fclose(fp);
fclose(ofp);
return 1;
}
png_infop info = png_create_info_struct(pngRead);
if (!info)
{
fprintf(stderr, "unable to initialize libpng info\n");
fclose(fp);
fclose(ofp);
return 1;
}
if (setjmp(png_jmpbuf(pngRead)))
{
fprintf(stderr, "unable to initialize libpng I/O for '%s'\n", command);
fclose(fp);
fclose(ofp);
return 1;
}
png_init_io(pngRead, fp);
png_set_sig_bytes(pngRead, 8);
png_read_info(pngRead, info);
png_uint_32 width = png_get_image_width(pngRead, info);
png_uint_32 height = png_get_image_height(pngRead, info);
png_byte colorType = png_get_color_type(pngRead, info);
png_byte bitDepth = png_get_bit_depth(pngRead, info);
if (colorType != PNG_COLOR_TYPE_RGB_ALPHA)
{
fprintf(stderr, "'%s' is not in RGBA color mode\n", command);
fclose(fp);
fclose(ofp);
return 1;
}
if (bitDepth != 8)
{
fprintf(stderr, "'%s' is not 8 bits-per-channel\n", command);
fclose(fp);
fclose(ofp);
return 1;
}
if (setjmp(png_jmpbuf(pngRead)))
{
fprintf(stderr, "unable to read image in '%s'\n", command);
fclose(fp);
fclose(ofp);
return 1;
}
unsigned long lWidth = width;
unsigned long lHeight = height;
fwrite(&lWidth, 1, sizeof(lWidth), ofp);
fwrite(&lHeight, 1, sizeof(lHeight), ofp);
for (png_uint_32 r=0 ; r<height ; ++r)
{
png_read_row(pngRead, row, NULL);
for (int i=0 ; i<width ; ++i)
{
unsigned long px;
px = row[i*4+2];
px |= row[i*4+1] << 8;
px |= row[i*4] << 16;
px |= row[i*4+3] << 24;
fwrite(&px, 1, sizeof(unsigned long), ofp);
}
}
png_destroy_read_struct(&pngRead, &info, NULL);
fclose(fp);
}
free(row);
fclose(ofp);
return 0;
}

2
hecl

@ -1 +1 @@
Subproject commit e09c0b4d73182282b4243a8de3685600fc47e226
Subproject commit f49ccd7fce8905bc5c04e2f2de1d0f18155eba2d