Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8744051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:47:00+00:00 2026-06-13T11:47:00+00:00

I am writing a simple program to convert grayscale binary (P5) to grayscale ascii

  • 0

I am writing a simple program to convert grayscale binary (P5) to grayscale ascii (P2) but am having trouble reading in the binary and converting it to int.

#include <iostream>
#include <fstream>
#include <sstream>
using namespace::std;

int usage(char* arg) {
// exit program
    cout << arg << ": Error" << endl;
    return -1;
}

int main(int argc, char* argv[]) {
    int rows, cols, size, greylevels;
    string filetype;

    // open stream in binary mode
    ifstream istr(argv[1], ios::in | ios::binary);
    if(istr.fail()) return usage(argv[1]);

    // parse header
    istr >> filetype >> rows >> cols >> greylevels;
    size = rows * cols;

    // check data
    cout << "filetype: " << filetype << endl;
    cout << "rows: " << rows << endl;
    cout << "cols: " << cols << endl;
    cout << "greylevels: " << greylevels << endl;
    cout << "size: " << size << endl;

    // parse data values
    int* data = new int[size];
    int fail_tracker = 0; // find which pixel failing on
    for(int* ptr = data; ptr < data+size; ptr++) {
        char t_ch;
        // read in binary char
        istr.read(&t_ch, sizeof(char));
        // convert to integer
        int t_data = static_cast<int>(t_ch);
        // check if legal pixel
        if(t_data < 0 || t_data > greylevels) { 
            cout << "Failed on pixel: " << fail_tracker << endl;
            cout << "Pixel value: " << t_data << endl;
            return usage(argv[1]);
        }
        // if passes add value to data array
        *ptr = t_data;
        fail_tracker++;
    }
    // close the stream
    istr.close();

    // write a new P2 binary ascii image
    ofstream ostr("greyscale_ascii_version.pgm");
    // write header
    ostr << "P2 " << rows << cols << greylevels << endl;
    // write data
    int line_ctr = 0;
    for(int* ptr = data; ptr < data+size; ptr++) {
        // print pixel value
        ostr << *ptr << " ";
        // endl every ~20 pixels for some readability
        if(++line_ctr % 20 == 0) ostr << endl;
    }
    ostr.close();

    // clean up
    delete [] data;
    return 0;
}

sample image – Pulled this from an old post. Removed the comment within the image file as I am not worried about this functionality now.

When compiled with g++ I get output:

$> ./a.out a.pgm
filetype: P5
rows: 1024
cols: 768
greylevels: 255
size: 786432
Failed on pixel: 1
Pixel value: -110
a.pgm: Error

The image is a little duck and there’s no way the pixel value can be -110…where am I going wrong?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:47:01+00:00Added an answer on June 13, 2026 at 11:47 am

    greylevels: 255

    -110 is 146 as an unsigned char. It appears you are on a platform where char is a signed type, try using unsigned char.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple program to convert text to strings. One quick question, how
I'm writing a program that does many things, but one of them is converting
Writing a simple program that will find exact duplicate files on my computer, but
I'm writing a simple program for file encryption. Mostly as an academic exercise but
I'm writing a simple program using SDL, but I'm running into a strange problem.
I am writing a simple Java program to send mail, but am getting errors.
I am writing a simple program to insert rows into a table.But when i
I'm writing a simple program for testing mouse. It compiles fine, but doesn't work.
I'm writing simple program to communicate between smart devices and I receive 11001 when
I am writing a simple program to filter out stock quotes from a given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.