From: Dominic L. <ma...@us...> - 2004-06-13 19:18:39
|
Update of /cvsroot/robotflow/RobotFlow/Probes/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15779 Modified Files: ImageProbeSDL.cc Log Message: using SDL image properly with different pixel sizes Index: ImageProbeSDL.cc =================================================================== RCS file: /cvsroot/robotflow/RobotFlow/Probes/src/ImageProbeSDL.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ImageProbeSDL.cc 20 Jun 2003 00:45:42 -0000 1.1 --- ImageProbeSDL.cc 13 Jun 2004 19:18:30 -0000 1.2 *************** *** 95,99 **** //SDL initialize ! if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) { char message[256]; sprintf(message, "Impossible d'initialiser SDL: %s\n", SDL_GetError()); --- 95,99 ---- //SDL initialize ! if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { char message[256]; sprintf(message, "Impossible d'initialiser SDL: %s\n", SDL_GetError()); *************** *** 102,187 **** atexit(SDL_Quit); - - m_screen = SDL_SetVideoMode(m_width, m_height, 15, SDL_SWSURFACE); if ( m_screen == NULL ) { char message[256]; ! sprintf(message, "Impossible de passer en 640x480 en 16 bpp: %s\n", SDL_GetError()); throw new GeneralException(message,__FILE__,__LINE__); } - - - } ! void DrawPixel(SDL_Surface *screen, int x, int y,Uint8 R, Uint8 G, Uint8 B) { ! ! Uint32 color = SDL_MapRGB(screen->format, R, G, B); ! ! if ( SDL_MUSTLOCK(screen) ) { ! if ( SDL_LockSurface(screen) < 0 ) { ! return; ! } } ! switch (screen->format->BytesPerPixel) { ! case 1: { /*On gère le mode 8bpp */ ! Uint8 *bufp; ! bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; ! *bufp = color; ! } ! break; ! case 2: { /* Certainement 15 ou 16 bpp */ ! Uint16 *bufp; ! bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; ! *bufp = color; ! } ! break; ! case 3: { /* 24 bpp lent et généralement pas utilisé */ ! Uint8 *bufp; ! bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; ! if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { ! bufp[0] = color; ! bufp[1] = color >> 8; ! bufp[2] = color >> 16; ! } else { ! bufp[2] = color; ! bufp[1] = color >> 8; ! bufp[0] = color >> 16; ! } ! } ! break; ! case 4: { /* Probablement 32 bpp alors */ ! Uint32 *bufp; ! bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; ! *bufp = color; ! } ! break; ! } ! if ( SDL_MUSTLOCK(screen) ) { ! SDL_UnlockSurface(screen); ! } ! SDL_UpdateRect(screen, x, y, 1, 1); ! } ! void display_image(Image &image) { ! if ( SDL_MUSTLOCK(m_screen) ) { ! if ( SDL_LockSurface(m_screen) < 0 ) { ! return; ! } } - //cerr<<"update"<<endl; - memcpy(m_screen->pixels,image.get_data(),image.get_size()); - - if ( SDL_MUSTLOCK(m_screen) ) { SDL_UnlockSurface(m_screen); --- 102,170 ---- atexit(SDL_Quit); + m_screen = SDL_SetVideoMode(m_width, m_height, 32, SDL_SWSURFACE); if ( m_screen == NULL ) { char message[256]; ! sprintf(message, "SDL init error using 32 bpp: %s\n", SDL_GetError()); throw new GeneralException(message,__FILE__,__LINE__); } } + void display_image(Image &image) { ! if ( SDL_MUSTLOCK(m_screen) ) { ! if ( SDL_LockSurface(m_screen) < 0 ) { ! return; ! } } ! ! //cerr<<"update"<<endl; ! //display image depending of pixel format ! //memcpy(m_screen->pixels,image.get_data(),image.get_size()); ! Uint32 color; ! unsigned int* display_ptr = (unsigned int*) m_screen->pixels; ! unsigned char* image_ptr = image.get_data(); ! unsigned short temp; ! for (int i = 0; i < image.get_width() * image.get_height(); i++) { ! unsigned int R,G,B; ! switch(image.get_pixelsize()) { ! ! case 1: ! R = G = B = *image_ptr; ! break; ! ! case 2: ! temp = (*(unsigned short*)(image_ptr)); ! R = (unsigned char) ((temp >> 7) & 0xF1); ! G = (unsigned char) ((temp >> 2) & 0xF1); ! B = (unsigned char)(temp & 0xF1) << 3; ! break; ! ! case 3: ! R = image_ptr[0]; ! G = image_ptr[1]; ! B = image_ptr[2]; ! break; ! default: ! throw new GeneralException("Unknown image pixelsize",__FILE__,__LINE__); ! break; ! ! } ! color = SDL_MapRGB(m_screen->format, R, G, B); ! //increment SDL image pointer ! *display_ptr++ = color; ! //increment Image pointer ! image_ptr += image.get_pixelsize(); } if ( SDL_MUSTLOCK(m_screen) ) { SDL_UnlockSurface(m_screen); |