c语言bmp转换成jpg,用c语言把bmp格式的彩色图片转换成黑白的

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

typedef unsigned char BYTE;

#define RGB 3.0

#define MAX 255.0

struct BitMap

{

short Type;

long Size;

short Reserve1;

short Reserve2;

long Offset;

long FileSize;

long Width;

long Height;

short ClrPlanes;

short BitsPerPix;

long Compression;

long ImSize;

long XPixPerMeter;

long YPixPerMeter;

long ClrTBClr;

long ImpClr;

} Header;

typedef struct

{

BYTE Blue;

BYTE Green;

BYTE Red;

} __attribute__((__packed__))

RGBTRIPLE;

int main(int argc, char **argv) {

FILE *inptr, *outptr;

float threshold;

threshold = atoi(argv[2]);

if(argc < 3) {

printf("Usage : ./a.out threshold\n");

exit(0);

}

else inptr = fopen(argv[1], "rb");

outptr = fopen ("out.bmp", "wb");

memset(&Header, 0, sizeof(Header));

fread(&Header.Type, 2, 1, inptr);

fwrite(&Header.Type, 2, 1, outptr);

fread(&Header.Size, 4, 1, inptr);

fwrite(&Header.Size, 4, 1, outptr);

fread(&Header.Reserve1, 2, 1, inptr);

fwrite(&Header.Reserve1, 2, 1, outptr);

fread(&Header.Reserve2, 2, 1, inptr);

fwrite(&Header.Reserve2, 2, 1, outptr);

fread(&Header.Offset, 4, 1, inptr);

fwrite(&Header.Offset, 4, 1, outptr);

fread(&Header.FileSize, 4, 1, inptr);

fwrite(&Header.FileSize, 4, 1, outptr);

fread(&Header.Width, 4, 1, inptr);

fwrite(&Header.Width, 4, 1, outptr);

fread(&Header.Height, 4, 1, inptr);

fwrite(&Header.Height, 4, 1, outptr);

fread(&Header.ClrPlanes, 2, 1, inptr);

fwrite(&Header.ClrPlanes, 2, 1, outptr);

fread(&Header.BitsPerPix, 2, 1, inptr);

fwrite(&Header.BitsPerPix, 2, 1, outptr);

fread(&Header.Compression, 4, 1, inptr);

fwrite(&Header.Compression, 4, 1, outptr);

fread(&Header.ImSize, 4, 1, inptr);

fwrite(&Header.ImSize, 4, 1, outptr);

fread(&Header.XPixPerMeter, 4, 1, inptr);

fwrite(&Header.XPixPerMeter, 4, 1, outptr);

fread(&Header.YPixPerMeter, 4, 1, inptr);

fwrite(&Header.YPixPerMeter, 4, 1, outptr);

fread(&Header.ClrTBClr, 4, 1, inptr);

fwrite(&Header.ClrTBClr, 4, 1, outptr);

fread(&Header.ImpClr, 4, 1, inptr);

fwrite(&Header.ImpClr, 4, 1, outptr);

// iterate over infile's scanlines

int i,j;

if (Header.Compression!=24) {

printf("Error: This program only deals with 24 compressed bmp file\n");

return 1;

}

if (threshold <0 || threshold >1.0) {

printf("Error: Threshold value is not valid\n");

return 1;

}

else for (i=0; i

{

// iterate over pixels in scanline

for (j=0; j

{

// temporary storage

RGBTRIPLE triple, tripleout;

int sum = 0;

// read RGB triple from infile

fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

sum = triple.Red+triple.Green+triple.Blue;

float ratio = sum/(RGB*MAX);

printf("(%d, %d, %d)\n", triple.Red, triple.Green, triple.Blue);

// prints rgb values for each pixel (used to check if the colours were right)

printf("ratio is: %f\n", ratio);

// gives the ratio

if (ratio

tripleout.Red = 0;

tripleout.Green = 0;

tripleout.Blue = 0;

}

// this part needs to be done

else {

tripleout.Red = 255;

tripleout.Green = 255;

tripleout.Blue = 255;

}

// write RGB triple to outfile

fwrite(&tripleout, sizeof(RGBTRIPLE), 1, outptr);

}

}

fclose(inptr);

fclose(outptr);

return 0;

}

/* To use compile: gcc -Wall -o check2 check2.c

After compiling

./check filename.bmp thresholdvalue

*/

#include <stdio.h> #include <stdlib.h> #include <jpeglib.h> #include <bmp_lib.h> int jpg2bmp(char *jpgname, char *bmpname) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *fp; int row_stride; int i, j; unsigned char * line_buffer; unsigned char *buffer; unsigned char *data; if ((fp = fopen(jpgname, "rb")) == NULL) { printf("open %s failed\n", jpgname); return -1; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); (void) jpeg_read_header(&cinfo, TRUE); (void) jpeg_start_decompress(&cinfo); row_stride = cinfo.output_width * cinfo.output_components; line_buffer = (unsigned char *)malloc(row_stride); buffer = (unsigned char *)malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components); data = buffer; while (cinfo.output_scanline < cinfo.output_height) { (void) jpeg_read_scanlines(&cinfo, &line_buffer, 1); for (i = 0; i < cinfo.output_width*cinfo.output_components; i++) *(buffer++) = *(line_buffer + i); } (void) jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(fp); free(line_buffer); bmp_write(bmpname, cinfo.output_width, cinfo.output_height, 24, data); free(data); return 0; } int main(int argc, char *argv[]) { if (argc != 3) { printf("useage: jpg2bmp jpgname bmpname\n"); return -1; } jpg2bmp(argv[1], argv[2]); return 0; }答案:我提供了一段C语言的代码,可以将jpg格式的图片转换成bmp格式图片:#include <stdio.h>#include <stdlib.h>#include <jpeglib.h>#include <bmp_lib.h>int jpg2bmp(char *jpgname, char *bmpname){ struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *fp; int row_stride; int i, j; unsigned char * line_buffer; unsigned char *buffer; unsigned char *data; if ((fp = fopen(jpgname, "rb")) == NULL){ printf("open %s failed\n", jpgname); return -1;} cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); (void) jpeg_read_header(&cinfo, TRUE); (void) jpeg_start_decompress(&cinfo); row_stride = cinfo.output_width * cinfo.output_components; line_buffer = (unsigned char *)malloc(row_stride); buffer = (unsigned char *)malloc(cinfo.output_width*cinfo.output_height*cinfo.output_components); data = buffer; while (cinfo.output_scanline < cinfo.output_height){ (void) jpeg_read_scanlines(&cinfo, &line_buffer, 1); for (i = 0; i < cinfo.output_width*cinfo.output_components; i++) *(buffer++) = *(line_buffer + i);} (void) jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(fp); free(line_buffer); bmp_write(bmpname, cinfo.output_width, cinfo.output_height, 24, data); free(data); return 0;} int main(int argc, char *argv[]){ if (argc != 3){ printf("useage: jpg2bmp jpgname bmpname\n"); return -1;} jpg2bmp(argv[1], argv[2]); return 0;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值