├── LICENSE ├── README.md ├── build.sh └── jpeg2array.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Rohit Gujarathi, 2020 Arthur Golubtsov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jpeg2array 2 | 3 | Convert jpeg image to C/C++ array for use with microcontrollers. 4 | 5 | This tool creates a header file which needs to be included in your program. 6 | 7 | This can be used with the tft library by bodmer 8 | https://github.com/Bodmer/TFT_eSPI 9 | 10 | ## Usage 11 | 12 | ```cmd 13 | ./jpeg2array jpeg_filename [options] 14 | Options: 15 | --stm32 Generate array for stm32 (default) 16 | --arduino Generate array for arduino 17 | ``` 18 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | gcc jpeg2array.c -o jpeg2array -Wall -------------------------------------------------------------------------------- /jpeg2array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define ANSI_COLOR_RED "\x1b[31;1m" 8 | #define ANSI_COLOR_GREEN "\x1b[32;1m" 9 | #define ANSI_COLOR_BLUE "\x1b[34;1m" 10 | #define ANSI_COLOR_RESET "\x1b[0m" 11 | 12 | void logError(char* error) { 13 | fprintf(stderr, ANSI_COLOR_RED "error: " ANSI_COLOR_RESET "%s", 14 | error); 15 | } 16 | 17 | void logInfo(char* info) { 18 | fprintf(stdout, ANSI_COLOR_BLUE "log: " ANSI_COLOR_RESET "%s", 19 | info); 20 | } 21 | 22 | void strUpper(char * temp) { 23 | while (*temp) { 24 | *temp = toupper((unsigned char) *temp); 25 | temp++; 26 | } 27 | } 28 | 29 | void printUsage() { 30 | printf("Convert jpeg image to C/C++ array for use with microcontrollers\n"); 31 | printf("Usage: ./jpeg2array jpeg_filename [options]\n"); 32 | printf("Options:\n--stm32\t\tGenerate array for stm32 (default)\n--arduino\tGenerate array for arduino\n"); 33 | } 34 | 35 | int main(int argc, char *argv[]){ 36 | FILE *jpegFile; 37 | FILE *arrayFile; 38 | unsigned char data; 39 | char line_len = 0; 40 | int mode = 1; // 0 - arduino, 1 - stm32 41 | 42 | if ((argc < 2) || (argc > 3)) { 43 | if (argc > 3) 44 | logError("Wrong number of arguments!\n"); 45 | printUsage(); 46 | return 1; 47 | } 48 | 49 | if (argc == 3) { 50 | if (strcmp(argv[2],"--stm32") == 0) 51 | mode = 1; 52 | else if (strcmp(argv[2],"--arduino") == 0) 53 | mode = 0; 54 | else { 55 | logError("Second argument is invalid!\n"); 56 | printUsage(); 57 | return 1; 58 | } 59 | } 60 | 61 | if (access(argv[1], R_OK) != -1) { 62 | jpegFile = fopen(argv[1], "r"); 63 | logInfo("Opened "); 64 | fprintf(stdout, "%s\n",argv[1]); 65 | 66 | //Extract name of jpeg file 67 | char *ptr, *filename, *filename_header, *arrayname, c; 68 | int i; 69 | 70 | ptr = strrchr(argv[1], '/'); // Find last slash in filename 71 | if(ptr) ptr++; // First character of filename (path stripped) 72 | else ptr = argv[1]; // No path; font in local dir. 73 | 74 | filename = malloc(strlen(ptr)+2); 75 | strcpy(filename, ptr); 76 | ptr = strrchr(filename, '.'); // Find last period (file ext) 77 | 78 | // Space and punctuation chars in name replaced w/ underscores. 79 | for(i=0; (c=filename[i]); i++) { 80 | if(isspace(c) || ispunct(c)) filename[i] = '_'; 81 | } 82 | 83 | // Make array name 84 | arrayname = malloc(strlen(filename)); 85 | strcpy(arrayname, filename); 86 | 87 | // Make file name 88 | strcat(filename,".h"); 89 | 90 | // Make header name 91 | filename_header = malloc(strlen(filename)+2); 92 | strcpy(filename_header, "__"); 93 | strcat(filename_header, filename); 94 | filename_header[strlen(filename_header)-2] = '_'; 95 | strUpper(filename_header); 96 | 97 | // Print log info 98 | logInfo("Creating header file "); 99 | printf("%s for ", filename); 100 | const char * target = mode == 0 ? "arduino" : "stm32"; 101 | printf("%s\n", target); 102 | arrayFile = fopen(filename,"w"); 103 | 104 | fputs("/*************************************************\n", arrayFile); 105 | fputs(" * File made by jpeg2array *\n", arrayFile); 106 | fputs(" * Authors: Rohit Gujarathi and Arthur Golubtsov *\n", arrayFile); 107 | fputs(" *************************************************/\n\n", arrayFile); 108 | 109 | if (mode == 0) { 110 | fputs("#include \n\n", arrayFile); 111 | fputs("const uint8_t ", arrayFile); fputs(arrayname, arrayFile); fputs("[] PROGMEM = {\n", arrayFile); 112 | } else { 113 | fputs("#ifndef ", arrayFile); fputs(filename_header, arrayFile); fputs("\n", arrayFile); 114 | fputs("#define ", arrayFile); fputs(filename_header, arrayFile); fputs("\n\n", arrayFile); 115 | fputs("const char ", arrayFile); fputs(arrayname, arrayFile); fputs("[] = {\n", arrayFile); 116 | } 117 | 118 | fseek(jpegFile,0,SEEK_SET); 119 | while(1){ 120 | if( feof(jpegFile) ) { 121 | break ; 122 | } 123 | data = getc(jpegFile); 124 | if(line_len >= 32){ 125 | line_len = 0; 126 | fprintf(arrayFile, "\n"); 127 | fprintf(stdout,"."); 128 | } 129 | fprintf(arrayFile,"0x%02X,",data); 130 | line_len++; 131 | } 132 | fputs("\n}",arrayFile); 133 | if (mode == 1) { 134 | fputs(";\n\n#endif", arrayFile); 135 | } 136 | fputs("\n", arrayFile); 137 | 138 | fprintf(stdout,"\n"); 139 | fclose(arrayFile); 140 | fclose(jpegFile); 141 | } else { 142 | logError("File does not exist or read permission not granted.\n"); 143 | } 144 | return 0; 145 | } --------------------------------------------------------------------------------