将pgm格式图像转换为pbm(黑白0或1)? [英] Convert pgm format image to pbm (black and white 0 or 1)?

查看:111
本文介绍了将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 =(< span class =code-keyword> 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 ++)
{
// < span class =code-comment>这里的位顺序可能是错误的。
// 如果是这样,请检查掩码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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆