X-Git-Url: http://nitlanguage.org diff --git a/lib/mnit_android/android_assets.nit b/lib/mnit_android/android_assets.nit index 2c703bb..51e1ec8 100644 --- a/lib/mnit_android/android_assets.nit +++ b/lib/mnit_android/android_assets.nit @@ -21,7 +21,7 @@ # * The Android ndk # * zlib (which is included in the Android ndk) # * libpng which must be provided by the Nit compilation framework -module android_assets +module android_assets is ldflags "-lz" import mnit import android_app @@ -35,8 +35,8 @@ in "C" `{ void mnit_android_png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { - struct AAsset *recv = png_get_io_ptr(png_ptr); - int read = AAsset_read(recv, data, length); + struct AAsset *self = png_get_io_ptr(png_ptr); + int read = AAsset_read(self, data, length); } void mnit_android_png_error_fn(png_structp png_ptr, png_const_charp error_msg) @@ -54,7 +54,7 @@ extern class AndroidAsset in "C" `{struct AAsset*`} fun read(count: Int): nullable String is extern import String.as nullable, NativeString.to_s `{ char *buffer = malloc(sizeof(char) * (count+1)); - int read = AAsset_read(recv, buffer, count); + int read = AAsset_read(self, buffer, count); if (read != count) return null_String(); else @@ -65,18 +65,18 @@ extern class AndroidAsset in "C" `{struct AAsset*`} `} fun length: Int is extern `{ - return AAsset_getLength(recv); + return AAsset_getLength(self); `} fun to_fd: Int is extern `{ off_t start; off_t length; - int fd = AAsset_openFileDescriptor(recv, &start, &length); + int fd = AAsset_openFileDescriptor(self, &start, &length); return fd; `} fun close is extern `{ - AAsset_close(recv); + AAsset_close(self); `} end @@ -86,7 +86,7 @@ redef class App 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 @@ -102,7 +102,7 @@ redef class App end protected fun load_asset_from_apk(path: String): nullable AndroidAsset is extern import String.to_cstring, AndroidAsset.as nullable, native_app_glue `{ - struct android_app *native_app_glue = App_native_app_glue(recv); + struct android_app *native_app_glue = App_native_app_glue(self); struct AAsset* a = AAssetManager_open(native_app_glue->activity->assetManager, String_to_cstring(path), AASSET_MODE_BUFFER); if (a == NULL) { @@ -118,8 +118,8 @@ end redef class Opengles1Image # Read a png from a zipped stream - new from_android_asset(asset: AndroidAsset) is extern `{ - struct mnit_opengles_Texture *recv = NULL; + new from_android_asset(asset: AndroidAsset) import Int.next_pow `{ + struct mnit_opengles_Texture *self = NULL; png_structp png_ptr = NULL; png_infop info_ptr = NULL; @@ -133,13 +133,16 @@ redef class Opengles1Image 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"); @@ -176,11 +179,16 @@ redef class Opengles1Image 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