c++ - SDL 生成黑色图像

标签 c++ image sdl

我目前正在阅读一本关于在图像压缩中使用 SDL 的书。有一段示例代码在屏幕上转换图像,该代码似乎可以正常读取图像,因为它生成了一个正确大小的窗口,但图像是黑色的。

我试了几个例子,所有的图片都是黑色的

我附上了下面的代码

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

//A public class is the same as a 'struct'
class CImage {
public:
  unsigned char red;
  unsigned char green;
  unsigned char blue;
};


void ppm_read_comments ( FILE *fp )
{
  int c;
  while ( (c = getc ( fp ) )  == '#' ) {
    while (  getc( fp )  != '\n' )
    ;
  }
  ungetc ( c, fp );
}

class ppm_error
{
  public:
    ppm_error() {
      printf("\nIncorrect PPM format!\n");
      exit ( 1 );
    }
};


//change from (R, G, B) to (B, G, R)
void  ppm2sdl ( CImage *ibuf, int width, int height )
{
  unsigned char temp;

  for ( int i = 0; i < height; ++i ) {
    int row_offset = i * width;
    for ( int j = 0; j < width; ++j ){
      int offset =  row_offset + j;
      temp = ibuf[offset].red;
      ibuf[offset].red = ibuf[offset].blue;
      ibuf[offset].blue = temp;
    }
  }
}

int main( int argc, char* args[] )
{
  int ppmh[20], c;          //PPM header
  int width, height;            //image width and height
  SDL_Surface *screen;
  char filename[] = "C:/data/beach.ppm";

  FILE *input = fopen (filename, "rb"); //PPM file for testing read
  if ( !input ) {
    printf("\nError opening input file %s!\n", filename);
    return 1;
  }

  //read PPM input file
  ppm_read_comments ( input );      //read comments
  char temp[100];
  fscanf ( input, "%2s", temp );
  temp[3] = 0;
  if ( strncmp ( temp, "P6", 2 ) )
    throw ppm_error();
  ppm_read_comments ( input );
  fscanf ( input, "%d", &width );
  ppm_read_comments ( input );
  fscanf ( input, "%d", &height );
  ppm_read_comments ( input );
  int colorlevels;
  fscanf ( input, "%d", &colorlevels );
  printf("\n%s PPM file: ", temp );
  printf(" \n\twidth=%d\theight=%d\tcolorlevles=%d\n", width,height,colorlevels+1 );
  ppm_read_comments ( input );
  while ( ( c = getc ( input )) == '\n' );     //get rid of extra line returns
  ungetc ( c ,input );

  // May use CImage ibuf[width][height] if we do not use SDL_QUIT;
  CImage *ibuf = (CImage *) malloc ( width * height * 3 );
  fread ( ibuf,  3, width * height, input );   //read image data from file
  fclose ( input );

  //initialize video system
  if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
    fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  //ensure SDL_Quit is called when the program exits
  atexit(SDL_Quit); //if not use this, we need to
                        //  do house cleaning manually when program ends

  //set video mode of width x height with 24-bit pixels
  screen = SDL_SetVideoMode( width, height, 24, SDL_SWSURFACE);
  if ( screen == NULL ) {
        fprintf(stderr, "Unable to set %dx%d video: %s\n", width, height,          SDL_GetError());
        exit(1);
  }

  //convert PPM format (R, G, B)  to SDL format (B, G, R)
      ppm2sdl ( ibuf,  width, height );

  screen->pixels = ibuf;  //point framebuffer to data buffer

    //  ibuf needs to be dynamically allocated if SDL_QUIT is used
    printf("update rect\n"   );
    SDL_UpdateRect ( screen, 0, 0, 0, 0 );  //blit data to screen

   // SDL_Flip(screen);
   // SDL_CreateRGBSurface(screen, 0, 0, 0, 0,0,0,0);

  SDL_Delay ( 4000 );     //delay 4 seconds before exit
  printf("Displaying PPM image %s successful!\n", filename );

  //do NOT free( ibuf ) if use SDL_QUIT which does the house cleaning
  return 0;
}

最佳答案

我有一段时间没有使用 SDL,但我认为您不应该手动更改屏幕表面的像素数据,即您不应该更改 pixels SDL_Surface 您从 SDL_SetVideoMode() 获得。

您可以使用 SDL_CreateRGBSurfaceFrom() 从您从 ppm 文件中读取的像素数据创建一个表面,然后使用 SDL_BlitSurface() 将其 blit 到屏幕。

关于c++ - SDL 生成黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9158516/

相关文章:

c - 依赖 SDL 的应用程序的 Makefile 编译严重失败

SDL_SetVideoMode 与 SDL_CreateRGBSurface

c++ - 使用部分模板特化来选择基类

c++ - 简单的方法来检查istream中是否有待处理的数据

Android MediaStore 插入图片 已创建附加文件

android - 相机拍摄照片的图像格式(安卓手机)

macos - 在 OSX 上使用 Waf 编译 OpenGL/SDL 应用程序时,如何抑制 '-arch' 、 'x86_64' 标志?

c++ - 从 zip 中提取文件

c++ - 试图理解 C++ 中的哈希表代码

python matplotlib更改图像的默认 Axis