Nuppiz

Texconv.c

Nov 4th, 2021 (edited)
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5.  
  6. char inputname[13];
  7. char outputname[13];
  8. char inname[13];
  9. char outname[13];
  10. uint8_t* far buffer;
  11. uint16_t filesize = 0;
  12. int width;
  13. int height;
  14. int checksum1;
  15. int checksum2;
  16. int running = 1;
  17.  
  18. void load_file(char* inname)
  19. {
  20.     FILE* file_ptr;
  21.     char c;
  22.     uint16_t i=0;
  23.     int j=0;
  24.     int lines=1;
  25.  
  26.     printf("Loading file %s...\n", inname);
  27.  
  28.     file_ptr = fopen(inname, "rb");
  29.    
  30.     //check that the file actually exists
  31.     if (file_ptr == NULL)
  32.     {
  33.         printf("Unable to open file: %s\n", inname);
  34.         printf("Please check the file actually exists\n");
  35.         exit(EXIT_FAILURE);
  36.         // exit will terminate the program here, nothing further will happen
  37.     }
  38.    
  39.     fseek(file_ptr, 0, SEEK_SET);
  40.     fread(&checksum1, 2, 1, file_ptr); //check for BMP file header
  41.     fseek(file_ptr, 28, SEEK_SET);
  42.     fread(&checksum2, 1, 1, file_ptr); // check bit depth
  43.    
  44.     //if file header or bit depth is wrong, then exit
  45.     if (checksum1 != 19778 || checksum2 != 8)
  46.     {
  47.         printf("Fatal error: %s is not a valid 8-bit bitmap!\n", inname);
  48.         printf("This program will now exit.\n");
  49.         exit(EXIT_FAILURE);
  50.         // exit will terminate the program here, nothing further will happen
  51.     }
  52.    
  53.     fseek(file_ptr, 18, SEEK_SET);
  54.     fread(&width, 2, 1, file_ptr);
  55.     printf("Checking file width... %dpx\n", width);
  56.     fseek(file_ptr, 22, SEEK_SET);
  57.     fread(&height, 2, 1, file_ptr);
  58.     printf("Checking file height... %dpx\n", height);
  59.     printf("Checking file dimensions complete!\n");
  60.    
  61.     filesize = width * height;
  62.     printf("Total file size: %u bytes\n", filesize);
  63.     printf("\n");
  64.    
  65.     printf("Allocating memory...");
  66.     buffer = (uint8_t far*)malloc(filesize);
  67.     printf("\n");
  68.    
  69.     printf("Starting to read first line...\n");
  70.     fseek(file_ptr, -width, SEEK_END);
  71.     c = fgetc(file_ptr);
  72.    
  73.     while (i < filesize)
  74.     {
  75.         buffer[j] = c;
  76.         i++;
  77.         if (i % width == 0)
  78.         {
  79.             lines++;
  80.             fseek(file_ptr, (-width*lines), SEEK_END);
  81.         }
  82.         c = fgetc(file_ptr);
  83.         j++;
  84.     }
  85.    
  86.     printf("\n%u bytes read!\n", i);
  87.     printf("\nFile read successfully!\n");
  88.     fclose(file_ptr);
  89.     printf("\n");
  90. }
  91.  
  92. void save_file(char* outname, uint8_t* source_data, uint16_t data_size)
  93. {
  94.     FILE* file_ptr;
  95.     file_ptr = fopen(outname, "wb+");
  96.     fseek(file_ptr, 0, SEEK_SET);
  97.     fwrite(source_data, 1, data_size, file_ptr);
  98.     fclose(file_ptr);
  99. }
  100.  
  101. void menu()
  102. {
  103.     char response;
  104.  
  105.     // take in the file input name
  106.     printf("Enter file to convert (without file extension):\n");
  107.     scanf("%s", inputname);
  108.     sprintf(inname, "%s.bmp", inputname);
  109.    
  110.     // load file into buffer
  111.     load_file(inname);
  112.    
  113.     // take in the file output name
  114.     printf("Enter output name (without file extension)\n");
  115.     printf("or write 'same' to save with the BMP name:\n");
  116.     scanf("%s", outputname);
  117.    
  118.     if (strcmp ("same", outputname) == 0)
  119.         sprintf(outname, "%s.7UP", inputname);
  120.    
  121.     else
  122.         sprintf(outname, "%s.7UP", outputname);
  123.    
  124.     printf("Writing file: %s\n", outname);
  125.  
  126.     // save file to 7UP format
  127.     save_file(outname, buffer, filesize);
  128.  
  129.     printf("BMP successfully converted into %s!\n", outname);
  130.  
  131.     //clear memory
  132.     inputname [0] = '\0';
  133.     outputname [0] = '\0';
  134.     inname [0] = '\0';
  135.     outname [0] = '\0';
  136.     farfree(buffer);
  137.    
  138.     // ask if user wants to convert another file, or quit
  139.     printf("Y to convert another file, or N to quit.\n");
  140.     scanf (" %c", &response);
  141.     if (response == 'y' || response == 'Y')
  142.         running = 1;
  143.  
  144.     else if (response == 'n' || response == 'N')
  145.         running = 0;
  146. }
  147.  
  148. int main()
  149. {
  150.     while (running == 1)
  151.         menu();
  152.     return 0;
  153. }
Add Comment
Please, Sign In to add comment