【问题标题】:bitmap image file manipulation using bitmap file structure in c在c中使用位图文件结构操作位图图像文件
【发布时间】:2012-12-26 20:16:24
【问题描述】:

我想实现一个简单的基于命令行的图像编辑器。该计划将 提供一个基于文本的菜单,它为用户提供了几个操作窗口的功能 位图 (.bmp) 图像文件。菜单将包括加载图像、旋转图像、镜像、保存图像 和退出选项。加载图像选项将用于打开和读取给定的像素值 位图文件。此选项还将打印给定文件的基本属性,例如尺寸和总大小。旋转和镜像选项将处理先前读取的像素值。一个图像 必须在应用这些选项之前加载。保存选项将像素值保存在 内存到具有给定文件名的位图文件。

关于这个项目和位图文件结构,你推荐给我哪种方法?

如果您就某个特定主题(例如加载文件)给我建议,我们将不胜感激。

【问题讨论】:

    标签: c file bitmap structure bitmapimage


    【解决方案1】:

    libbmp 将使您的程序实现起来几乎是微不足道的。

    【讨论】:

    • 感谢您的推荐。我查看了该库,但其中没有读取位图文件的功能。
    • @hotaru,显然有。这就是该库的全部目的
    • 很抱歉我找不到,这些人也找不到code.google.com/p/libbmp/issues/detail?id=4 如果你能找到我很高兴看到它,因为我非常需要它:D
    • @hotaru,bmp_get_* 的所有函数呢?
    【解决方案2】:

    如果你真的想使用 C,那么试试 libbmp 库http://code.google.com/p/libbmp/

    但是,我建议使用 C#,这样使用 System.Drawing 命名空间就可以轻松完成任务。

    【讨论】:

    • 感谢您的帮助,不幸的是我必须在这个项目中使用 c
    【解决方案3】:

    此函数用于将 bmp 文件加载到内存中。 你必须先声明一个带有 bmp 结构的头文件

    BMP* load_BMP(char *filename);
    
    BMP *bmp;       // local integer for file loaded
    FILE *in;       // pointer for file opening
    int rowsize;   
    
    int row, col, color, i;
    unsigned char b;
    
    in=fopen(filename,"rb"); // open binary file
    if (in==NULL)
    {
        printf("Problem in opening file %s.\n",filename);
        return NULL;
    }
    
    
    bmp=(BMP*) malloc(sizeof(BMP)); //memory allocation
    if (bmp==NULL)
    {
        printf("Not enough memory to load the image.\n");
        return NULL;
    }
    
    fread(bmp->BM,2,1,in);
    if (bmp->BM[0]!='B' || bmp->BM[1]!='M')
    {
        printf("Bad BMP image file.\n");
        free(bmp);
        return NULL;
    }
    
    fread(&bmp->fileSize,4,1,in);
    fread(&bmp->Reserved1,2,1,in);
    fread(&bmp->Reserved2,2,1,in);
    fread(&bmp->imageOffset,4,1,in);
    fread(&bmp->imageHeaderSize,4,1,in);
    fread(&bmp->imageWidth,4,1,in);
    
    rowsize=4*((3*bmp->imageWidth+3)/4); //calculate rowsize because of padding
    
    fread(&bmp->imageHeight,4,1,in);
    fread(&bmp->colorPlanes,2,1,in);
    fread(&bmp->compressionMethod,4,1,in);
    fread(&bmp->imageSize,4,1,in);
    fread(&bmp->hPPM,4,1,in);
    fread(&bmp->vPPM,4,1,in);
    fread(&bmp->paletteColors,4,1,in);
    fread(&bmp->paletteImportantColors,4,1,in);
    
    
    bmp->data=(unsigned char*) malloc(bmp->imageSize); //allocate memory for image data array
    if (bmp->data==NULL)
    {
        printf("There is not enough memory to load the image\n");
        free(bmp);
        return NULL;
    }
    
    for(row=0;row<bmp->imageHeight;row++)  //read picture data
    {
        for(col=0;col<bmp->imageWidth;col++)
            for(color=0;color<=2;color++)
                fread(&bmp->data[row*rowsize+3*col+color],
                sizeof(unsigned char),1,in);
    
        //read extra bytes for end of row padding
        for(i=0;i<rowsize-3*bmp->imageWidth;i++)
            fread(&b,1,1,in);
    }
    
    fclose(in);
    return bmp;
    

    }

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 2012-12-26
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多