cmap[3*i] = surface->format->palette->colors[i].b;
cmap[3*i] = surface->format->palette->colors[i].r;
static bool save_image(SDL_Surface *surface, SDL_RWops *dst){  Uint8 *cmap = 0;  Uint8 *pixels;  Uint8 tmp;  int offset_x, offset_y, width, height, pitch;  int colors, i;  PCX_Header header;  width = surface->w;  height = surface->h;  pixels = (Uint8*)surface->pixels;  pitch = surface->pitch;  header.manufacturer = 0x0a;  header.version = 5;  header.compression = 1;  if (surface->format->palette && surface->format->BitsPerPixel == 8) {    colors = surface->format->palette->ncolors;    cmap = (Uint8*)malloc(3*colors);    for (i = 0; i < colors; i++) {      cmap[3*i] = surface->format->palette->colors[i].b;      cmap[3*i+1] = surface->format->palette->colors[i].g;      cmap[3*i+2] = surface->format->palette->colors[i].r;    }    header.bpp = 8;    header.bytesperline = htoqs (width);    header.planes = 1;    header.color = htoqs (1);  } else if (surface->format->BitsPerPixel == 24) {    header.bpp = 8;    header.bytesperline = htoqs (width);    header.planes = 3;    header.color = htoqs(1);  } else {    return false;  }    header.x1 = 0;  header.y1 = 0;  header.x2 = htoqs (width - 1);  header.y2 = htoqs (height - 1);  header.hdpi = htoqs (300);  header.vdpi = htoqs (300);  header.reserved = 0;  /* write header */  /*  fp_offset = SDL_RWtell(dst);*/  SDL_RWwrite(dst, &header, sizeof(PCX_Header), 1);  if (cmap) {    save_8 (dst, width, height, pitch, pixels);    /* write palette */    tmp = 0x0c;    SDL_RWwrite(dst, &tmp, 1, 1);    SDL_RWwrite(dst, cmap, 3, colors);    /* fill unused colors */    tmp = 0;    for (i = colors; i < 256; i++) {      SDL_RWwrite(dst, &tmp, 1, 1);      SDL_RWwrite(dst, &tmp, 1, 1);      SDL_RWwrite(dst, &tmp, 1, 1);    }    free(cmap);  } else {    save_24 (dst, width, height, pitch, pixels);  }  return true;}