📄 img_xcf.c
字号:
h = (xcf_hierarchy *) malloc (sizeof (xcf_hierarchy)); h->width = SDL_ReadBE32 (src); h->height = SDL_ReadBE32 (src); h->bpp = SDL_ReadBE32 (src); h->level_file_offsets = NULL; i = 0; do { h->level_file_offsets = (Uint32 *) realloc (h->level_file_offsets, sizeof (Uint32) * (i+1)); h->level_file_offsets [i] = SDL_ReadBE32 (src); } while (h->level_file_offsets [i++]); return h;}void free_xcf_level (xcf_level * l) { free (l->tile_file_offsets); free (l);}xcf_level * read_xcf_level (SDL_RWops * src) { xcf_level * l; int i; l = (xcf_level *) malloc (sizeof (xcf_level)); l->width = SDL_ReadBE32 (src); l->height = SDL_ReadBE32 (src); l->tile_file_offsets = NULL; i = 0; do { l->tile_file_offsets = (Uint32 *) realloc (l->tile_file_offsets, sizeof (Uint32) * (i+1)); l->tile_file_offsets [i] = SDL_ReadBE32 (src); } while (l->tile_file_offsets [i++]); return l;}void free_xcf_tile (unsigned char * t) { free (t);}unsigned char * load_xcf_tile_none (SDL_RWops * src, Uint32 len, int bpp, int x, int y) { unsigned char * load; load = (char *) malloc (len); // expect this is okay SDL_RWread (src, load, len, 1); return load;}unsigned char * load_xcf_tile_rle (SDL_RWops * src, Uint32 len, int bpp, int x, int y) { unsigned char * load, * t, * data, * d; Uint32 reallen; int i, size, count, j, length; unsigned char val; t = load = (char *) malloc (len); reallen = SDL_RWread (src, t, 1, len); data = (char *) malloc (x*y*bpp); for (i = 0; i < bpp; i++) { d = data + i; size = x*y; count = 0; while (size > 0) { val = *t++; length = val; if (length >= 128) { length = 255 - (length - 1); if (length == 128) { length = (*t << 8) + t[1]; t += 2; } count += length; size -= length; while (length-- > 0) { *d = *t++; d += bpp; } } else { length += 1; if (length == 128) { length = (*t << 8) + t[1]; t += 2; } count += length; size -= length; val = *t++; for (j = 0; j < length; j++) { *d = val; d += bpp; } } } } free (load); return (data);}static Uint32 rgb2grey (Uint32 a) { Uint8 l; l = 0.2990 * ((a && 0x00FF0000) >> 16) + 0.5870 * ((a && 0x0000FF00) >> 8) + 0.1140 * ((a && 0x000000FF)); return (l << 16) | (l << 8) | l;}void create_channel_surface (SDL_Surface * surf, xcf_image_type itype, Uint32 color, Uint32 opacity) { Uint32 c = 0; switch (itype) { case IMAGE_RGB: case IMAGE_INDEXED: c = opacity | color; break; case IMAGE_GREYSCALE: c = opacity | rgb2grey (color); break; } SDL_FillRect (surf, NULL, c);}int do_layer_surface (SDL_Surface * surface, SDL_RWops * src, xcf_header * head, xcf_layer * layer, load_tile_type load_tile) { xcf_hierarchy * hierarchy; xcf_level * level; unsigned char * tile; Uint8 * p8; Uint16 * p16; Uint32 * p; int x, y, tx, ty, ox, oy, i, j; Uint32 *row; SDL_RWseek (src, layer->hierarchy_file_offset, SEEK_SET); hierarchy = read_xcf_hierarchy (src); level = NULL; for (i = 0; hierarchy->level_file_offsets [i]; i++) { SDL_RWseek (src, hierarchy->level_file_offsets [i], SEEK_SET); level = read_xcf_level (src); ty = tx = 0; for (j = 0; level->tile_file_offsets [j]; j++) { SDL_RWseek (src, level->tile_file_offsets [j], SEEK_SET); ox = tx+64 > level->width ? level->width % 64 : 64; oy = ty+64 > level->height ? level->height % 64 : 64; if (level->tile_file_offsets [j+1]) { tile = load_tile (src, level->tile_file_offsets [j+1] - level->tile_file_offsets [j], hierarchy->bpp, ox, oy); } else { tile = load_tile (src, ox*oy*6, hierarchy->bpp, ox, oy); } p8 = tile; p16 = (Uint16 *) p8; p = (Uint32 *) p8; for (y=ty; y < ty+oy; y++) { row = (Uint32 *)((Uint8 *)surface->pixels + y*surface->pitch + tx*4); switch (hierarchy->bpp) { case 4: for (x=tx; x < tx+ox; x++) *row++ = Swap32 (*p++); break; case 3: for (x=tx; x < tx+ox; x++) { *row = 0xFF000000; *row |= ((Uint32) *(p8++) << 16); *row |= ((Uint32) *(p8++) << 8); *row |= ((Uint32) *(p8++) << 0); row++; } break; case 2: // Indexed/Greyscale + Alpha switch (head->image_type) { case IMAGE_INDEXED: for (x=tx; x < tx+ox; x++) { *row = ((Uint32) (head->cm_map [*p8*3]) << 16); *row |= ((Uint32) (head->cm_map [*p8*3+1]) << 8); *row |= ((Uint32) (head->cm_map [*p8++*3+2]) << 0); *row |= ((Uint32) *p8++ << 24);; row++; } break; case IMAGE_GREYSCALE: for (x=tx; x < tx+ox; x++) { *row = ((Uint32) *p8 << 16); *row |= ((Uint32) *p8 << 8); *row |= ((Uint32) *p8++ << 0); *row |= ((Uint32) *p8++ << 24);; row++; } break; default: fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type); return 1; } break; case 1: // Indexed/Greyscale switch (head->image_type) { case IMAGE_INDEXED: for (x = tx; x < tx+ox; x++) { *row++ = 0xFF000000 | ((Uint32) (head->cm_map [*p8*3]) << 16) | ((Uint32) (head->cm_map [*p8*3+1]) << 8) | ((Uint32) (head->cm_map [*p8*3+2]) << 0); p8++; } break; case IMAGE_GREYSCALE: for (x=tx; x < tx+ox; x++) { *row++ = 0xFF000000 | (((Uint32) (*p8)) << 16) | (((Uint32) (*p8)) << 8) | (((Uint32) (*p8++)) << 0); } break; default: fprintf (stderr, "Unknown Gimp image type (%d)\n", head->image_type); return 1; } break; } } tx += 64; if (tx >= level->width) { tx = 0; ty += 64; } if (ty >= level->height) { break; } free_xcf_tile (tile); } free_xcf_level (level); } free_xcf_hierarchy (hierarchy); return 0;}SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src) { SDL_Surface *surface, *lays; xcf_header * head; xcf_layer * layer; xcf_channel ** channel; int read_error, chnls, i, offsets; Uint32 offset, fp; unsigned char * (* load_tile) (SDL_RWops *, Uint32, int, int, int); /* Check to make sure we have something to do */ if ( ! src ) { return NULL; } /* Initialize the data we will clean up when we're done */ surface = NULL; read_error = 0; head = read_xcf_header (src); switch (head->compr) { case COMPR_NONE: load_tile = load_xcf_tile_none; break; case COMPR_RLE: load_tile = load_xcf_tile_rle; break; default: fprintf (stderr, "Unsupported Compression.\n"); free_xcf_header (head); return NULL; } /* Create the surface of the appropriate type */ surface = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32, 0x00FF0000,0x0000FF00,0x000000FF,0xFF000000); if ( surface == NULL ) { IMG_SetError("Out of memory"); goto done; } head->layer_file_offsets = NULL; offsets = 0; while ((offset = SDL_ReadBE32 (src))) { head->layer_file_offsets = (Uint32 *) realloc (head->layer_file_offsets, sizeof (Uint32) * (offsets+1)); head->layer_file_offsets [offsets] = offset; offsets++; } fp = SDL_RWtell (src); lays = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32, 0x00FF0000,0x0000FF00,0x000000FF,0xFF000000); if ( lays == NULL ) { IMG_SetError("Out of memory"); goto done; } // Blit layers backwards, because Gimp saves them highest first for (i = offsets; i > 0; i--) { SDL_Rect rs, rd; SDL_RWseek (src, head->layer_file_offsets [i-1], SEEK_SET); layer = read_xcf_layer (src); do_layer_surface (lays, src, head, layer, load_tile); rs.x = 0; rs.y = 0; rs.w = layer->width; rs.h = layer->height; rd.x = layer->offset_x; rd.y = layer->offset_y; rd.w = layer->width; rd.h = layer->height; if (layer->visible) SDL_BlitSurface (lays, &rs, surface, &rd); free_xcf_layer (layer); } SDL_FreeSurface (lays); SDL_RWseek (src, fp, SEEK_SET); // read channels channel = NULL; chnls = 0; while ((offset = SDL_ReadBE32 (src))) { channel = (xcf_channel **) realloc (channel, sizeof (xcf_channel *) * (chnls+1)); fp = SDL_RWtell (src); SDL_RWseek (src, offset, SEEK_SET); channel [chnls++] = (read_xcf_channel (src)); SDL_RWseek (src, fp, SEEK_SET); } if (chnls) { SDL_Surface * chs; chs = SDL_AllocSurface(SDL_SWSURFACE, head->width, head->height, 32, 0x00FF0000,0x0000FF00,0x000000FF,0xFF000000); if (chs == NULL) { IMG_SetError("Out of memory"); goto done; } for (i = 0; i < chnls; i++) { // printf ("CNLBLT %i\n", i); if (!channel [i]->selection && channel [i]->visible) { create_channel_surface (chs, head->image_type, channel [i]->color, channel [i]->opacity); SDL_BlitSurface (chs, NULL, surface, NULL); } free_xcf_channel (channel [i]); } SDL_FreeSurface (chs); } done: free_xcf_header (head); if ( read_error ) { SDL_FreeSurface(surface); IMG_SetError("Error reading XCF data"); surface = NULL; } return(surface);}#else/* See if an image is contained in a data source */int IMG_isXCF(SDL_RWops *src){ return(0);}/* Load a XCF type image from an SDL datasource */SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src){ return(NULL);}#endif /* LOAD_XCF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -