All pastes #778519 Raw Edit

Raph

public text v1 · immutable
·published 2007-11-17 17:40 UTC
rendered paste body
pict2file

//gcc -Wall -g -I/usr/include/SDL -lSDL

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>


SDL_Surface* affichage;
Uint8 *monBeauPixel;
void printPixel(int x, int y);
short int b;

int main( int argc, char **argv ) {
  unsigned long int x = 0;
  unsigned long int y = 0;
  
  if (argc<=1) {
    printf("usage : \n%s picture.bmp\n",argv[0]);
    return 0;
  }

  affichage = SDL_LoadBMP(argv[1]);

  if(affichage==NULL) { printf("file sux\n"); return -1;  }
  if (affichage->format->BytesPerPixel != 1) { printf("Not8b/px\n"); return -1;  }

  while (y<=affichage->h) {
    x=0;
    while(x<=affichage->w) {
      printPixel(x, y);  
      x++;
    }
    //printf("\n"); // to draw ascii picture
    y++;
  }
  SDL_Quit();
  return 0;
}

void printPixel(int x, int y) {
  monBeauPixel=(Uint8 *)affichage->pixels + x + y * affichage->w;
  b=*monBeauPixel;
  printf("%c",b); //%57+65); // to draw ascii picture
  //  printf("%c",*monBeauPixel); //%57+65); // to draw ascii picture
  /*
  r=affichage->format->palette->colors[*monBeauPixel].r;
  g=affichage->format->palette->colors[*monBeauPixel].g;
  b=affichage->format->palette->colors[*monBeauPixel].b;
  printf("%d (%d,%d,%d)  --> (%d,%d)\n",*monBeauPixel,r,g,b,x,y);
  */

}
------------------------------------------------------------------------------------

file2pict


#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>

const int SCREEN_WIDTH = 800; 
const int SCREEN_HEIGHT = 600; 
const int SCREEN_BPP = 8;

SDL_Surface* affichage;

// pré définitions
void initSDL(void);
void attendreTouche(void);
void setPixel(int x, int y, Uint32 coul);
void actualiser(void);

int main( int argc, char **argv ) {
  if (argc<=2) {
    printf("usage : %s input_file output_file\n",argv[0]);
    return -1;
  }

  FILE *myInputStream;
  myInputStream = fopen(argv[1], "rb");
  if (myInputStream == NULL) {
    printf("Can't open %s",argv[1]);
    return -1;
  }

  char octet;
  unsigned long int x = 0;
  unsigned long int y = 0;
  initSDL();

  while (fread(&octet,sizeof(char),1,myInputStream)) {
    setPixel(x, y, octet);
    x++;
    if (x==SCREEN_WIDTH) {
      x = 0;
      y++;
      //if (y%15 == 0) printf("%x\n",octet);
      //actualiser();
      if (y >= SCREEN_HEIGHT) break;
    }

  }
  fclose(myInputStream);
  SDL_SaveBMP(affichage,argv[2]);

  //actualiser();
  attendreTouche();
  return EXIT_SUCCESS;

}


void initSDL(void)
{
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "Erreur à l'initialisation de la SDL : %s\n", SDL_GetError());
    exit(EXIT_FAILURE);
  }

  atexit(SDL_Quit);
  affichage = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

  if (affichage == NULL) {
    fprintf(stderr, "Impossible d'activer le mode graphique : %s\n", SDL_GetError());
    exit(EXIT_FAILURE);
  }

  SDL_WM_SetCaption("Rainbow Storage", NULL);
}

void attendreTouche(void)
{
  SDL_Event event;

  do
    SDL_WaitEvent(&event);
  while (event.type != SDL_QUIT && event.type != SDL_KEYUP);
}

void setPixel(int x, int y, Uint32 coul)
{
  *((Uint32*)(affichage->pixels) + x + y * affichage->w) = coul;
}

void actualiser(void)
{
  SDL_UpdateRect(affichage, 0, 0, 0, 0);
}