从C开始

          学习和研究计算机科学,尤其是在windows的环境下学习和研究计算机科学,永远也绕不过去的两道坎就是C和C++;

       C和C++是一种知识和这种知识的延伸。所以,从C开始------没错; 

        基于以前在C#下的编程经验,捡起C语言开始并不困难,快速的浏览了编程语言的通有的一些知识,数据类型、运算符、表达式、语句、循环、分支、跳转、输入输出等。函数隔过去没看。重点看了一下C的数组和指针、内存管理等。对应用程序内存的管理和分配有了些新的认识,这种感觉是在.net/java 一类的语言使用过程中体会不到的。建议有C#经验和正在从事C#编程的同行们抽空研读一下C下的Malloc()、Calloc()、free()、memset()和memcpy()等,应该会有新的收获。

 

学习程序,最好的方法是读代码并调试通过它。下面这段程序是我用来学习C研究的一个sample,是关于用C语言转换Jpeg文件到bmp文件的。后面会边学习边整理这段程序。

这段程序用到了些数学知识,参见:http://blog.csdn.net/supnumb/archive/2007/07/28/1714549.aspx

 main.cpp文件

//  JpegToBmp.cpp : Defines the entry point for the console application.
//
// #ifndef __JPEGDEC_H__
// #define __JPEGDEC_H__ 

#include 
" stdafx.h "

// BEGINNING OF HEADERS
/

#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< string .h >


#define  BYTE unsigned char
#define  WORD unsigned short int

#define  DWORD unsigned int
#define  SDWORD signed int

#define  SBYTE signed char
#define  SWORD signed short int

int  load_JPEG_header(FILE  * fp, DWORD  * X_image, DWORD  * Y_image);
void  decode_JPEG_image();
int  get_JPEG_buffer(WORD X_image,WORD Y_image, BYTE  ** address_dest_buffer);

// #endif

/
// BEGINNING OF PROGRAM SOURCE
//

// #include "jpegdec.h"
#include  < conio.h >
#include 
< time.h >
char   * FileName = " image.jpg " ;
extern   char  error_string[ 90 ];

typedef 
struct  s_BM_header {
              WORD BMP_id ; 
//  'B''M'
              DWORD size;  //  size in bytes of the BMP file
              DWORD zero_res;  //  0
              DWORD offbits;  //  54
              DWORD biSize;  //  0x28
              DWORD Width;   //  X
              DWORD Height;   //  Y
              WORD  biPlanes;  //  1
              WORD  biBitCount ;  //  24
              DWORD biCompression;  //  0 = BI_RGB
              DWORD biSizeImage;  //  0
              DWORD biXPelsPerMeter;  //  0xB40
              DWORD biYPelsPerMeter;  //  0xB40
              DWORD biClrUsed;  // 0
              DWORD biClrImportant;  // 0
} BM_header;
typedef 
struct  s_RGB {
             BYTE B;
             BYTE G;
             BYTE R;
} RGB;


// ==============================================================
//
//  打印错误消息
//
// ==============================================================
void  exitmessage( char   * message)
{
    printf(
" %s " ,message);exit( 0 );
}

// ==============================================================
//
//  写入bmp文件
//
// ==============================================================
void  write_buf_to_BMP(BYTE  * im_buffer, WORD X_bitmap, WORD Y_bitmap,  char   * BMPname)
{
    SWORD x,y;
    RGB 
* pixel;
    BM_header BH;
    FILE 
* fp_bitmap;
    DWORD im_loc_bytes;
    BYTE nr_fillingbytes, i;
    BYTE zero_byte
= 0 ;

    fp_bitmap
= fopen(BMPname, " wb " );
    
if  (fp_bitmap == NULL) exitmessage( " File cannot be created " );

    
if  (X_bitmap % 4 != 0 ) nr_fillingbytes = 4 - ((X_bitmap * 3L ) % 4 );
    
else  nr_fillingbytes = 0 ;

    BH.BMP_id 
=   ' M ' * 256 + ' B ' ;
    fwrite(
& BH.BMP_id, 2 , 1 ,fp_bitmap);

    BH.size
= 54 + Y_bitmap * (X_bitmap * 3 + nr_fillingbytes);
    fwrite(
& BH.size, 4 , 1 ,fp_bitmap);

    BH.zero_res 
=   0 ;             
    fwrite(
& BH.zero_res, 4 , 1 ,fp_bitmap);

    BH.offbits 
=   54 ;             
    fwrite(
& BH.offbits, 4 , 1 ,fp_bitmap);

    BH.biSize 
=   0x28 ;            
    fwrite(
& BH.biSize, 4 , 1 ,fp_bitmap);

    BH.Width 
=  X_bitmap;          
    fwrite(
& BH.Width, 4 , 1 ,fp_bitmap);

    BH.Height 
=  Y_bitmap;          
    fwrite(
& BH.Height, 4 , 1 ,fp_bitmap);

    BH.biPlanes 
=   1 ;             
    fwrite(
& BH.biPlanes, 2 , 1 ,fp_bitmap);

    BH.biBitCount 
=   24 ;          
    fwrite(
& BH.biBitCount, 2 , 1 ,fp_bitmap);

    BH.biCompression 
=   0 ;        
    fwrite(
& BH.biCompression, 4 , 1 ,fp_bitmap);

    BH.biSizeImage 
=   0 ;          
    fwrite(
& BH.biSizeImage, 4 , 1 ,fp_bitmap);

    BH.biXPelsPerMeter 
=   0xB40 ;  
    fwrite(
& BH.biXPelsPerMeter, 4 , 1 ,fp_bitmap);

    BH.biYPelsPerMeter 
=   0xB40 ;  
    fwrite(
& BH.biYPelsPerMeter, 4 , 1 ,fp_bitmap);

    BH.biClrUsed 
=   0 ;              
    fwrite(
& BH.biClrUsed, 4 , 1 ,fp_bitmap);

    BH.biClrImportant 
=   0 ;          
    fwrite(
& BH.biClrImportant, 4 , 1 ,fp_bitmap);

    printf(
" Writing bitmap ... " );
    im_loc_bytes
= (DWORD)im_buffer + ((DWORD)Y_bitmap - 1 ) * X_bitmap * 4 ;

    
for  (y = 0 ;y < Y_bitmap;y ++ )
    {
        
for  (x = 0 ;x < X_bitmap;x ++ )
        {
            pixel
= (RGB  * )im_loc_bytes;
            fwrite(pixel, 
3 1 , fp_bitmap);
            im_loc_bytes
+= 4 ;
        }

        
for  (i = 0 ;i < nr_fillingbytes;i ++ )
            fwrite(
& zero_byte, 1 , 1 ,fp_bitmap);

        im_loc_bytes
-= 2L * X_bitmap * 4 ;
    }

    printf(
" Done. " );
    fclose(fp_bitmap);
}

// ==============================================================
//
//  控件台入品函数
//
// ==============================================================
void  main( int  argc,  char   * argv[])
{
    FILE 
* fp;
    DWORD X_image, Y_image;
    BYTE 
* our_image_buffer;
    clock_t start_time, finish_time;
    
float  duration;

    
if  (argc <= 1
        fp
= fopen(FileName, " rb " );
    
else  
        fp
= fopen(argv[ 1 ], " rb " );

    
if  (fp == NULL) 
        exitmessage(
" File not found ? " );

    
if  ( ! load_JPEG_header(fp, & X_image, & Y_image)) 
    {
        exitmessage(error_string);
        
return ;
    }

    fclose(fp);

    printf(
"  X_image = %d " ,X_image);
    printf(
"  Y_image = %d " ,Y_image);

    printf(
" Decoding JPEG image... " );

    
//  main decoder
    start_time  =  clock();
    
    
// 解压缩文件
    decode_JPEG_image();

    printf(
" Decoding finished. " );

    finish_time 
=  clock();

    duration 
=  ( double )(finish_time  -  start_time)  /  CLK_TCK;

    printf( 
" Time elapsed: %2.1f seconds " , duration );

    
if  ( ! get_JPEG_buffer(X_image,Y_image, & our_image_buffer))
    {
        exitmessage(error_string);
        
return ;
    }

    
// 写入bmp文件
    write_buf_to_BMP(our_image_buffer,X_image,Y_image,  " image.bmp " );

    getch();
}

 

<a target="_blank" href="http://wp.qq.com/wpa/qunwpa?idkey=5669f5b53fd11788bb11f9944667cb0d284fa17a9d048d2fa78e24f99585d88a"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="debian" title="debian"></a>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值