Guest User

Eddy_Em

a guest
Jan 11th, 2013
2,776
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <sndfile.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. #define RATE 44100
  8.  
  9. typedef struct {
  10.     unsigned short type;                 /* Magic identifier            */
  11.     unsigned int size;                       /* File size in bytes          */
  12.     unsigned int reserved;
  13.     unsigned int offset;                     /* Offset to image data, bytes */
  14. } HEADER;
  15. typedef struct {
  16.     unsigned int size;               /* Header size in bytes      */
  17.     int width,height;                /* Width and height of image */
  18.     unsigned short planes;       /* Number of colour planes   */
  19.     unsigned short bits;         /* Bits per pixel            */
  20.     unsigned int compression;        /* Compression type          */
  21.     unsigned int imagesize;          /* Image size in bytes       */
  22.     int xresolution,yresolution;     /* Pixels per meter          */
  23.     unsigned int ncolours;           /* Number of colours         */
  24.     unsigned int importantcolours;   /* Important colours         */
  25. } INFOHEADER;
  26. typedef struct {
  27.     unsigned char r,g,b,junk;
  28. } COLOURINDEX;
  29.  
  30.  
  31. int main(int argc, char *argv[]){
  32.     int i,j,rd;
  33.     int gotindex = 0;
  34.     unsigned char grey,r,g,b;
  35.     double ampl;
  36.     short _2byte[2];
  37.     HEADER header;
  38.     INFOHEADER infoheader;
  39.     COLOURINDEX colourindex[256];
  40.     FILE *fptr;
  41.     SNDFILE* sndfile = NULL;
  42.     SF_INFO sfinfo;
  43.     long rate = RATE;
  44.    
  45.     void (*bmpread)();
  46.     void _eightbit(){
  47.         if(fread(&grey, sizeof(unsigned char), 1, fptr) != 1){
  48.         fprintf(stderr,"Image read failed\n");
  49.         exit(-1);
  50.         }
  51.         if (gotindex){
  52.             ampl =  colourindex[grey].r * 64. +
  53.                 colourindex[grey].g * 128.+
  54.                 colourindex[grey].b * 64.;
  55.         } else {
  56.             ampl = grey * 256. - 32768.;
  57.         }
  58. //      printf("%.2f\n", ampl);
  59.     }
  60.     void _twentyfourbit(){
  61.         do{
  62.             if((rd = fread(&b, sizeof(unsigned char), 1, fptr)) != 1) break;
  63.             if((rd = fread(&g, sizeof(unsigned char), 1, fptr)) != 1) break;
  64.             if((rd = fread(&r, sizeof(unsigned char), 1, fptr)) != 1) break;
  65.         }while(0);
  66.         if(rd != 1){   
  67.             fprintf(stderr,"Image read failed\n");
  68.             exit(-1);
  69.         }
  70.         ampl = r * 64. + g * 128. + b * 64. - 32768.;
  71. //      printf("%.2f\n", ampl);
  72.     }
  73.     if (argc < 3){
  74.         printf("Usage: %s <input.bmp> <output.wav> [samplerate]\n", argv[0]);
  75.         printf("For example:\n\t%s pict.bmp sample.wav 44100 2\n", argv[0]);
  76.         exit(0);
  77.     }
  78.     printf("Input file: %s\n", argv[1]);
  79.     printf("Output file: %s\n", argv[2]);
  80.     if(argc > 3) rate = atoi(argv[3]);
  81.     if(rate < 4000) rate = 4000;
  82.     //if(argc > 4) channels = atoi(argv[4]);       
  83.     sfinfo.samplerate = rate;
  84.     sfinfo.channels = 2;
  85.     sfinfo.format = SF_FORMAT_WAV|SF_FORMAT_PCM_16;
  86.     if((fptr = fopen(argv[1],"r")) == NULL) {
  87.         fprintf(stderr,"Unable to open BMP file \"%s\"\n",argv[1]);
  88.         exit(-1);
  89.     }
  90.         /* Read and check BMP header */
  91.     if(fread(&header.type, 2, 1, fptr) != 1){
  92.         fprintf(stderr, "Failed to read BMP header\n");
  93.         exit(-1);
  94.     }
  95.     if(header.type != 'M'*256+'B'){
  96.         fprintf(stderr, "File is not bmp type\n");
  97.         exit(-1);
  98.     }
  99.     do{
  100.         if((rd = fread(&header.size, 4, 1, fptr)) != 1) break;
  101.         printf("File size: %d bytes\n", header.size);
  102.         if((rd = fread(&header.reserved, 4, 1, fptr)) != 1) break;
  103.         if((rd = fread(&header.offset, 4, 1, fptr)) != 1) break;
  104.         printf("Offset to image data is %d bytes\n", header.offset);
  105.     }while(0);
  106.     if(rd =! 1){
  107.         fprintf(stderr, "Error reading file\n");
  108.         exit(-1);
  109.     }
  110.     /* Read and check the information header */
  111.     if (fread(&infoheader, sizeof(INFOHEADER), 1, fptr) != 1) {
  112.         fprintf(stderr,"Failed to read BMP info header\n");
  113.         exit(-1);
  114.     }
  115.     printf("Image size = %d x %d\n", infoheader.width, infoheader.height);
  116.     printf("Number of colour planes is %d\n", infoheader.planes);
  117.     printf("Bits per pixel is %d\n", infoheader.bits);
  118.     printf("Compression type is %d\n", infoheader.compression);
  119.     printf("Number of colours is %d\n", infoheader.ncolours);
  120.     printf("Number of required colours is %d\n", infoheader.importantcolours);
  121.     /* Read the lookup table if there is one */
  122.     for (i=0; i<255; i++){
  123.         colourindex[i].r = rand() % 256;
  124.         colourindex[i].g = rand() % 256;
  125.         colourindex[i].b = rand() % 256;
  126.         colourindex[i].junk = rand() % 256;
  127.     }
  128.     if (infoheader.ncolours > 0) {
  129.         for (i=0; i<infoheader.ncolours; i++){
  130.             do{
  131.             if ((rd = fread(&colourindex[i].b, sizeof(unsigned char),1,fptr)) != 1)
  132.                 break;
  133.             if ((rd = fread(&colourindex[i].g, sizeof(unsigned char),1,fptr)) != 1)
  134.                 break;
  135.             if ((rd = fread(&colourindex[i].r, sizeof(unsigned char),1,fptr)) != 1)
  136.                 break;
  137.             if ((rd = fread(&colourindex[i].junk, sizeof(unsigned char),1,fptr)) != 1)
  138.                 break;
  139.             }while(0);
  140.             if(rd != 1){
  141.                 fprintf(stderr,"Image read failed\n");
  142.                 exit(-1);
  143.             }          
  144.             printf("%3d\t%3d\t%3d\t%3d\n", i,
  145.             colourindex[i].r, colourindex[i].g, colourindex[i].b);
  146.         }
  147.         gotindex = 1;
  148.     }
  149.     if(infoheader.bits < 8){
  150.         printf("Too small image map depth (%d < 8)\n", infoheader.bits);
  151.         exit(-1);
  152.     }
  153.     /* Seek to the start of the image data */
  154.     fseek(fptr, header.offset, SEEK_SET);
  155.     printf("Creating 16bit WAV %liHz.\n", rate);
  156.     sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo);
  157.     if(sndfile == NULL){
  158.         fprintf(stderr, "Cannot open output file!\n"); exit(-1);
  159.     }
  160.     bmpread = _eightbit;
  161.     if(infoheader.bits == 24)
  162.         bmpread = _twentyfourbit;
  163.    
  164.     /* Read the image */
  165.     for (j=0;j<infoheader.height;j++) {
  166.         _2byte[1] = 32700;
  167.         for (i=0;i<infoheader.width;i++) {
  168.             bmpread();
  169.             _2byte[0] = (short)ampl;
  170.             sf_write_short(sndfile, _2byte, 2);
  171.             _2byte[1] = 0;
  172.         } // i
  173.     } // j
  174.     fclose(fptr);
  175.     sf_close(sndfile);
  176. }
RAW Paste Data