Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- struct _sBmpHeader
- {
- char bm[2];
- uint32_t size;
- uint32_t reserve;
- uint32_t offset;
- uint32_t header_size;
- uint32_t width;
- uint32_t height;
- uint16_t planes;
- uint16_t bpp;
- uint32_t compression;
- uint32_t bitmap_size;
- uint32_t hres;
- uint32_t vres;
- uint32_t used;
- uint32_t important;
- }__attribute__ ((__packed__));
- typedef struct _sBmpHeader sBmpHeader;
- int main()
- {
- char fn[258];
- char c;
- int32_t rate;
- int32_t originHeight, originWidth;
- FILE *SourceFile = NULL;
- FILE *ResultFile = NULL;
- puts("your file path?");
- fgets(fn, 256, stdin);
- while( strlen(fn)>256 )
- {
- puts("file path too long, plz retry");
- strcpy(fn, "");
- fgets(fn, 256, stdin);
- }
- if( fn[ strlen(fn) ] > 256)
- fn[256] = '\0' ;
- else
- fn[ strlen(fn)-1 ] = '\0' ;
- if( ( SourceFile = fopen( fn, "rb" ) ) == NULL )
- {
- printf( "sorry, file [%s] could not be opened!\n" ,fn);
- return 0;
- }
- rate = 1;
- puts("your downscale rate? (int )[2~10]");
- while( scanf("%d" ,&rate)!= 1 || rate > 10 || rate < 2)
- {
- puts("downscale rate error, plz retry");
- while( c = getchar()!='\n' )
- ;
- }
- SourceFile = fopen( "downscaled.bmp", "wb" );
- sBmpHeader header;
- fread( &header, sizeof( header ), 1, SourceFile );
- originHeight = header.height;
- originWidth = header.width;
- header.height = originHeight / rate ;
- header.width = originWidth / rate ;
- fwrite( &header, sizeof( header ), 1, ResultFile );
- while( !feof( SourceFile ) )
- {
- uint8_t original[ originHeight ][ originWidth ] = {0};
- uint8_t modified[ originHeight/rate ][ originWidth/rate ] = {0};
- for(size_t i = 0 ; i < rate ; i++)
- {
- size_t count = fread( original, 1, originWidth, SourceFile );
- for( size_t j = k = 0 ; j < originWidth ; j++)
- {
- modified[i][k] = modified[i][k] + original[i][j] ;
- if( (j+1) % rate == 0)
- k++;
- }
- }
- for( size_t j = 0 ; j < originWidth/rate ; j++)
- modified[j] = modified[j]/ rate ;
- //fwrite( modified, count, 1, ResultFile );
- }
- fclose( SourceFile );
- puts("done, your file [downscaled.bmp] is ready");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement