2007年8月28日

BMP文件结构如下
详情可见下文
BMP结构
BITMAPFILEHEADER    bmfh;  --文件头
BITMAPINFOHEADER bmih; --信息表
RGBQUAD aColors[]; --颜色表
BYTE aBitmapBits[]; --颜色数据

struct A{
    
char type[2];                 /* Magic identifier            */
    unsigned 
long size;                       /* File size in bytes          */
    unsigned 
short int reserved1, reserved2;
    unsigned 
long offset;                     /* Offset to image data, bytes */
};

struct B{
    unsigned 
long size;               /* Header size in bytes      */
    unsigned 
long width,height;                /* Width and height of image */
    unsigned 
short int planes;       /* Number of colour planes   */
    unsigned 
short int bits;         /* Bits per pixel            */
    unsigned 
long compression;        /* Compression type          */
    unsigned 
long imagesize;          /* Image size in bytes       */
    unsigned 
long xresolution,yresolution;     /* Pixels per meter          */
    unsigned 
long ncolours;           /* Number of colours         */
    unsigned 
long importantcolours;   /* Important colours         */
};
以上是C语言的结构定义,下面是结构的含义,英文很简单,就不多说了。

The BITMAPFILEHEADER:
start     size     name     stdvalue     purpose
1     2     bfType     19778     must always be set to 'BM' to declare that this is a .bmp-file.
3     4     bfSize     ??     specifies the size of the file in bytes.
7     2     bfReserved1     0     must always be set to zero.
9     2     bfReserved2     0     must always be set to zero.
11     4     bfOffBits     1078     specifies the offset from the beginning of the file to the bitmap data.
The BITMAPINFOHEADER:
start     size     name     stdvalue     purpose
15     4     biSize     40     specifies the size of the BITMAPINFOHEADER structure, in bytes.
19     4     biWidth     100     specifies the width of the image, in pixels.
23     4     biHeight     100     specifies the height of the image, in pixels.
27     2     biPlanes     1     specifies the number of planes of the target device, must be set to zero.
29     2     biBitCount     8     specifies the number of bits per pixel.
31     4     biCompression     0     Specifies the type of compression, usually set to zero (no compression).
35     4     biSizeImage     0     specifies the size of the image data, in bytes. If there is no compression, it is valid to set this member to zero.
39     4     biXPelsPerMeter     0     specifies the the horizontal pixels per meter on the designated targer device, usually set to zero.
43     4     biYPelsPerMeter     0     specifies the the vertical pixels per meter on the designated targer device, usually set to zero.
47     4     biClrUsed     0     specifies the number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.
51     4     biClrImportant     0     specifies the number of color that are 'important' for the bitmap, if set to zero, all colors are important.

根据C语言,我重新定义了C#下的结构,在定义结构时,请注意使用StructLayout属性来指示编译器如何定义结构,
因为CPU为了进行优化处理,会自动进行对称处理,结果struct大小会出乎你理料之外。可参见
Mastering structs in C#
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    
struct Header
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst
=2)]
        
char [] type ; //位图类型 2字节
        uint size; //ulong size;  //文件大小
        ushort reserved1, reserved2;  //文件保留字,为0
        uint offset;// ulong offset;  //位图起始位置
    }

    [StructLayout(LayoutKind.Sequential, Pack
=1)]
    
struct InforHeader
    {
        
uint size;// ulong size; //结构占用字节数
        public uint width, height;// ulong width, height; //宽度,高度
        ushort planes; //目标设备级别
        public ushort bits; //像素位数 1双色,4 16色,8 256色 24真柴色
        uint compression;// ulong compression; //压缩类型 0, 1, 2
        uint imagesize;// ulong imagesize; //位图大小
        uint xresolution, yresolution; //ulong xresolution, yresolution; //分辨率,象素数
        uint ncolors;// ulong ncolors;  //颜色数
        uint importantcolors;// ulong importantcolors; //重要颜色数
    }

在BMP结构中,要注意的是当BMP使用24bit model时,颜色表不再存在了,每个像素使用三个byte来表示RGB,从36H(54)的位置
开始存放像素信息。
以下是从BMP文件中读取信息。
            FileStream file = File.OpenRead("c:\\test1.bmp");
            BinaryReader br 
= new BinaryReader(file);
           
            
byte[] buffheader = new byte[Marshal.SizeOf(typeof(Header))];
            
byte[] buffInfoheader = new byte[Marshal.SizeOf(typeof(InforHeader))];

            
int amt = 0;
            amt 
+= file.Read(buffheader, amt, buffheader.Length);
            
long i = file.Position;

            GCHandle handle 
= GCHandle.Alloc(buffheader, GCHandleType.Pinned);
            Header header 
= (Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
                
typeof(Header));
            handle.Free();

            amt 
+= file.Read(buffInfoheader, 0, buffInfoheader.Length);
            i 
= file.Position;

            handle 
= GCHandle.Alloc(buffInfoheader, GCHandleType.Pinned);
            InforHeader infoheader 
= (InforHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
                
typeof(InforHeader));
            handle.Free();

            
//24位真彩没有颜色表,直接读颜色数据

            
byte [] buffData = new byte[infoheader.height*infoheader.width*3];

            amt 
+= file.Read(buffData, 0, buffData.Length);
C#中有丰富的处理图形的类,使用以上代码的机会并不多,只是喜欢C#才去改写C代码。

posted @ 2007-08-28 19:06 wind2 阅读(129) 评论(0) 编辑

2006年6月13日

 

今天我一个哥们终于如愿拿到一个政府的面试机会,是.net 开发。他想进政府也不是一天两天的了,已经坚持给政府发简介快一年了,以前收到两个电话面试,地点都不错,是温哥华的,可惜没能把握住机会,这次的机会比较大,过了一轮,让他飞过去面试,但是居然不报销,政府也够抠门的,不过值的,都说进了政府就象进了天堂一样,很想见识下政府的IT部门是个怎么样,我就聊聊那哥们的经过吧。

 

面试时间不长,只有一个多小时,三个人问,开始还以为是HR部门的,后来告别时,要了下卡片,才知道两个是IT部门的,一个是HR的。问的问题有些好玩,主要是很general 的问题,比如说如何处理同事关系了啊,如何对待工作任务,有意思的是有个问题是,如果你去修改别人的code,结果发现都是garbage,怎么办,答案当然是,不要自以是,各有各的风格,估计政府的那帮人也知道自己写的code是什么玩意,但不能让你给笑话了。

 

剩下的技术问题就好玩了,

1.How do you use ASP.net? (KAO,这个问题问的太不专业了,那有面试用use这个词),问的奇怪,我那哥们只好说,用它作过些user control, ….按理就要根据回答深入一层了,结果stop了,没有继续问下去了,呵,真是好水。

2.How do you use C#? ( 没听过这样问C#的问题的,这是第一次,不是语言不好啊,那都是native speaker)答:因为不会VB.net, 就只好用C#了,而且估计MS也不会继续支持VB了。。。。(答的不太对题,但也想不出要如何答。)

3.你写过JavaScript吗?答:写过,但用的不多,主要是C#代码,呵,也打住了不在问了

4.你作过些什么? 答:。。。,当中提到了odbc,于是提问者好奇了,什么是odbc啊,真是晕倒,就算是只用过oracle,也不会不知道odbc吧。

 

以上就是主要的技术问题了,听完后,真要倒地不起了,就这样的一个职位,工资还可以,有5万加元,活是估计没什么的了,一天作个2小时也就到顶了,最主要的是不用担心lay off,而且每年还有大把的假,以前光听说政府的人都是一帮废物,拿着高薪,天天只会吹牛瞎摆忽,今天总算是见识到了,国内国外在这点上也没什么区别啊。

  

posted @ 2006-06-13 11:03 wind2 阅读(521) 评论(5) 编辑

2006年5月4日

摘要: 上周被头叫去,临时加入到一个team,就谈谈这一周的感觉。
Team的组成:
Team leader 新进公司的,有多年的开发经验,开发过不少大型项目,对Design很熟,主要在项目中负责Class设计,但对业务逻辑不了解。
程序员A 也是新进公司的,有几年经验,写.net程序很熟,但同样对业务逻辑不了解。
阅读全文
posted @ 2006-05-04 10:10 wind2 阅读(7681) 评论(14) 编辑