Skip to content

Instantly share code, notes, and snippets.

@nclettiere
Created March 13, 2022 08:28
Show Gist options
  • Select an option

  • Save nclettiere/7c0e7f6a637fa8bc684572dde5eeb092 to your computer and use it in GitHub Desktop.

Select an option

Save nclettiere/7c0e7f6a637fa8bc684572dde5eeb092 to your computer and use it in GitHub Desktop.
Converts images to a C style buffer header file
#######################################################################
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar
# 14 rue de Plaisance, 75014 Paris, France
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
#
########################################################################
#
# Author
# Nicolas Cabrera Lettiere -> https://github.com/nclettiere/
#
########################################################################
#
# About
# Converts images inside a directory to a C style const static char * buffer
#
#######################################################################
#
# Dependencies
# GraphicsMagick (gm.exe in the system PATH) with the convert module enabled.
# http://www.graphicsmagick.org/
#
########################################################################
#
# Notes
# Is opt to you to rename files with the same name :)
#
########################################################################
#
# Usage:
#
# .\to_buffer -dir .\dir_with_bunch_of_images (<- Loops trough the directory and just starts converting
# .\to_buffer -dir .\dir -out images.hpp (<- Add -out to modify the output file
# .\to_buffer -dir .\dir -recurse (<- Add -recurse to loop trough all files and directories
#
#
# Outputs smth like:
#
# #pragma once
#
# /*
# ButtonImage : [Button.tga]
# */
# #define ButtonExtent 5163
#
# static const unsigned char
# Button[]=
# {
# 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x00, 0x01, 0x40, 0x00, 0xF6, 0x50,
# ...
# }
# ...
#
#######################################################################
#
# TODO
# 1. Work on a single file or specified files
# 2. Remove gm dependency
# 3. Add optional suffixes to the generated variables
# 4. Linux support
#
########################################################################
param (
[string]$dir = $(throw "-dir is required."),
[string]$out,
[switch]$recurse = $false
)
if([string]::IsNullOrEmpty($out))
{
$file = Get-Item -Path $dir
$name = $file.Name
$out = "$name.hpp"
Write-Host "Default output file: $out -> (use '-output' to override)`r`n"
}
if($recurse)
{
$images = Get-ChildItem -Path $dir –Recurse -File
}
else
{
$images = Get-ChildItem -Path $dir -File
}
$converted_files = @{}
foreach($image in $images)
{
$temp_file = New-TemporaryFile
Remove-Item -Path $temp_file -Force
$temp_file_h = "$temp_file.h"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "gm.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "convert $image $temp_file_h"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
if($p.ExitCode -ne 0)
{
Write-Error $stderr
$(throw "Failed to convert image to C buffer. Internal GM error.")
}
else
{
$converted_files.Add($image, $temp_file_h) # key, value
}
}
# Create the actual output file
New-Item $out
Add-Content -Path $out -Value "//////////////////////////////////////////////////////////`r`n//`tAuto Generated source file with tool: img2buff.ps1 //`r`n//////////////////////////////////////////////////////////`r`n"
Add-Content -Path $out -Value "#pragma once`r`n"
# Append the formatted buffer to the output one by one
$converted_files.GetEnumerator() | ForEach-Object{
$filepath = $_.key
$name = $filepath.Basename
$contents = Get-Content -Path $_.value
$title = 'Logo image declaration (GIF format).'
$extent = 'LogoImageExtent'
$variable = 'LogoImage'
$format_title = "$($name) : [$($filepath.Name)]"
$format_extent = "$($name)Extent"
$contents.replace($title, $format_title).replace($extent, $format_extent).replace($variable, $name) | Add-Content -Path $out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment