c语言简单图像处理,希望大家帮忙看看--根据例子,补充程序

minna1 2011-08-03 10:30:26
先上来例子
这个是bmp.c文件
#include "image.h"

typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned short WORD;
typedef unsigned long LONG;

#define BI_RGB 0L
#define BI_RLE8 1L
#define BI_RLE4 2L
#define BI_BITFIELDS 3L

// BMP头文件定义
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;

typedef struct tagBITMAPCOREHEADER {
DWORD bcSize;
WORD bcWidth;
WORD bcHeight;
WORD bcPlanes;
WORD bcBitCount;
} BITMAPCOREHEADER, *PBITMAPCOREHEADER;

typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;

#define MAXCOLORS 256

//2个字节整数写入
int fwriteWORD(WORD val,FILE *fp)
{
int i,c;

c=val;
for(i=0;i<2;i++) {
fputc(c%256,fp);
c/=256;
}
return TRUE;
}

// 4个字节的整数写入
int fwriteDWORD(DWORD val,FILE *fp)
{
int i,c;

c=val;
for(i=0;i<4;i++) {
fputc(c%256,fp);
c/=256;
}
return TRUE;
}

// 2个字节的整数读取
int freadWORD(WORD *res,FILE *fp)
{
int i,c;
int val[2];

for(i=0;i<2;i++) {
c=fgetc(fp);
if(c==EOF) return FALSE;
val[i]=c;
}
*res=val[1]*256+val[0];
return TRUE;
}

// 4个字节的整数读取
int freadDWORD(DWORD *res,FILE *fp)
{
int i,c;
int val[4];
DWORD tmp=0;

for(i=0;i<4;i++) {
c=fgetc(fp);
if(c==EOF) return FALSE;
val[i]=c;
}
tmp=0;
for(i=3;i>=0;i--) {
tmp*=256;
tmp+=val[i];
}
*res=tmp;
return TRUE;
}
//判定bmp
static BOOL IsWinDIB(BITMAPINFOHEADER* pBIH)
{
if (((BITMAPCOREHEADER*)pBIH)->bcSize == sizeof(BITMAPCOREHEADER)) {
return FALSE;
}
return TRUE;
}

int countOfDIBColorEntries(int iBitCount)
{
int iColors;

switch (iBitCount) {
case 1:
iColors = 2;
break;
case 4:
iColors = 16;
break;
case 8:
iColors = 256;
break;
default:
iColors = 0;
break;
}

return iColors;
}

int getDIBxmax(int width,int depth)
{
switch(depth) {
case 32:
return width*4;
case 24:
//return mx;
return ((width*3)+3)/4*4;
break;
case 16:
return (width+1)/2*2;
break;
case 8:
return (width+3)/4*4;
break;
case 4:
return (((width+1)/2)+3)/4*4;
break;
case 1:
return (((width+7)/8)+3)/4*4;
}
return width;
}
// 根据bmp读取
int readBMPfile(char *filename,ImageData **image)
{
int i,c;
int errcode=0;
BITMAPFILEHEADER BMPFile;
BITMAPINFOHEADER BMPInfo;
BITMAPCOREHEADER BMPCore;
int colors;
int colorTableSize;
int bitsSize;
int BISize;
int x,y;
int mx,my,depth;
int pad;
int mxbyte; // 一行所需要的字节数
int isPM=FALSE;
FILE *fp;

WORD HEAD_bfType;
DWORD HEAD_bfSize;
WORD HEAD_bfReserved1;
WORD HEAD_bfReserved2;
DWORD HEAD_bfOffBits;
DWORD INFO_bfSize;
Pixel palet[MAXCOLORS];
Pixel setcolor;

if((fp=fopen(filename,"rb"))==NULL) {
return -1;
}

//bmp文件一定要bm(0x4d42)开始,在那意外的场合不是bmp就中止
if(!freadWORD(&HEAD_bfType,fp)) {
errcode=-2;
goto $ABORT;
}
if (HEAD_bfType != 0x4d42) {
errcode=-10;
goto $ABORT;
}
// 头文件大小(Byte)
if(!freadDWORD(&HEAD_bfSize,fp)) {
errcode=-10;
goto $ABORT;
}
// 预约用
if(!freadWORD(&HEAD_bfReserved1,fp)) {
errcode=-10;
goto $ABORT;
}
// 预约领域(没使用)
if(!freadWORD(&HEAD_bfReserved2,fp)) {
errcode=-10;
goto $ABORT;
}
// offset
if(!freadDWORD(&HEAD_bfOffBits,fp)) {
errcode=-10;
goto $ABORT;
}
// 头文件大小
if(!freadDWORD(&INFO_bfSize,fp)) {
errcode=-10;
goto $ABORT;
}
if (INFO_bfSize == 40/*sizeof(BITMAPINFOHEADER)*/ || INFO_bfSize == 12/*sizeof(BITMAPCOREHEADER)*/) {
BMPInfo.biSize = INFO_bfSize;

if(INFO_bfSize == sizeof(BITMAPCOREHEADER)) {
WORD tmp;
isPM = TRUE;
// 图像的横
if(!freadWORD(&tmp,fp)) {
errcode=-10;
goto $ABORT;
}
BMPInfo.biWidth=tmp;
//图像的纵
if(!freadWORD(&tmp,fp)) {
errcode=-10;
goto $ABORT;
}
BMPInfo.biHeight=tmp;

if(!freadWORD(&(BMPInfo.biPlanes),fp)) {
errcode=-10;
goto $ABORT;
}
// 1个像素的字节数
if(!freadWORD(&(BMPInfo.biBitCount),fp)) {
errcode=-10;
goto $ABORT;
}
}
else {
// 图像的横
if(!freadDWORD(&(BMPInfo.biWidth),fp)) {
errcode=-10;
goto $ABORT;
}

if(!freadDWORD(&(BMPInfo.biHeight),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadWORD(&(BMPInfo.biPlanes),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadWORD(&(BMPInfo.biBitCount),fp)) {
errcode=-10;
goto $ABORT;
}
}
if(!isPM) {
if(!freadDWORD(&(BMPInfo.biCompression),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadDWORD(&(BMPInfo.biSizeImage),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadDWORD(&(BMPInfo.biXPelsPerMeter),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadDWORD(&(BMPInfo.biYPelsPerMeter),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadDWORD(&(BMPInfo.biClrUsed),fp)) {
errcode=-10;
goto $ABORT;
}
if(!freadDWORD(&(BMPInfo.biClrImportant),fp)) {
errcode=-10;
goto $ABORT;
}
}
}
else {
errcode=-10;
goto $ABORT;
}
mx=BMPInfo.biWidth;
my=BMPInfo.biHeight;
depth=BMPInfo.biBitCount;
if(depth!=8 && depth!=24) {
errcode=-3;
goto $ABORT;
}
if(BMPInfo.biCompression!=BI_RGB) {
errcode=-20;
goto $ABORT;
}
if(BMPInfo.biClrUsed==0) {
colors = countOfDIBColorEntries(BMPInfo.biBitCount);
}
else {
colors = BMPInfo.biClrUsed;
}

if (!isPM) {
for(i=0;i<colors;i++) {
// Blue
c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
palet[i].b=c;
// Green
c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
palet[i].g=c;
// Red
c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
palet[i].r=c;

c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
}
} else {
for(i=0;i<colors;i++) {
c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
palet[i].b=c;
c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
palet[i].g=c;
c=fgetc(fp);
if(c==EOF) {
errcode=-10;
goto $ABORT;
}
palet[i].r=c;
}
}
*image=createImage(mx,my,24);
mxbyte=getDIBxmax(mx,depth);
pad=mxbyte-mx*depth/8;
for(y=my-1;y>=0;y--) {
for(x=0;x<mx;x++) {
if(depth==8) {
c=fgetc(fp);
if(c==EOF) {
errcode=-20;
goto $ABORT;
}
setcolor.r=palet[c].r;
setcolor.g=palet[c].g;
setcolor.b=palet[c].b;
}
else if(depth==24) {
c=fgetc(fp);
if(c==EOF) {
errcode=-20;
goto $ABORT;
}
setcolor.b=c;
c=fgetc(fp);
if(c==EOF) {
errcode=-20;
goto $ABORT;
}
setcolor.g=c;
c=fgetc(fp);
if(c==EOF) {
errcode=-20;
goto $ABORT;
}
setcolor.r=c;
}
setPixel(*image,x,y,&setcolor);
}
for(i=0;i<pad;i++) {
c=fgetc(fp);
if(c==EOF) {
errcode=-20;
goto $ABORT;
}
}
}
$ABORT:
fclose(fp);
return errcode;
}

int writeBMPfile(char *filename,ImageData *image)
{
FILE *fp;
BITMAPFILEHEADER bfn;
int width,height;
int mxbyte;
//int x
int pad;
int depth;
int byte_per_pixel;
int palet_size;
int x,y,i;
int iBytes;
//unsigned int wsize;
Pixel pixel;

width=image->width;
height=image->height;
depth=image->depth;

if(depth!=24) {
//errcode=-3;
goto $abort1;
}
if(depth==24) {
byte_per_pixel=1;
}
else {
byte_per_pixel=depth/8;
}
if(depth>=24) {
palet_size=0;
}
else {
palet_size=256;
}
// -- end --
mxbyte=getDIBxmax(width,depth);
bfn.bfType=0x4d42; //'BM'
bfn.bfSize=14+40+/*sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER) +*/
palet_size*4/*sizeof(RGBQUAD)*/ +
mxbyte*height*byte_per_pixel;
bfn.bfReserved1=0;
bfn.bfReserved2=0;
bfn.bfOffBits=14+40/*sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER)*/ +
palet_size*4/*sizeof(RGBQUAD)*/;

if((fp=fopen(filename,"wb"))==NULL) {
goto $abort1;
}
fwriteWORD(bfn.bfType,fp);
fwriteDWORD(bfn.bfSize,fp);
fwriteWORD(bfn.bfReserved1,fp);
fwriteWORD(bfn.bfReserved2,fp);
fwriteDWORD(bfn.bfOffBits,fp);
fwriteDWORD(40/*sizeof(BITMAPINFOHEADER)*/,fp); //biSize
fwriteDWORD(width,fp); // biWidth
fwriteDWORD(height,fp); // biHeight
fwriteWORD(1,fp); // biPlanes
fwriteWORD(depth,fp); // biBitCount
fwriteDWORD(BI_RGB,fp); // biCompression
fwriteDWORD(0,fp); // biSizeImage
fwriteDWORD(300,fp); // biXPelsPerMeter
fwriteDWORD(300,fp); // biYPelsPerMeter
fwriteDWORD(0,fp); // biClrUsed
fwriteDWORD(0,fp); // biClrImportant

pad=mxbyte-width*depth/8;
for(y=height-1;y>=0;y--) {
for(x=0;x<width;x++) {
getPixel(image,x,y,&pixel);
fputc(pixel.b,fp);
fputc(pixel.g,fp);
fputc(pixel.r,fp);
}
for(i=0;i<pad;i++) {
fputc(0,fp);
}
}
return 0;
$abort1:
return 1;
$abort2:
fclose(fp);
return 1;
}
楼下给出iamge.c文件
然后给出题目。
...全文
210 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-08-04
  • 打赏
  • 举报
回复
仅供参考
#include <windows.h>
#include <stdio.h>

int main() {
const DWORD uWidth = 18 + 17 * 256, uHeight = 18 + 17 * 128;

PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2);
pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = uWidth;
pbmi->bmiHeader.biHeight = uHeight;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 1;
pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;
pbmi->bmiColors[0].rgbBlue = 0;
pbmi->bmiColors[0].rgbGreen = 0;
pbmi->bmiColors[0].rgbRed = 0;
pbmi->bmiColors[1].rgbBlue = 255;
pbmi->bmiColors[1].rgbGreen = 255;
pbmi->bmiColors[1].rgbRed = 255;

HDC hDC = CreateCompatibleDC (0);
void * pvBits;
HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, 0, &pvBits, NULL, 0);
SelectObject (hDC, hBitmap);
HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
SelectObject (hDC, hFont);
BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, WHITENESS);

char c[4];
int i, j;
for (i = 128; i < 256; i++) {
sprintf (c, "%02X", i);
TextOut (hDC, 1, (i - 127) * 17 + 1, c, 2);
}
for (j = 0; j < 256; j++) {
sprintf (c, "%02X", j);
TextOut (hDC, (j + 1)* 17 + 1, 1, c, 2);
}
for (i = 128; i < 256; i++) {
for (j = 0; j < 256; j++) {
c[0] = (char) i;
c[1] = (char) j;
TextOut (hDC, (j + 1) * 17 + 1, (i - 127) * 17 + 1, c, 2);
}
}
for (i = 0; i < 130; i++) {
MoveToEx (hDC, 0, i * 17, NULL);
LineTo (hDC, uWidth, i * 17);
}
for (j = 0; j < 258; j++) {
MoveToEx (hDC, j * 17, 0, NULL);
LineTo (hDC, j * 17, uHeight);
}

BITMAPFILEHEADER bmfh;
bmfh.bfType = *(PWORD) "BM";
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2;
bmfh.bfSize = bmfh.bfOffBits + pbmi->bmiHeader.biSizeImage;

HANDLE hFile = CreateFile ("goal.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD dwWritten;
WriteFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile (hFile, pbmi, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2, &dwWritten, NULL);
WriteFile (hFile, pvBits, pbmi->bmiHeader.biSizeImage, &dwWritten, NULL);

CloseHandle (hFile);
}

DeleteObject (hFont);
DeleteObject (hBitmap);
DeleteDC (hDC);
LocalFree (pbmi);

return 0;
}




minna1 2011-08-04
  • 打赏
  • 举报
回复
原来很多人不愿意帮助别人 还是你们不会啊 就是个bmp保存
minna1 2011-08-03
  • 打赏
  • 举报
回复
#include "image.h"
ImageData* createImage(int width,int height,int depth)
{
ImageData *new_image;
int byte_per_pixel;

if(width<0 || height<0) return NULL;
if(depth!=8 && depth!=24) return NULL;

new_image=malloc(sizeof(ImageData));
if(new_image==NULL) return NULL;
byte_per_pixel=depth/8;
new_image->pixels=malloc(sizeof(BYTE)*byte_per_pixel*width*height);
if(new_image->pixels==NULL) {
free(new_image);
return NULL;
}
new_image->width=width;
new_image->height=height;
new_image->depth=depth;
return new_image;
}

/* List2-3
废除图片
*/
void disposeImage(ImageData *image)
{
if(image->pixels!=NULL) free(image->pixels);
free(image);
return;
}

/* List2-5
图像像素值的取得
x,y 图像坐标

*/
int getPixel(ImageData *image,int x,int y,Pixel *pixel)
{
int return_value=1;
int adress;
int depth,gray_value;
BYTE *pixels;

if(image==NULL) return -1;
if(image->pixels==NULL) return -1;
if(x<0) {
x=0;
return_value=0;
}
if(x >= image->width ) {
x=image->width -1;
return_value=0;
}
if(y<0) {
y=0;
return_value=0;
}
if(y >= image->height ) {
y=image->height -1;
return_value=0;
}
depth=image->depth;
adress=x + y*image->width;
pixels=image->pixels;
if(depth==8) {
gray_value=pixels[adress];
pixel->r=gray_value;
pixel->g=gray_value;
pixel->b=gray_value;
}
else if(depth==24) {
pixels+=(adress*3);
pixel->r=(*pixels);
pixels++;
pixel->g=(*pixels);
pixels++;
pixel->b=(*pixels);
}
else {
return -1;
}
return return_value;
}

int correctPixelValue(int value,int max)
{
if(value<0) return 0;
if(value>max) return max;
return value;
}

int setPixel(ImageData *image,int x,int y,Pixel *pixel)
{
int adress;
int depth;
BYTE *pixels;

if(image==NULL) return -1;
if(image->pixels==NULL) return -1;
if(x<0 || x >= image->width || y<0 || y >= image->height ) {
return 0;
}
depth=image->depth;
adress=x + y*image->width;
pixels=image->pixels;
if(depth==8) {
pixels[adress]=correctPixelValue(pixel->r,PIXELMAX);
}
else if(depth==24) {
pixels+=(adress*3);
(*pixels)=correctPixelValue(pixel->r,PIXELMAX);
pixels++;
(*pixels)=correctPixelValue(pixel->g,PIXELMAX);
pixels++;
(*pixels)=correctPixelValue(pixel->b,PIXELMAX);
}
else {
return -1;
}
return 1;
}
以上是例子。下面给出题目
#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
typedef unsigned char BYTE;


typedef struct STRUCT_IMAGE{
int width,height;
int depth;
void* pixels;
}ImageData;

#define PIXELMAX 255


typedef struct STRUCT_PIXEL{
int r; // Red
int g; // Green
int b; // Blue
}Pixel;

typedef struct STRUCT_COORDINATE{
int x,y; // 返回处理所有坐标
int buffer1,buffer2,buffer3;
}Coordinate;


ImageData* createImage(int width,int height,int depth);
void disposeImage(ImageData *img);
int readBMPfile(char *fname,ImageData **img);
int writeBMPfile(char *fname,ImageData *img);
int getPixel(ImageData *img,int x,int y,Pixel *pix);
int setPixel(ImageData *img,int x,int y,Pixel *pix);

#include "image.h"

main(int ac,char *av[])
{
ImageData *image;
int x,y;
Pixel pixel1,pixel2;

pixel1.r=255;
pixel1.g=255;
pixel1.b=255;
pixel2.r=0;
pixel2.g=0;
pixel2.b=0;
image=createImage(100,100,24);
for(y=0;y<100;y++) {
for(x=0;x<100;x++) {
if(x%2 == y%2) {
setPixel(image,x,y,&pixel1);
}
else {
setPixel(image,x,y,&pixel2);
}
}
}


//
//在这个地方输入您的代码
//


disposeImage(image);

}

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧