Mail Archives: djgpp/1997/07/21/09:46:27
This is a multi-part message in MIME format.
--------------392776BA1959
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
John M. Aldrich wrote:
>
> Isaac Waldron wrote:
> >
> > I am trying to dynamically allocate memory for a buffer. I wrote a
> > header file that loads and displays pcx files. I need to use a buffer
> > for the decompressed image.
> >
> [code snipped]
> > It always exits with a "Not enough memory" error.
> >
> > Any help would be appreciated.
>
> Please try to post the exact code that you use. You may have made some
> error that doesn't show up in your example. But here are some things to
> check:
>
> - Is 'bufsize' initialized?
> - Do you in fact have enough free memory? (run 'go32-v2' without
> arguments)
> - Does replacing 'bufsize' in the call to malloc() with a constant value
> like 10 work?
> - Have you #included <stdlib.h>, which declares the prototype for
> malloc()?
> - If you compile with the '-O' and '-Wall' switches, do you get any
> warnings?
>
> hth
>
> --
> John M. Aldrich <fighteer AT cs DOT com>
>
> * Anything that happens, happens.
> * Anything that, in happening, causes something else to happen,
> causes something else to happen.
> * Anything that, in happening, causes itself to happen again, happens
> again.
> * It doesn't necessarily do it in chronological order, though.
>
> --- Douglas Adams
Ok, i'll attach the code. It won't be very good, definately unoptimized,
but that's ok. BTW, bufsize was initialized, I have plenty of memory,
stdlib.h was included, and i'll have to try the -Wall switch.
--
-Begin Signature-
Isaac Waldron <waldroni AT lr DOT net> N1YZI
http://www.geocities.com/SiliconValley/Lakes/5703/home.html
-End Signature-
--------------392776BA1959
Content-Type: text/plain; charset=us-ascii; name="Pcx.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Pcx.h"
/*
pcx.h
A C++ header of functions for loading and display of .pcx pictures
Author: Isaac Waldron
*/
#define __pcx
#ifndef __mode13
#include "mode13.h"
#endif
#include <stdlib.h>
#include <iostream.h>
#include <memory.h>
#include <stdio.h>
#include <dos.h>
struct PCXHeader
{
char manufacturer;
char version;
char encoding;
char bpp;
int xStart, yStart;
int xEnd, yEnd;
int xRes, yRes;
char egaPal[48];
char reserved;
char numPlanes;
int bpl;
int pType;
char padding[58];
};
class PCXImage
{
public:
PCXImage(char *filename, short int newX, short int newY)
{
FILE *fh;
unsigned int bufsize, datatotal, dataindex;
unsigned char data, runlength;
fh = fopen(filename, "rb");
if (fh == NULL)
{
SetText();
cout << "The file could not be found.";
exit(EXIT_FAILURE);
};
fread(&header, sizeof(PCXHeader), 1, fh);
if (header.manufacturer != 10)
{
SetText();
cout << "The file is not a valid PCX file.";
exit(EXIT_FAILURE);
};
height = header.yEnd - header.yStart + 1;
width = header.xEnd - header.xStart + 1;
bufsize = width * height;
imagebuffer = malloc(bufsize);
if (imagebuffer == NULL)
{
SetText();
cout << "Not enough memory.";
exit(EXIT_FAILURE);
};
fseek(fh, 128, SEEK_SET);
datatotal = bufsize;
dataindex = 0;
while (dataindex < datatotal)
{
data = getc(fh);
if (data > 192)
{
runlength = data - 192;
data = getc(fh);
while (runlength-- > 0)
{
imagebuffer[dataindex++] = data;
};
}
else
{
imagebuffer[dataindex++] = data;
};
};
fseek(fh, -768, SEEK_END);
for (dataindex = 0; dataindex < 768; dataindex++)
{
palette[dataindex] = getc(fh) >> 2;
};
fclose(fh);
x = newX;
y = newY;
};
void PCXDraw()
{
unsigned int bufsize, windex, hindex, dest;
unsigned char *source = imagebuffer;
bufsize = width * height;
dest = x + (y * 320);
for (hindex = 0; hindex < height; hindex++)
{
for (windex = 0; windex < width; windex++, source++)
{
_farpokeb(VideoRAM, dest + windex, source[0]);
};
dest += 320;
};
};
~PCXImage()
{
free(imagebuffer);
};
protected:
struct PCXHeader header;
unsigned char *imagebuffer;
unsigned char palette[768];
unsigned char palOrig[768];
int height, width, x, y;
};
--------------392776BA1959--
- Raw text -