Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. struct _sBmpHeader
  7. {
  8. char bm[2];
  9. uint32_t size;
  10. uint32_t reserve;
  11. uint32_t offset;
  12. uint32_t header_size;
  13. uint32_t width;
  14. uint32_t height;
  15. uint16_t planes;
  16. uint16_t bpp;
  17. uint32_t compression;
  18. uint32_t bitmap_size;
  19. uint32_t hres;
  20. uint32_t vres;
  21. uint32_t used;
  22. uint32_t important;
  23. }__attribute__ ((__packed__));
  24.  
  25. typedef struct _sBmpHeader sBmpHeader;
  26.  
  27. int main()
  28. {
  29. char fn[258];
  30. char c;
  31. int32_t rate;
  32. int32_t originHeight, originWidth;
  33. FILE *SourceFile = NULL;
  34. FILE *ResultFile = NULL;
  35.  
  36. puts("your file path?");
  37. fgets(fn, 256, stdin);
  38. while( strlen(fn)>256 )
  39. {
  40. puts("file path too long, plz retry");
  41. strcpy(fn, "");
  42. fgets(fn, 256, stdin);
  43. }
  44.  
  45. if( fn[ strlen(fn) ] > 256)
  46. fn[256] = '\0' ;
  47. else
  48. fn[ strlen(fn)-1 ] = '\0' ;
  49.  
  50.  
  51. if( ( SourceFile = fopen( fn, "rb" ) ) == NULL )
  52. {
  53. printf( "sorry, file [%s] could not be opened!\n" ,fn);
  54. return 0;
  55. }
  56.  
  57. rate = 1;
  58. puts("your downscale rate? (int )[2~10]");
  59. while( scanf("%d" ,&rate)!= 1 || rate > 10 || rate < 2)
  60. {
  61. puts("downscale rate error, plz retry");
  62. while( c = getchar()!='\n' )
  63. ;
  64. }
  65.  
  66. SourceFile = fopen( "downscaled.bmp", "wb" );
  67.  
  68. sBmpHeader header;
  69. fread( &header, sizeof( header ), 1, SourceFile );
  70. originHeight = header.height;
  71. originWidth = header.width;
  72. header.height = originHeight / rate ;
  73. header.width = originWidth / rate ;
  74. fwrite( &header, sizeof( header ), 1, ResultFile );
  75.  
  76. while( !feof( SourceFile ) )
  77. {
  78. uint8_t original[ originHeight ][ originWidth ] = {0};
  79. uint8_t modified[ originHeight/rate ][ originWidth/rate ] = {0};
  80.  
  81. for(size_t i = 0 ; i < rate ; i++)
  82. {
  83. size_t count = fread( original, 1, originWidth, SourceFile );
  84. for( size_t j = k = 0 ; j < originWidth ; j++)
  85. {
  86. modified[i][k] = modified[i][k] + original[i][j] ;
  87. if( (j+1) % rate == 0)
  88. k++;
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. for( size_t j = 0 ; j < originWidth/rate ; j++)
  99. modified[j] = modified[j]/ rate ;
  100.  
  101. //fwrite( modified, count, 1, ResultFile );
  102.  
  103. }
  104.  
  105.  
  106. fclose( SourceFile );
  107. puts("done, your file [downscaled.bmp] is ready");
  108.  
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement