Advertisement

SDL Program

Started by October 15, 2011 05:56 PM
0 comments, last by Wooh 13 years, 1 month ago
Hello Forum.

I have a SDL program I pieced together from other projects. All it's supposed to do is open an image, check each pixel color and place those into a text file.
It works, except when I try to place a really big image into it. Whenever I put an image that is 1000x1000px the compile says pixelcolor is a bad pointer. I have no idea what that means especially when it works on smaller file sizes.

main.h
[source]
#ifndef _MAIN_H
#define _MAIN_H

#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <string>
#include <assert.h>
#include <fstream>
#include "Surface.h"

#pragma comment( lib, "SDL.lib" )
#pragma comment( lib, "SDLmain.lib" )
#pragma comment( lib, "SDL_image.lib" )


SDL_Surface* Image;
bool Done;
std::ofstream fout;

int FirstX;
int FirstY;
int LastX;
int LastY;

Uint32 CheckPixel( SDL_Surface* Surf, int x, int y );

#endif
[/source]

main.cpp
[source]
#include "main.h"

Uint32 CheckPixel( SDL_Surface* Surf, int x, int y )
{
if(SDL_MUSTLOCK( Surf ))
SDL_LockSurface( Surf );

int bpp = Surf->format->BytesPerPixel;
/*here p is the address to the pixel we want to retrieve*/
Uint8 *p = (Uint8 *)Surf->pixels + y * Surf->pitch + x * bpp;

Uint32 pixelcolor = 0;

switch(bpp)
{
case(1):
pixelcolor = *p;
break;

case(2):
pixelcolor = *(Uint16 *)p;
break;

case(3):
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
pixelcolor = p[0] << 16 | p[1] << 8 | p[2];
else
pixelcolor = p[0] | p[1] << 8 | p[2] << 16;
break;

case(4):
pixelcolor = *(Uint32 *)p; // HERE is where the program stops then I get an unhandled exception
break;
}

if( SDL_MUSTLOCK( Surf ) )
SDL_UnlockSurface( Surf );

return pixelcolor;
}

int main( int argc, char* argv[] )
{
Done = false;
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
std::cout << "ALERT! SDL Did not Init properly!" << std::endl;
SDL_Delay( 1000 );
return -1;
}

Image = SDL_SetVideoMode( 1024, 1024, 32, SDL_HWSURFACE );

std::cout << "Opening Image" << std::endl;

std::string file = "./image/Image.jpg";

if( ( Image = Surface::Load_Image( file ) ) == NULL )
{
std::cout << "Problem loading " << file << std::endl;
printf("IMG_Load: %s\n", IMG_GetError());
SDL_Delay( 2000 );
return 1;
}

Uint32 WHITE = SDL_MapRGB( Image->format, 0xFF, 0xFF, 0xFF );
Uint32 BLUE = SDL_MapRGB( Image->format, 0xFF, 0, 0 );
Uint32 GREEN = SDL_MapRGB( Image->format, 0, 0xFF, 0 );
Uint32 RED = SDL_MapRGB( Image->format, 0xFF, 0, 0 );
Uint32 BLACK = SDL_MapRGB( Image->format, 0, 0, 0 );

SDL_SetColorKey( Image, SDL_SRCCOLORKEY, WHITE );

fout.open( "./data/2D_Array.txt", std::ios::beg );

while( !Done )
{
for( int y = 0; y <= Image->h; y++ )
{
for( int x = 0; x <= Image->w; x++ )
{
if( CheckPixel( Image, x, y ) == WHITE )
{
fout << "X: " << x << " Y: " << y << " 0" << std::endl;
}

else if( CheckPixel( Image, x, y ) == BLACK )
{
fout << "X: " << x << " Y: " << y << " 1" << std::endl;
}

else if( CheckPixel( Image, x, y ) == GREEN )
{
fout << "X: " << x << " Y: " << y << " 2" << std::endl;
}

else if( CheckPixel( Image, x, y ) == RED )
{
fout << "X: " << x << " Y: " << y << " 3" << std::endl;
}

else if( CheckPixel( Image, x, y ) == BLUE )
{
fout << "X: " << x << " Y: " << y << " 4" << std::endl;
}
}
}

fout.close();
SDL_FreeSurface( Image );
Done = true;
}

SDL_Quit();
return 0;
}
[/source]

Surface.h
[source]
#ifndef _SURFACE_H
#define _SURFACE_H

#include <SDL.h>
#include <SDL_image.h>
#include <string>
#include <iostream>

class Surface
{
public:
Surface();

static SDL_Surface *Load_Image(std::string file);
static bool Draw(SDL_Surface *Source, SDL_Surface *Destination, int X, int Y);
static bool Draw(SDL_Surface *Source, SDL_Surface *Destination, int X, int Y,
int clipX, int clipY, int width, int height);
bool Transparent(SDL_Surface* Surf_Dest, int R, int G, int B);
static bool Draw(SDL_Surface *Source, SDL_Surface *Destination, SDL_Rect *AniMatrix, int PosX, int PosY);

};

#endif

[/soure]

Surface.cpp
[source]
#include "Surface.h"

Surface::Surface()
{
}

SDL_Surface *Surface::Load_Image(std::string file)
{
SDL_Surface *Surf_Temp = NULL;
SDL_Surface *Surf_Return = NULL;

Surf_Temp = IMG_Load( file.c_str() );

if( Surf_Temp == NULL)
{
return NULL;
}

Surf_Return = SDL_DisplayFormatAlpha(Surf_Temp);

SDL_FreeSurface(Surf_Temp);

return Surf_Return;
}

bool Surface::Draw(SDL_Surface *Source, SDL_Surface *Destination, int X, int Y)
{
if(Source == NULL || Destination == NULL)
{
return false;
}

SDL_Rect DestR;

DestR.x = X;
DestR.y = Y;

SDL_BlitSurface(Source, NULL, Destination, &DestR);

return true;
}

bool Surface::Draw(SDL_Surface *Source, SDL_Surface *Destination,
int clipX, int clipY, int X, int Y, int width, int height)
{
if(Source == NULL || Destination == NULL)
{
return false;
}

SDL_Rect DestR;

DestR.x = X;
DestR.y = Y;

SDL_Rect SourceR;

SourceR.x = clipX;
SourceR.y = clipY;
SourceR.w = width;
SourceR.h = height;

SDL_BlitSurface(Source, &SourceR, Destination, &DestR);
return true;
}

bool Surface::Draw(SDL_Surface *Source, SDL_Surface *Destination, SDL_Rect *AniMatrix, int PosX, int PosY)
{
SDL_Rect DestR;

DestR.x = PosX;
DestR.y = PosY;

SDL_BlitSurface(Source, AniMatrix, Destination, &DestR);
return true;
}

[/source]
you are looping outside the image. Change y <= Image->h to y < Image->h and do the the same with x.

This topic is closed to new replies.

Advertisement