[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to display a PGM image in Visual C++

Posted on 1998-11-10
3
Medium Priority
?
2,230 Views
Last Modified: 2008-02-01
I have a problem of diplaying PGM format image under Visual C++ program. wondering how to make my program display them?
0
Comment
Question by:nigelcct
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Expert Comment

by:euclid091498
ID: 1177451
nigelcct:
Where exactly are you having a problem?

Is it the pgm file format?
If so have a look at:
  http://www.wotsit.org/
You should find there a description of the pgm file format (search for pgm.zip).

Or is it Windows related?

Regards.
0
 

Author Comment

by:nigelcct
ID: 1177452
Yes , it is the pgm file (image). But the web site only show the format of the file but never show how i can display in my Visual C++ ver 5 program. I wish to the image file in one of the window when I click a button.

thanks for you info but wonder if you can help me further?
thanks
0
 
LVL 1

Accepted Solution

by:
payn earned 200 total points
ID: 1177453
There are two separate problems involved in displaying new file formats in Windows: Reading the file, and displaying the picture.

First, for reading and parsing a PGM file, there's plenty of open-source code out there to handle that part. Look in the various Unix image viewers (xv at ftp.cis.upenn.edu/pub/xv/xv-3.10a.tar.gz), xli at ftp.cs.columbia.edu/archives/X11R6/contrib/applications/xli.1.16.tar.gz, etc.), or in the IJG JPEG tools distribution at ftp.uu.net/graphics/jpeg/jpegsrc.v6a.tar.gz.

Actually, PGM is so simple you might want to write your own parsing code. Here's some pseudocode. This assumes you have a routine GetNextWord() that skips any whitespace and comment lines, then returns a CString holding the next characters from the PGM file up to but not including the first whitespace, and a routine GetNextByte() that just returns the next byte no matter what it is.

struct GreyMap {
  char** map;
  int nWidth, nHeight;
};

GreyMap ReadPGM(const char* szFilename)
{
  GreyMap gm = { NULL, 0, 0 };
  bool bRaw = false;

  // The idea of PrepareFile is that it opens the file and sets
  // up GetNextWord to deal with it.
  if (!PrepareFile(szFilename)) error;

  // Read the header
  CString strFormat = GetNextWord();
  if (strFormat == "P5") bRaw = true;
  else if (GetNextWord() != "P2") error;
  gm.nWidth = atoi(GetNextWord());
  gm.nHeight = atoi(GetNextWord());
  int nMaxValue = atoi(GetNextWord());
 
  // ReadPGM32 and ReadPGM16 are very similar, but they read 16-
  // and 32-bit PGMs. You can decide whether to keep this data,
  // or throw it away (by multiplying each read value by
  // nMaxValue/255, or just right-shifting away extra bits).
  // Also note that raw PGMs (P5 format) cannot be greater than
  // 8 bits.
  if (nMaxValue >= 2<<16) return ReadPGM32(szFilename)
  if (nMaxValue >= 2<<8) return ReadPGM16(szFilename)
  gm.map = new unsigned char[nWidth][nHeight];
 
  // Read in the pixel data
  if (bRaw)
    for (int y = 0; y != gm.nHeight; y++)
      for (int x = 0; x != gm.nWidth; x++)
        gm.map[x][y] = GetNextByte();
    for (int y = 0; y != gm.nHeight; y++)
      for (int x = 0; x != gm.nWidth; x++)
        gm.map[x][y] = atoi(GetNextWord());

  // Clean up and return
  CloseFile(szFilename);
  return gm;
}

The next step is displaying your image. Basically, you're going to wait to stick your raw data into a Windows bitmap and just draw that bitmap. Look at Julian Smart's CImage library (available on CodeGuru, www.codeguru.com) for a good idea of how to do this, or ask here for more help.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question