Spirrwell
November 24th, 2011, 10:35 PM
I'm REALLY unsure on how to classify my question here. I created an SDL program that gets pixels from an input image file and writes it to an "img.pxls" as I guess "raw" data... Since I'm a bit unsure on how else to describe it, here's the code:
#include <iostream>
#include <fstream>
#include "SDL.h"
#include "SDL_image.h"
using namespace std;
SDL_Surface *IMG;
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
break;
case 2:
return *(Uint16 *)p;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;
case 4:
return *(Uint32 *)p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void WriteSurface(SDL_Surface *Surface)
{
ofstream img;
img.open ("img.pxls");
for(Sint32 y = 0; y < Surface->h; y++)
{
for(Sint32 x = 0; x < Surface->w; x++)
{
img << getpixel(Surface, x, y);
img << "\n";
}
}
img.close();
}
int main( int argc, char* args[] )
{
system("COLOR 02");
string FILENAMES;
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
printf("Please enter the location of the image you would like to extract pixels from:\n");
scanf("%s", FILENAMES.c_str());
IMG = IMG_Load(FILENAMES.c_str());
if (IMG != NULL)
{
WriteSurface(IMG);
}
if (IMG == NULL)
printf("\nImage does not exist!\n\n");
if (IMG != NULL)
printf("\nSuccessfully extracted pixels!\n\n");
system("PAUSE");
//Quit SDL
SDL_FreeSurface(IMG);
SDL_Quit();
return 0;
}
I used a simple 640 x 480 image for input and when it "extracts" the pixels, I get my img.pxls file. It contains a bunch of numbers. What I want to do is directly access the screen or video memory with a program and write these pixels to the screen for as long as the program is executed. If someone can point me in the right direction it'd be appreciated If you're wondering why in the hell would I want to do that, I'm bored. Ultimately I want to to see if this could help me in creation of an operating system by compiling a program into a .bin file that reads these pixels and prints them to screen. I'm learning Assembly and stuff, so it's just something that has caught my interest.
#include <iostream>
#include <fstream>
#include "SDL.h"
#include "SDL_image.h"
using namespace std;
SDL_Surface *IMG;
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
break;
case 2:
return *(Uint16 *)p;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;
case 4:
return *(Uint32 *)p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void WriteSurface(SDL_Surface *Surface)
{
ofstream img;
img.open ("img.pxls");
for(Sint32 y = 0; y < Surface->h; y++)
{
for(Sint32 x = 0; x < Surface->w; x++)
{
img << getpixel(Surface, x, y);
img << "\n";
}
}
img.close();
}
int main( int argc, char* args[] )
{
system("COLOR 02");
string FILENAMES;
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
printf("Please enter the location of the image you would like to extract pixels from:\n");
scanf("%s", FILENAMES.c_str());
IMG = IMG_Load(FILENAMES.c_str());
if (IMG != NULL)
{
WriteSurface(IMG);
}
if (IMG == NULL)
printf("\nImage does not exist!\n\n");
if (IMG != NULL)
printf("\nSuccessfully extracted pixels!\n\n");
system("PAUSE");
//Quit SDL
SDL_FreeSurface(IMG);
SDL_Quit();
return 0;
}
I used a simple 640 x 480 image for input and when it "extracts" the pixels, I get my img.pxls file. It contains a bunch of numbers. What I want to do is directly access the screen or video memory with a program and write these pixels to the screen for as long as the program is executed. If someone can point me in the right direction it'd be appreciated If you're wondering why in the hell would I want to do that, I'm bored. Ultimately I want to to see if this could help me in creation of an operating system by compiling a program into a .bin file that reads these pixels and prints them to screen. I'm learning Assembly and stuff, so it's just something that has caught my interest.