Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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);
Posted
Updated 10-Jun-16 1:02am
v2
Comments
barneyman 9-Jun-16 7:46am    
so, what's the problem? obviously you have to determine the threshold for B vs W
yagami_md 11-Jun-16 8:36am    
the problem is not for the threshold !! my problem is when I tried that with the library PIL on pythn and I shw the ixel I senn thati have ony 2 color in image 0 or 255 black or white , after when I go to visual studio with c language and I tried to get all pxel I seen more value fixel than 0 or 255? nota it is he same image pbm
barneyman 11-Jun-16 20:20pm    
you've asked this question before, and been given the answer

http://www.codeproject.com/Questions/1105390/Different-value-pixel-in-C-and-Python-language

in python the image is being parsed/interpreted by the image library, in C you're reading the raw bytes

1 solution

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

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


"Take it to the limit" ;-)
 
Share this answer
 
Comments
CPallini 9-Jun-16 15:32pm    
data_byte = data_byte & 0x80 ? 0xFF : 0;
looks more democratic, to me.
BTW, have my 5.
yagami_md 9-Jun-16 19:48pm    
i succed to convert the image to pbm file black and white but using python program , and this the resut of the image :
from PIL import Image
# ouverture d’une image au format pgm binaire
imageSource=Image.open("lena512.pbm")
# remarque : imageSource est un nom de variable

# sa largeur et sa hauteur en pixels :

largeur ,hauteur=imageSource. size
# ouverture d’une nv image
# pour l ’option "L", voir http://www.pythonware.com/library/pil/handbook/concepts.htm # et pour Image.new() , voir http://www.pythonware.com/library/pil/handbook/image.htm
#imageBut=Image.new("L" ,(largeur ,hauteur))
# remarque : imageBut est un nom de variable , vous pouvez mettre un autre nom à la place .

# pour chaque ligne :
for y in range(hauteur) :
#pour chaque colonne :
for x in range(largeur) :
# code du pixel (niveau de gris )
p=imageSource. getpixel ((x,y))
print (p)
# inversion du niveau de gris :

#q=255-p
# création du pixel correspondant dans la nv image :
#imageBut. putpixel ((x,y) ,q)


# sauvegarde de l ’image créée :
#imageBut.save("InversionAvecPil .pgm")
# on montre l ’image :
#imageBut.show
after i tried to do the seem work to show al pixel using c program but show different value of pixel
// show_pixel_image.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <conio.h>


void func()
{
FILE *pFile=fopen("result.pbm","r");
int c;
do {
c = fgetc (pFile);
printf("%d",c);
} while (c != EOF);
fclose(pFile);
}


int _tmain(int argc, _TCHAR* argv[])
{
func();
getch();


}

plese help me , why they are different between c and python language?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900