Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>
GLenum mnit_opengles_error_code;
GLenum mnit_opengles_error_code;
- struct mnit_opengles_Texture *mnit_opengles_load_image( const uint_least32_t *pixels, int width, int height, int has_alpha );
+ struct mnit_opengles_Texture *mnit_opengles_load_image(
+ const uint_least32_t *pixels, int width, int height,
+ int width_pow2, int height_pow2, int has_alpha);
- struct mnit_opengles_Texture *mnit_opengles_load_image( const uint_least32_t *pixels, int width, int height, int has_alpha )
+ struct mnit_opengles_Texture *mnit_opengles_load_image(
+ const uint_least32_t *pixels, int width, int height,
+ int width_pow2, int height_pow2, int has_alpha)
{
struct mnit_opengles_Texture *image = malloc(sizeof(struct mnit_opengles_Texture));
int format = has_alpha? GL_RGBA: GL_RGB;
{
struct mnit_opengles_Texture *image = malloc(sizeof(struct mnit_opengles_Texture));
int format = has_alpha? GL_RGBA: GL_RGB;
image->src_xo = 0;
image->src_yo = 0;
image->src_xo = 0;
image->src_yo = 0;
- image->src_xi = 1.0;
- image->src_yi = 1.0;
+ image->src_xi = ((float)width)/width_pow2;
+ image->src_yi = ((float)height)/height_pow2;
if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
PRINT_ERROR("error loading image after malloc: %i", mnit_opengles_error_code);
if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
PRINT_ERROR("error loading image after malloc: %i", mnit_opengles_error_code);
PRINT_ERROR("error loading image glBindTexture: %i", mnit_opengles_error_code);
}
PRINT_ERROR("error loading image glBindTexture: %i", mnit_opengles_error_code);
}
- glTexImage2D( GL_TEXTURE_2D, 0, format, width, height,
+ glTexImage2D( GL_TEXTURE_2D, 0, format, width_pow2, height_pow2,
0, format, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
0, format, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
image->scale = recv->scale;
image->blended = recv->blended;
image->scale = recv->scale;
image->blended = recv->blended;
- image->src_xo = ((float)x)/recv->width;
- image->src_yo = ((float)y)/recv->height;
- image->src_xi = ((float)x+w)/recv->width;
- image->src_yi = ((float)y+h)/recv->height;
+ float r_dx = recv->src_xi - recv->src_xo;
+ float r_dy = recv->src_yi - recv->src_yo;
+ image->src_xo = recv->src_xo + ((float)x)/recv->width*r_dx;
+ image->src_yo = recv->src_yo + ((float)y)/recv->height*r_dy;
+ image->src_xi = recv->src_xo + ((float)x+w)/recv->width*r_dx;
+ image->src_yi = recv->src_yo + ((float)y+h)/recv->height*r_dy;
return Opengles1Image_as_Image( image );
`}
return Opengles1Image_as_Image( image );
`}
var a = load_asset_from_apk(path)
if a != null then
if path.file_extension == "png" then
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
a.close
return png
else if path.file_extension == "txt" then
redef class Opengles1Image
# Read a png from a zipped stream
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;
struct mnit_opengles_Texture *recv = NULL;
png_structp png_ptr = NULL;
unsigned char *pixels = NULL;
unsigned int i;
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;
}
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_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);
}
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);
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);
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++)
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, has_alpha);
+ recv = mnit_opengles_load_image((const uint_least32_t *)pixels,
+ width, height, width_pow2, height_pow2, has_alpha);
close_png_ptr:
if (info_ptr != NULL)
close_png_ptr:
if (info_ptr != NULL)
+
+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
redef extern class Opengles1Image
new from_sdl_image( sdl_image: SDLImage ) is extern `{
redef extern class Opengles1Image
new from_sdl_image( sdl_image: SDLImage ) is extern `{
- return mnit_opengles_load_image( sdl_image->pixels, sdl_image->w, sdl_image->h, sdl_image->format->Amask );
+ return mnit_opengles_load_image( sdl_image->pixels,
+ sdl_image->w, sdl_image->h,
+ sdl_image->w, sdl_image->h, sdl_image->format->Amask );
fprintf(stderr, "SDL failed to load image <%s>: %s\n", String_to_cstring(path), IMG_GetError());
return NULL;
} else {
fprintf(stderr, "SDL failed to load image <%s>: %s\n", String_to_cstring(path), IMG_GetError());
return NULL;
} else {
- opengles_image = mnit_opengles_load_image( sdl_image->pixels, sdl_image->w, sdl_image->h, sdl_image->format->Amask );
+ opengles_image = mnit_opengles_load_image( sdl_image->pixels,
+ sdl_image->w, sdl_image->h,
+ sdl_image->w, sdl_image->h, sdl_image->format->Amask );
SDL_FreeSurface(sdl_image);
return opengles_image;
}
SDL_FreeSurface(sdl_image);
return opengles_image;
}