int png_to_img(const char * filename, uint32 mask, kos_img_t * rv) { uint16 *temp_tex; /* More stuff */ uint8 *buffer; /* Output row buffer */ uint32 row_stride; /* physical row width in output buffer */ uint32 channels; /* 3 for RGB 4 for RGBA */ FILE *infile; /* source file */ void *strs; /* internal structs */ assert( rv != NULL ); if ((infile = fopen(filename, "r")) == 0) { dbglog(DBG_ERROR, "png_to_texture: can't open %s\n", filename); return -1; } /* Step 1: Initialize loader */ strs = readpng_init(infile); if (!strs) { fclose(infile); return -2; } /* Step 1.5: Create output kos_img_t */ /* rv = (kos_img_t *)malloc(sizeof(kos_img_t)); */ /* Step 2: Read file */ buffer = readpng_get_image(strs,&channels, &row_stride, &rv->w, &rv->h); temp_tex = (uint16 *)malloc(sizeof(uint16) * rv->w * rv->h); rv->data = (void *)temp_tex; rv->byte_count = rv->w * rv->h * 2; _png_copy_texture(buffer, temp_tex, channels, row_stride, mask, rv->w, rv->h); switch (mask) { case PNG_NO_ALPHA: rv->fmt = KOS_IMG_FMT(KOS_IMG_FMT_RGB565, 0); break; case PNG_MASK_ALPHA: rv->fmt = KOS_IMG_FMT(KOS_IMG_FMT_ARGB1555, 0); break; case PNG_FULL_ALPHA: rv->fmt = KOS_IMG_FMT(KOS_IMG_FMT_ARGB4444, 0); break; } /* Step 3: Finish decompression */ free(buffer); readpng_cleanup(strs); fclose(infile); /* And we're done! */ return 0; }
int img_load_data(FILE *f, IMG_INFO *info, kos_img_t *img) { uint32 channels, rowBytes; uint8 *data = NULL; uint8 allocate = 0; if (info == NULL) { allocate = 1; info = (IMG_INFO *)malloc(sizeof(IMG_INFO)); memset(&info,0,sizeof(IMG_INFO)); } switch(info->type) { case IMG_FILE_GUESS: return -1; break; case IMG_FILE_JPEG: { readjpeg_init(f); data = readjpeg_get_image(&channels, &rowBytes, &img->w, &img->h); readjpeg_cleanup(); break; } case IMG_FILE_PNG: { readpng_init(f); data = readpng_get_image(&channels, &rowBytes, &img->w, &img->h); readpng_cleanup(); break; } case IMG_FILE_BMP: { readbmp_init(f); data = readbmp_get_image(&channels, &rowBytes, &img->w, &img->h); readbmp_cleanup(); break; } case IMG_FILE_PCX: { readpcx_init(f); data = readpcx_get_image(&channels, &rowBytes, &img->w, &img->h); readpcx_cleanup(); break; } } if (info->dither_width == 0) info->dither_width = img->w; if (info->dither_height == 0) info->dither_height = img->h; img->data = (uint16 *)malloc(sizeof(uint16)*img->w*img->h); img->byte_count = sizeof(uint16)*img->w*img->h; img_copy_texture(img->data, data, channels, rowBytes, info, img->w, img->h); free(data); if (allocate) { free(info); } switch(info->alpha) { case IMG_ALPHA_NONE: img->fmt = KOS_IMG_FMT(KOS_IMG_FMT_RGB565, 0); break; case IMG_ALPHA_MASK: img->fmt = KOS_IMG_FMT(KOS_IMG_FMT_ARGB1555, 0); break; case IMG_ALPHA_KEYED: img->fmt = KOS_IMG_FMT(KOS_IMG_FMT_ARGB1555, 0); break; case IMG_ALPHA_FULL: img->fmt = KOS_IMG_FMT(KOS_IMG_FMT_ARGB4444, 0); break; } return 0; }