none
C++读取bmp文件 RRS feed

  • 问题

  • 头文件如下:

    typedef struct
    {
     int width;             // 图像像素的宽度
     int height;             // 图像像素的高度
     int format;             // Number Of Bytes Per Pixel
     //unsigned char *data;          // 纹理数据
     byte *data;
    } TEXTURE_IMAGE;

    typedef TEXTURE_IMAGE *P_TEXTURE_IMAGE;       // 一个对纹理图像数据类型的指针

    P_TEXTURE_IMAGE AllocateTextureBuffer( int  w, int h, int f);
    void DeallocateTexture( P_TEXTURE_IMAGE t );
    int ReadTextureData ( char *filename, P_TEXTURE_IMAGE buffer);

    程序如下:

    P_TEXTURE_IMAGE AllocateTextureBuffer( int  w, int h, int f)
    {
      P_TEXTURE_IMAGE ti = NULL;         // Pointer To Image Struct
       // unsigned char *c = NULL;         // Pointer To Block Memory For Image
        byte *c = NULL; 
        ti = (P_TEXTURE_IMAGE)malloc(sizeof(TEXTURE_IMAGE));  // One Image Struct Please
       
        if( ti != NULL )
     {
            ti->width  = w;           // Set Width
            ti->height = h;           // Set Height
            ti->format = f;           // Set Format
            //c = (unsigned char *)malloc( w * h * f);
            c = (byte *)malloc( w * h * f);
      memset(c, 0, w*h*f);
            if ( c != NULL )
      {
                ti->data = c;
            }
            else
      {
       MessageBox(NULL,"Could Not Allocate Memory For A Texture Buffer","BUFFER ERROR",MB_OK | MB_ICONINFORMATION);
                return NULL;
            }
        }
        else
     {
      MessageBox(NULL,"Could Not Allocate An Image Structure","IMAGE STRUCTURE ERROR",MB_OK | MB_ICONINFORMATION);
      return NULL;
        }
     return ti;                  // Return Pointer To Image Struct
    }

    // Free Up The Image Data
    void DeallocateTexture( P_TEXTURE_IMAGE t )
    {
     if(t)
     {
      if(t->data)
      {
       free(t->data);
      }

      free(t);
     }
    }

    // Read A .RAW File In To The Allocated Image Buffer Using Data In The Image Structure Header.
    // Flip The Image Top To Bottom.  Returns 0 For Failure Of Read, Or Number Of Bytes Read.
    int ReadTextureData ( char *filename, P_TEXTURE_IMAGE buffer)
    {
     FILE *f;
     int i,j,k,done=0;
     int stride = buffer->width * buffer->format;    // Size Of A Row (Width * Bytes Per Pixel)
     //unsigned char *p = NULL;
        byte *p=NULL;

        // windows.h 给我们这些类型来处理Bitmap文件

        BITMAPFILEHEADER fileheader;
        BITMAPINFOHEADER infoheader;
        RGBTRIPLE rgb;
      
     f = fopen(filename, "rb");         // Open "filename" For Reading Bytes
        fread(&fileheader, sizeof(fileheader), 1, f); // Read the fileheader
       
        fseek(f, sizeof(fileheader), SEEK_SET); // Jump the fileheader
        fread(&infoheader, sizeof(infoheader), 1, f); // and read the infoheader


     if( f != NULL )            // If File Exists
     {
     // for( i = buffer->height-1; i >= 0 ; i-- ) // Loop Through Height (Bottoms Up - Flip Image)
      for( i =0 ; i <buffer->height; i++) 
      {
       p = buffer->data+ (i * stride );     //
       for ( j = 0; j < buffer->width ; j++ )    // Loop Through Width
       {
        for ( k = 0 ; k < buffer->format-1 ; k++, p++, done++ )
        {
        // fread(&rgb, sizeof(rgb), 1, f);
         p = fgetc(f);        // Read Value From File And Store In Memory
           }
        *p = 255; p++;         // Store 255 In Alpha Channel And Increase Pointer
       }
      }

           // Close The File
     }
     else              // Otherwise
     {
      MessageBox(NULL,"Unable To Open Image File","IMAGE ERROR",MB_OK | MB_ICONINFORMATION);
     }
     return done;            // Returns Number Of Bytes Read In
    }

     

    最后读出来的图像。。红色变成了蓝色。

    2010年4月14日 8:25

答案

全部回复