CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/native/tools/zimtool.cpp
Views: 1401
#include <cstring>1#include <cstdlib>2#include <cstdio>34#include "Common/Data/Format/PNGLoad.h"5#include "Common/Data/Format/ZIMLoad.h"6#include "Common/Data/Format/ZIMSave.h"78#include "Common/Common.h"910char magic[5] = "ZIMG";1112const char *format_strings[4] = { "8888", "4444", "565", "ETC1" };13int formats[3] = { ZIM_RGBA8888, ZIM_RGBA4444, ZIM_RGB565 };1415void printusage() {16fprintf(stderr, "Usage: zimtool infile.png outfile.zim [-f=FORMAT] [-m] [-g] [-z[LEVEL]]\n");17fprintf(stderr, "Formats: 8888 4444 565 ETC1\n");18}1920int filesize(const char *filename) {21FILE *f = fopen(filename, "rb");22fseek(f, 0, SEEK_END);23int sz = ftell(f);24fclose(f);25return sz;26}2728int main(int argc, char **argv) {29// Parse command line arguments.30const char *FLAGS_infile;31const char *FLAGS_outfile;32if (argc >= 3) {33FLAGS_infile = argv[1];34FLAGS_outfile = argv[2];35} else {36fprintf(stderr, "ERROR: Not enough parameters.\n");37printusage();38return 1;39}4041int flags = 0;42int level = 0;43bool format_set = false;44for (int i = 3; i < argc; i++) {45if (argv[i][0] != '-') {46fprintf(stderr, "Additional arguments must start with '-'\n");47return 1;48}49switch (argv[i][1]) {50case 'm':51flags |= ZIM_HAS_MIPS;52// Generates mips directly here. We can generate gamma53// corrected mips, and mips for ETC1.54break;55case 'g':56flags |= ZIM_GEN_MIPS;57break;58case 'c':59flags |= ZIM_CLAMP;60break;61case 'f':62{63for (int j = 0; j < 4; j++) {64if (!strcmp(format_strings[j], argv[i] + 3)) {65flags |= j;66format_set = true;67}68}69}70break;71case 'z':72flags |= ZIM_ZSTD_COMPRESSED;73if (argv[i][2] != '\0') {74int pos = 2;75while (argv[i][pos] >= '0' && argv[i][pos] <= '9') {76level = level * 10 + argv[i][pos] - '0';77pos++;78}79}80break;81}82}83// TODO: make setting?84flags |= ZIM_ETC1_MEDIUM;85if (!format_set) {86fprintf(stderr, "Must set format\n");87printusage();88return 1;89}9091uint8_t *image_data;92int width, height;93if (1 != pngLoad(FLAGS_infile, &width, &height, &image_data)) {94fprintf(stderr, "Input not a PNG file\n");95printusage();96return 1;97}9899FILE *f = fopen(FLAGS_outfile, "wb");100SaveZIM(f, width, height, width * 4, flags, image_data, level);101fclose(f);102int in_file_size = filesize(FLAGS_infile);103int out_file_size = filesize(FLAGS_outfile);104fprintf(stdout, "Converted %s to %s. %i b to %i b. %ix%i, %s.\n", FLAGS_infile, FLAGS_outfile, in_file_size, out_file_size, width, height, format_strings[flags & ZIM_FORMAT_MASK]);105return 0;106}107108109