GIMP-Exported png会记住已删除的图层

时间:2016-09-27 22:01:45

标签: c image png libpng

我在GIMP中制作了一组类似的图像。对于每个新图像,我描绘了上一个图像,并在中心画了一个不同的字母,摆脱了上一个图像的字母。这就是我的图像:

enter image description here

我有一个C程序,它使用libpng加载图像,然后在OpenGL中将其显示为纹理。这就是我得到的:

enter image description here

上一张图片中的字母位于此图片的字母后面。它不再出现在GIMP中(或Atom),我只有一层。为什么会发生这种情况?

可能怀疑的代码:

int load_texture(const char* filename, GLuint* ptexture) {
  FILE* fp = fopen(filename, "rb");

  if (!fp) {
    return LOAD_TEXTURE__FILE_NOT_FOUND;
  }

  unsigned char bytes[8];

  fread(bytes, 1, 8, fp);

  bool is_png = !png_sig_cmp(bytes, 0, 8);

  if (!is_png) {
    return LOAD_TEXTURE__FILE_NOT_PNG;
  }

  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (!png_ptr) {
    return LOAD_TEXTURE__PNG_READ_STRUCT_FAILURE;
  }

  png_infop info_ptr = png_create_info_struct(png_ptr);

  if (!info_ptr) {
    png_destroy_read_struct(&png_ptr, (png_infopp)(NULL), (png_infopp)(NULL));
    return LOAD_TEXTURE__PNG_INFO_PTR_FAILURE;
  }

  png_infop end_info = png_create_info_struct(png_ptr);

  if (!end_info) {
    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)(NULL));
    return LOAD_TEXTURE__PNG_END_INFO_FAILURE;
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    return LOAD_TEXTURE__PNG_JMPBUF_FAILURE;
  }

  png_init_io(png_ptr, fp);
  png_set_sig_bytes(png_ptr, 8);
  png_read_info(png_ptr, info_ptr);

  int width = png_get_image_width(png_ptr, info_ptr);
  int height = png_get_image_height(png_ptr, info_ptr);
  png_byte color_type = png_get_color_type(png_ptr, info_ptr);
  png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
  int number_of_passes = png_set_interlace_handling(png_ptr);
  png_read_update_info(png_ptr, info_ptr);

  if (setjmp(png_jmpbuf(png_ptr))) {
    return LOAD_TEXTURE__PNG_JMPBUF_FAILURE;
  }

  png_bytep* row_pointers = (png_bytep*)(malloc(sizeof(png_bytep) * height));

  for (int y = 0; y < height; y++) {
    row_pointers[y] = (png_byte*)(malloc(png_get_rowbytes(png_ptr,info_ptr)));
  }

  png_read_image(png_ptr, row_pointers);

  unsigned char* data = malloc(height * png_get_rowbytes(png_ptr, info_ptr));

  fclose(fp);

  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);

  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      memcpy(data + (i * width * 4 + j * 4), &row_pointers[i][j * 4], 4);
    }
  }

  for (int y = 0; y < height; y++) {
    free(row_pointers[y]);
  }

  free(row_pointers);

  GLuint texture_id = 0;
  glGenTextures(1, &texture_id);
  glBindTexture(GL_TEXTURE_2D, texture_id);

  GLuint gl_color_type = glColorTypeFromPngColorType(color_type);

  glTexImage2D (
    GL_TEXTURE_2D, 0, gl_color_type, // GL_RGBA8
    width, height, 0, GL_RGBA,
    GL_UNSIGNED_BYTE, data
  );

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

  free(data);

  *ptexture = texture_id;

  return LOAD_TEXTURE__SUCCESS;
}

0 个答案:

没有答案