Advertisement
Aslai

Untitled

Dec 9th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #define PI 3.141592654
  6.  
  7. //Modify these values to change the output
  8. #define C_R 600
  9. #define C_S 200
  10. #define C_M 1000
  11.  
  12. typedef struct{ int r,g,b; } color;
  13. typedef struct{ int t; color* data; int w, h; } bitmap;
  14. typedef struct{ int t; color* data; int r, s, w, h; } r_bitmap;
  15.  
  16. bitmap* bmp_load_from_file( const char* fname ){
  17.     FILE* b = fopen( fname, "rb" );
  18.     if( b <= 0 ) return 0;
  19.     int num;
  20.     fscanf( b, "BM%n", &num );
  21.     if( num < 2 ) return 0;
  22.     struct{ int size, reserved, offset;
  23.         int hsize, wid, hig, planes:16, bpp:16,
  24.          comp, bmpsize, hres, vres, colors, important; } head;
  25.     fread( &head, 13, 4, b );
  26.     bitmap* bmp = malloc( sizeof( bitmap ) );
  27.     bmp->data = malloc( head.wid * head.hig * sizeof( color ) );
  28.     bmp->w = head.wid;
  29.     bmp->h = head.hig;
  30.     for( int y = head.hig - 1; y >= 0; --y ){
  31.         int x;
  32.         for( x = 0; x < head.wid; ++x ){
  33.             color t;
  34.             t.r = fgetc( b );
  35.             t.g = fgetc( b );
  36.             t.b = fgetc( b );
  37.             bmp->data[x+y*bmp->w] = t;
  38.         }
  39.         x*=3;
  40.         while( x%4 != 0 ){
  41.             ++x;
  42.             fgetc( b );
  43.         }
  44.     }
  45.     bmp->t = 0;
  46.     fclose( b );
  47.     return bmp;
  48. }
  49.  
  50. void bmp_save( const char* fname, bitmap* bmp ){
  51.     FILE* b = fopen( fname, "wb" );
  52.     if( b <= 0 ) return 0;
  53.     struct{ int size, reserved, offset;
  54.         int hsize, wid, hig, planes:16, bpp:16, comp,
  55.          bmpsize, hres, vres, colors, important; } head;
  56.     fprintf( b, "BM" );
  57.     head.size = 3 * (bmp->w+4)/4*4 * bmp->h + 54;
  58.     head.offset = 54;
  59.     head.hsize = 40;
  60.     head.wid = bmp->w;
  61.     head.hig = bmp->h;
  62.     head.planes = 1;
  63.     head.bpp = 24;
  64.     head.comp = 0;
  65.     head.bmpsize = 3 * (bmp->w+4)/4*4 * bmp->h;
  66.     head.hres = 72;
  67.     head.vres = 72;
  68.     head.colors = 0;
  69.     head.important = 0;
  70.     fwrite( &head, 13, 4, b );
  71.     for( int y = bmp->h - 1; y >= 0; --y ){
  72.         int x;
  73.         for( x = 0; x < bmp->w; ++x ){
  74.             fputc( bmp->data[x + y * bmp->w].r, b );
  75.             fputc( bmp->data[x + y * bmp->w].g, b );
  76.             fputc( bmp->data[x + y * bmp->w].b, b );
  77.         }
  78.         x*=3;
  79.         while( x % 4 != 0 ){
  80.             ++x;
  81.             fputc(0, b);
  82.         }
  83.     }
  84.     fclose( b );
  85. }
  86.  
  87. color color_mix( color a, color b, int offset ){
  88.     a.r += ( b.r - a.r ) * offset / 255;
  89.     a.g += ( b.g - a.g ) * offset / 255;
  90.     a.b += ( b.b - a.b ) * offset / 255;
  91.     return a;
  92. }
  93.  
  94. r_bitmap* bmp_to_r( bitmap* b ){
  95.     r_bitmap* r = malloc( sizeof( r_bitmap ) );
  96.     r->t = 1;
  97.     int radius = sqrt( b->w * b->w + b->h * b->h ) / 2 * C_R / C_M + 2;
  98.     int step = C_S * ( b->w + b->h ) / C_M;
  99.     r->data = malloc( radius * step * sizeof( color ) );
  100.     r->r = radius;
  101.     r->s = step;
  102.     r->w = b->w;
  103.     r->h = b->h;
  104.     color black = {0, 0, 0};
  105.     for( double i = 0; i < radius; ++ i ){
  106.         for( double j = 0; j < step; ++j ){
  107.             double x = i * C_M * cos( 2 * PI * j / step ) / C_R + b->w / 2;
  108.             double y = i * C_M * sin( 2 * PI * j / step ) / C_R + b->h / 2;
  109.             int ix = x;
  110.             int iy = y;
  111.             if( x < 0 || x >= b->w || y < 0 || y >= b->h )
  112.                 r->data[(int)(j + i * step)] = black;
  113.             else{
  114.                 color tmp = b->data[ix + iy * b->w];
  115.                 if( iy < b->h - 1 ){
  116.                     int off = 255 * (y - iy);
  117.                     tmp = color_mix( tmp, b->data[ix + (iy+1) * b->w], off );
  118.                 }
  119.                 if( ix < b->w - 1 ){
  120.                     int off = 255 * ( x - ix );
  121.                     tmp = color_mix( tmp, b->data[ix +1 + iy * b->w], off );
  122.                 }
  123.                 r->data[(int)(j + i * step)] = tmp;
  124.             }
  125.         }
  126.     }
  127.     return r;
  128. }
  129.  
  130. bitmap* bmp_from_r( r_bitmap* r ){
  131.     bitmap* b = malloc( sizeof( bitmap ) );
  132.     b->t = 0;
  133.     b->data = malloc( r->w * r->h * sizeof( color ) );
  134.     b->w = r->w;
  135.     b->h = r->h;
  136.     for( int y = 0; y < b->h; ++y ){
  137.         for( int x = 0; x < b->w; ++x ){
  138.             int tx = x - b->w/2;
  139.             int ty = y - b->h/2;
  140.  
  141.             double rad = sqrt( tx*tx+ty*ty ) * C_R / C_M;
  142.             double s = atan2( ty, tx );
  143.             if( s < 0 ) s += 2 * PI;
  144.             s *= r->s / ( 2 * PI );
  145.  
  146.             int is = s;
  147.             int irad = rad;
  148.  
  149.             color tmp = r->data[(int)(is + irad * r->s)];
  150.             b->data[x+y*b->w] = tmp;
  151.         }
  152.     }
  153.     return b;
  154. }
  155.  
  156.  
  157.  
  158. int main( ) {
  159.     bitmap* b = bmp_load_from_file( "foo.bmp" );
  160.     r_bitmap* r = bmp_to_r( b );
  161.     bitmap* c = bmp_from_r( r );
  162.     bmp_save( "lol.bmp", c );
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement