apps/ccam/dma2bmp.c

Go to the documentation of this file.
00001 /*
00002  * dma2bmp.c
00003  * monochrome bmp file format (first - no compression, later - add RLE)
00004  *
00005  */
00006 #include <fcntl.h>  /*open*/
00007 #include <unistd.h> /* close */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <sys/mman.h>           /* mmap */
00013 
00014 #include "imageaccess.h"
00015 #include "dma2bmp.h"
00016 
00017 
00018 #define D(x)
00019 #define D1(x)
00020 
00021 
00022 int dma2bmp (int ImageWidth, int ImageHeight,
00023                          int Mode,      // 0 - 8 bit(b/w), 1 - 24 bits (color)  -- will add later - now only b/w
00024                          int Contrast, int Color, int bayerOrient, int Depth, const char * pseudo,
00025                          const char * ifn, const char * ofn) {
00026 
00027  int Compression=0;
00028  char * byteLine;
00029 // int i; //,j; // j - debug
00030  int i; //,j;//, od, bi,bd,dd; // j - debug
00031  int row;
00032  int paddedWidth;       // padded to multiple of four width
00033  unsigned long * plt;
00034 
00035   struct {
00036   char           Signature[2];
00037   unsigned long  FileSize;
00038   unsigned long  reserved1;
00039   unsigned long  DataOffset;
00040   unsigned long  Size;
00041   unsigned long  Width;
00042   unsigned long  Height;
00043   unsigned short Planes;
00044   unsigned short BitCount;
00045   unsigned long  Compression;
00046   unsigned long  ImageSize;
00047   unsigned long  XpixelsPerM;
00048   unsigned long  YpixelsPerM;
00049   unsigned long  ColorsUsed;
00050   unsigned long  ColorsImportant;
00051   unsigned long  ColorTable[256];
00052 
00053 } bmfh;
00054 
00055 
00056 
00057 // struct BITMAPFILEHEADER bmfh;
00058   /* More stuff */
00059 
00060   FILE * outfile;               /* target file */
00061 
00062   struct pixel_buffers pb;
00063 
00064  // Contrast (0-5) - shift data left by Contrast bits (before shifting right by 2)
00065     if ((Contrast <0) || (Contrast >5 )) Contrast=0;
00066     
00067 
00068   /* Step 1: open dma data memory through 1-st filename */
00069 
00070     if (initPixelBuffers(&pb, ifn,ImageWidth,ImageHeight,Contrast, Color, bayerOrient, Depth, pseudo) <0) return -1;
00071 
00072         D1(fprintf (stderr,"width:       %d\r\n",ImageWidth));
00073         D1(fprintf (stderr,"height:      %d\r\n",ImageHeight));
00074         D1(fprintf (stderr,"input file:  %s\r\n",ifn));
00075         D1 (fprintf (stderr,"output file: %s\r\n",ofn));
00076 
00077 
00078   /* Step 3: open output file */
00079 
00080 
00081         if (ofn) {
00082           if ((outfile = fopen(ofn, "wb")) == NULL) {
00083             fprintf(stderr, "can't open %s\n", ofn);
00084             closePixelBuffers(&pb);
00085             exit(1);
00086           }
00087         } else {
00088           outfile=stdout;
00089           D1(fprintf(stderr, "Using stdout\n"));
00090 
00091     }
00092 
00093   /* Step 4: fill header info */
00094 
00095   paddedWidth = ImageWidth & 0x7ffffffc; // zero 2 LSBs;
00096   if (paddedWidth < ImageWidth) paddedWidth += 4;
00097   bmfh.Signature[0]=   'B';
00098   bmfh.Signature[1]=   'M';
00099   bmfh.FileSize=       paddedWidth*ImageHeight+sizeof(bmfh);    // will adjust later if Compression
00100   bmfh.reserved1=      0;
00101   bmfh.DataOffset=     sizeof(bmfh); // 1060
00102   bmfh.Size=           40;
00103   bmfh.Width=          ImageWidth;
00104   bmfh.Height=         ImageHeight;
00105   bmfh.Planes=         1;
00106   bmfh.BitCount=       8;
00107   bmfh.Compression=    Compression;
00108   bmfh.ImageSize=      paddedWidth*ImageHeight; // will adjust later if Compression
00109   bmfh.XpixelsPerM=    11811; //300dpi
00110   bmfh.YpixelsPerM=    11811; //300dpi
00111   bmfh.ColorsUsed=     256;
00112   bmfh.ColorsImportant=0;
00113 // use calculated pseudocolor pallet if available, if not - build greyscale
00114   if ((plt= getPalette (&pb))) memcpy (bmfh.ColorTable,plt,sizeof(bmfh.ColorTable));
00115   else for (i=0;i<256;i++) bmfh.ColorTable[i]= (unsigned long) i*0x10101;
00116  
00117   /* Step 5: write header to file */
00118   fwrite (&bmfh,sizeof(bmfh),1,outfile);
00119    
00120   
00121   /* Step 6: write raster data line by line */
00122 //  myRowAligned[(paddedWidth>>2)-1]=0;
00123   for (row=bmfh.Height-1;row>=0;row--) {
00124     byteLine=getMonoRow (&pb, row, 0);
00125     fwrite (byteLine,paddedWidth,1,outfile);
00126   } // for (row=0;row<bmfh.Height;row++)
00127   D1(fprintf (stderr,"bmfh.FileSize=%ld\r\n",bmfh.FileSize));
00128   D1(fprintf (stderr,"bmfh.ImageSize=%ld\r\n",bmfh.ImageSize));
00129 
00130   fclose(outfile);
00131   closePixelBuffers(&pb);
00132 
00133   /* And we're done! */
00134   return 0;
00135 }
00136 

Generated on Thu Aug 7 16:18:59 2008 for elphel by  doxygen 1.5.1