将pgm格式图像转换为pbm(黑白0或1)?

编程入门 行业动态 更新时间:2024-10-28 07:27:48
本文介绍了将pgm格式图像转换为pbm(黑白0或1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿所有人 请我想用c语言转换图像格式pgm(灰度)到pbm格式图像黑白) 我尝试过:

#include < stdio.h > #include < io.h > #include < ctype.h > // ... FILE * pFile = fopen( result.pgm, r); // 注意:使用Microsoft编译器,这些函数可能需要 // 得分领先(_filelength,_fileno) long file_len = filelength(fileno(pFile)); unsigned char * buffer =( unsigned char *)malloc(file_len); fread(buffer, 1 ,file_len,pFile); fclose(pFile); const char * header =( const char *)缓冲区; // 跳过幻数P4 while (isalnum(* header))header ++; // 跳过空格 while (isspace(* header))header ++; // 获取宽度 int width = atoi(header); // 跳过宽度 while (isdigit(* header))header ++; // 跳过空格 while (isspace(* header))header ++; int height = atoi(header); // 跳过高度 while (isdigit(* header))header ++; // 跳过单个空格 header ++; // 指向第一个数据(像素)字节的指针 const unsigned char * data =( const unsigned char *)header; // 如果宽度不是8的倍数,则每行额外字节 int stride = width% 8 ; for ( int i = 0 ; i< height; i ++) { for ( int j = 0 ; j< width / 8 ; j ++) { unsigned data_byte = * data ++; for ( int k = 0 ; k< 8 ; k ++) { // 这里的位顺序可能是错误的。 // 如果是这样,请检查掩码0x80并向左移动。 // 编辑:黑色是1,白色为零! printf( %d,, (data_byte& 1 )? 0 : 255 ); data_byte>> = 1 ; } } if (stride) { unsigned data_byte = * data ++; for ( int k = 0 ; k< stride; k ++) { // 再次:也许是订单这里的位错误。 printf( %d,, (data_byte& 1 )? 0 : 255 ); data_byte>> = 1 ; } } } 免费(缓冲);

解决方案

barneyman is对,你必须决定哪个灰色值是白色,哪个是黑色。没有一点变化,但是一个难题

data_byte =(data_byte> 0x80)? 0xff: 0 ;

把它带到极限; - )

hey all guys please I want to convert image format pgm (greyscale) to pbm format image black and white ) using c language What I have tried:

#include <stdio.h> #include <io.h> #include <ctype.h> // ... FILE *pFile = fopen("result.pgm", "r"); // NOTE: With Microsoft compilers these functions might require a // leading under score (_filelength, _fileno) long file_len = filelength(fileno(pFile)); unsigned char *buffer = (unsigned char *)malloc(file_len); fread(buffer, 1, file_len, pFile); fclose(pFile); const char *header = (const char *)buffer; // Skip magic number "P4" while (isalnum(*header)) header++; // Skip white spaces while (isspace(*header)) header++; // Get width int width = atoi(header); // Skip width while (isdigit(*header)) header++; // Skip whitespace while (isspace(*header)) header++; int height = atoi(header); // Skip height while (isdigit(*header)) header++; // Skip single whitespace header++; // Pointer to first data (pixel) byte const unsigned char *data = (const unsigned char *)header; // Extra byte per row if width is not a multiple of 8 int stride = width % 8; for (int i = 0; i < height; i++) { for (int j = 0; j < width / 8; j++) { unsigned data_byte = *data++; for (int k = 0; k < 8; k++) { // Maybe the order of bits is wrong here. // If so check with mask 0x80 and shift left. // EDIT: Black is 1 and white is zero! printf("%d,", (data_byte & 1) ? 0 : 255); data_byte >>= 1; } } if (stride) { unsigned data_byte = *data++; for (int k = 0; k < stride; k++) { // Again: Maybe the order of bits is wrong here. printf("%d,", (data_byte & 1) ? 0 : 255); data_byte >>= 1; } } } free(buffer);

解决方案

barneyman is right, you must decide which grey-value are white and which are black. No bit shifting, but a hard question

data_byte = (data_byte > 0x80) ? 0xff : 0;

"Take it to the limit" ;-)

更多推荐

将pgm格式图像转换为pbm(黑白0或1)?

本文发布于:2023-10-30 01:23:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   黑白   图像   格式   pgm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!