/* * Image Converter - Command line tool * * Usage: cell run convert.ce * Example: cell run convert.ce lenna.png lenna.qoi * * Supported formats: * - Decode: png, jpg, bmp, tga, gif, psd, qoi, ase/aseprite * - Encode: png, jpg, bmp, tga, qoi */ var io = use('fd') var png = use('png') var jpg = use('jpg') var bmp = use('bmp') var tga = use('tga') var gif = use('gif') var psd = use('psd') var qoi = use('qoi') var aseprite = use('aseprite') if (length(args) < 2) { log.console("Usage: cell run convert.ce ") log.console("Example: cell run convert.ce lenna.png lenna.qoi") log.console("Decode: png, jpg, bmp, tga, gif, psd, qoi, ase/aseprite") log.console("Encode: png, jpg, bmp, tga, qoi") $stop() } var input_file = args[0] var output_file = args[1] function ext_lower(path) { var e = array(path, '.')[] if (!e) return null return lower(e) } function decode_image(blob, ext) { if (ext == 'png') return png.decode(blob) if (ext == 'jpg' || ext == 'jpeg') return jpg.decode(blob) if (ext == 'bmp') return bmp.decode(blob) if (ext == 'tga') return tga.decode(blob) if (ext == 'gif') return gif.decode(blob) if (ext == 'psd') return psd.decode(blob) if (ext == 'qoi') return qoi.decode(blob) if (ext == 'ase' || ext == 'aseprite') return aseprite.decode(blob) // Try common decoders as a fallback var decoders = [png.decode, jpg.decode, qoi.decode, aseprite.decode] var i = 0 var r = null var fn = function() { r = decoders[i](blob) } disruption { r = null } for (i = 0; i < length(decoders); i++) { fn() if (r) return r } return null } function encode_image(img, ext) { if (ext == 'png') return png.encode(img) if (ext == 'jpg' || ext == 'jpeg') return jpg.encode(img) if (ext == 'bmp') return bmp.encode(img) if (ext == 'tga') return tga.encode(img) if (ext == 'qoi') { if (!img.format) img.format = "rgba32" return qoi.encode(img) } return null } log.console("Loading input image: " + input_file) var input_blob = io.slurp(input_file) if (!input_blob) { log.console("Error: Could not load input file: " + input_file) $stop() } var input_ext = ext_lower(input_file) var decoded = decode_image(input_blob, input_ext) if (!decoded) { log.console("Error: Failed to decode image: " + input_file) $stop() } log.console("Image loaded: " + text(decoded.width) + "x" + text(decoded.height) + " pixels") var output_ext = ext_lower(output_file) if (!output_ext) { log.console("Error: Could not determine output format from: " + output_file) $stop() } log.console("Converting to format: " + output_ext) var encoded = encode_image(decoded, output_ext) if (!encoded) { log.console("Error: Failed to encode image to format: " + output_ext) $stop() } var success = io.slurpwrite(output_file, encoded.pixels) if (!success) { log.console("Error: Could not save output file: " + output_file) $stop() } log.console("Successfully converted " + input_file + " to " + output_file) log.console("Output size: " + text(length(encoded.pixels)) + " bytes")