var a = load_asset_from_apk(path)
if a != null then
if path.file_extension == "png" then
- var png = new Opengles1Image.from_android_asset(a)
+ var png = new Opengles1Image.from_android_asset(a)
a.close
return png
else if path.file_extension == "txt" then
redef class Opengles1Image
# Read a png from a zipped stream
- new from_android_asset(asset: AndroidAsset) is extern `{
+ new from_android_asset(asset: AndroidAsset) import Int.next_pow `{
struct mnit_opengles_Texture *recv = NULL;
png_structp png_ptr = NULL;
unsigned char *pixels = NULL;
unsigned int i;
+ png_uint_32 width_pow2, height_pow2;
+ unsigned int row_bytes_pow2;
+
unsigned char sig[8];
int sig_read = AAsset_read(asset, sig, 8);
if (png_sig_cmp(sig, 0, sig_read)) {
LOGW("invalide png signature");
return NULL;
}
-
+
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
LOGW("png_create_read_struct failed");
png_read_update_info(png_ptr, info_ptr);
}
- LOGW("w: %i, h: %i", width, height);
+ width_pow2 = Int_next_pow(width, 2);
+ height_pow2 = Int_next_pow(height, 2);
+
+ LOGW("Loading image of w: %i, h: %i, w2: %d, h2: %d",
+ width, height, width_pow2, height_pow2);
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
- pixels = malloc(row_bytes * height);
- row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
+ row_bytes_pow2 = row_bytes * width_pow2 / width;
+ pixels = malloc(row_bytes_pow2 * height_pow2);
+ row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height_pow2);
for (i=0; i<height; i++)
row_pointers[i] = (png_byte*) malloc(row_bytes);
png_read_image(png_ptr, row_pointers);
for (i = 0; i < height; i++)
- memcpy(pixels + (row_bytes*i),
- row_pointers[i], row_bytes);
+ memcpy(pixels + (row_bytes_pow2*i), row_pointers[i], row_bytes);
+
+ recv = mnit_opengles_load_image((const uint_least32_t *)pixels,
+ width, height, width_pow2, height_pow2, has_alpha);
- recv = mnit_opengles_load_image((const uint_least32_t *)pixels, width, height, has_alpha);
- LOGW("OK");
+ // Calculate the size of the client-side memory allocated and freed
+ float size = ((float)row_bytes_pow2) * height_pow2/1024.0/1024.0;
+ static float total_size = 0;
+ total_size += size;
+ LOGI("Loaded OK %fmb out of %fmb", size, total_size);
close_png_ptr:
if (info_ptr != NULL)
return recv;
`}
end
+
+redef universal Int
+ # The first power of `exp` greater or equal to `self`
+ private fun next_pow(exp: Int): Int
+ do
+ var p = 0
+ while p < self do p = p*exp
+ return p
+ end
+end