Tuesday, January 5, 2010

I have written a app for playing RGB(raw) data using SDL lib. This app will work on linux based system and tested on Ubuntu. Looking forward for your valuable comments and any bug within it:


/*********************************************************************************
Filename : rgbplayer.c
Desc             : This file use SDL library to display the RGB(565) data. Its a
                      opensource file

Dependency  : Intented to run on LINUX/UNIX based system. It needs SDL.h
                      (which is generally available at /usr/include/SDL). if not available
                      please download SDL library from
                      http://sdl.beuc.net/sdl.wiki/FrontPage Documentation for SDL
                      APIs are also available there.
Usage : rgbplayer
Building : Makefile is provided with the source code.
$make - will build the binary "rgbplayer",
      copy the binary to ~/bin/ and change its mode to executable
and delete binary from present directory.
$make clean - will delete the binary from ~/bin directory.
Notes    : default frame rate is set to 10 FPS can be changed accordingly
by changing LINE 105 - SDL_Delay(100);
Author : EteRniTy aka Tirtha Kanti Ghosh (gtirtha@gmail.com)
Date : 09/11/2009
**********************************************************************************/

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>


#include "/usr/include/SDL/SDL.h"
#define RMASK 0
#define GMASK 0
#define BMASK 0
#define AMASK 0
int main(int argc, char *argv[])
{
SDL_Surface *screen; //This pointer will reference the drawing screen
SDL_Surface *image; //This pointer will reference our bitmap sprite
SDL_Surface *temp; //This pointer will temporarily reference the data (checker board)
SDL_Rect dest; //The destination region of our blit
Uint32 width, height;
Uint8 bpp = 16;
char filename[20];
char *data;
FILE *fp
if(argc != 4)
{
printf("\nUsage: rgbplayer width height \n");
exit(1);
}
width = atoi(argv[1]);
height = atoi(argv[2]);
strcpy(filename, (const char*)argv[3]);
data = (char *)malloc(width*height*2);
printf("\n%s file contains RGB data with resolution %dx%d 16bpp\n", filename, width, height);
//We must first initialize the SDL video component, and check for success
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//When this program exits, SDL_Quit must be called
atexit(SDL_Quit);
//Set the video mode to 640x480 with 16bit colour
screen = SDL_SetVideoMode(640, 480, bpp, SDL_ANYFORMAT);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
fp = fopen(filename, "rb");
while((fread(data,1, width*height*2, fp)) != 0)
{
// Create the temp surface from the raw RGB data
temp = SDL_CreateRGBSurfaceFrom(data, width, height, bpp,
width*2, RMASK, GMASK, BMASK, AMASK);
if (temp == NULL) {
printf("Unable to load checkImage: %s\n", SDL_GetError());
return 1;
}
//Convert the surface to the appropriate display format
image = SDL_DisplayFormat(temp);
//Release the temporary surface
SDL_FreeSurface(temp);
//Construct the destination rectangle for our blit
dest.x = 100; //Display the image at the (X,Y) coordinates (100,100)
dest.y = 100;
dest.w = image->w;
dest.h = image->h;
//Blit the image to the screen. Important will actually shows the surface on the screen
SDL_BlitSurface(image, NULL, screen, &dest);
//Update the part of the screen that changed
SDL_UpdateRects(screen, 1, &dest);
//Wait for 100ms (~10 FPS) so we can see the image
SDL_Delay(100);
//Release the surface
SDL_FreeSurface(image);
}
return 0;
}

The make file is as follows:


# mAkE fIlE for RgBplAyer
# Tirtha Kanti Ghosh
OPTFLAGS = -Wall
SDL_LIBS = `sdl-config --static-libs`
SDL_CFLAGS = `sdl-config --cflags`
CFLAGS = $(OPTFLAGS) $(SDL_CFLAGS)
LDFLAGS = $(SDL_LIBS)
CSRC = rgbplayer.c
TARGET = rgbplayer
OBJ = $(CSRC:.c=.o)
default: $(TARGET)
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

$(TARGET): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)
cp rgbplayer ~/bin/
chmod 777 ~/bin/rgbplayer
clean:
rm $(OBJ) $(TARGET)
rm ~/bin/rgbplayer

No comments: