00001
00002
00003
00004
00005
00006 #include <fcntl.h>
00007 #include <unistd.h>
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <sys/mman.h>
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,
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
00030 int i;
00031 int row;
00032 int paddedWidth;
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
00058
00059
00060 FILE * outfile;
00061
00062 struct pixel_buffers pb;
00063
00064
00065 if ((Contrast <0) || (Contrast >5 )) Contrast=0;
00066
00067
00068
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
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
00094
00095 paddedWidth = ImageWidth & 0x7ffffffc;
00096 if (paddedWidth < ImageWidth) paddedWidth += 4;
00097 bmfh.Signature[0]= 'B';
00098 bmfh.Signature[1]= 'M';
00099 bmfh.FileSize= paddedWidth*ImageHeight+sizeof(bmfh);
00100 bmfh.reserved1= 0;
00101 bmfh.DataOffset= sizeof(bmfh);
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;
00109 bmfh.XpixelsPerM= 11811;
00110 bmfh.YpixelsPerM= 11811;
00111 bmfh.ColorsUsed= 256;
00112 bmfh.ColorsImportant=0;
00113
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
00118 fwrite (&bmfh,sizeof(bmfh),1,outfile);
00119
00120
00121
00122
00123 for (row=bmfh.Height-1;row>=0;row--) {
00124 byteLine=getMonoRow (&pb, row, 0);
00125 fwrite (byteLine,paddedWidth,1,outfile);
00126 }
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
00134 return 0;
00135 }
00136