Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleanup imagelib, format auto-detection, global helper function

  • Loading branch information...
commit 3312ecc685b7f87eec3944a38b41098e3f9a52ad 1 parent 479609d
@thp authored
View
2  apkenv.c
@@ -44,6 +44,7 @@
#include "jni/shlib.h"
#include "apklib/apklib.h"
#include "apklib/keycodes.h"
+#include "imagelib/imagelib_priv.h"
#include "debug/debug.h"
#include "compat/gles_wrappers.h"
#include "compat/gles2_wrappers.h"
@@ -377,6 +378,7 @@ int main(int argc, char **argv)
global.read_file_to_jni_array = read_file_to_jni_array_impl;
global.recursive_mkdir = recursive_mkdir;
global.lookup_resource = lookup_resource_impl;
+ global.image_loader = imagelib_load_from_mem;
global.module_hacks = &global_module_hacks;
hooks_init();
View
2  apkenv.h
@@ -32,6 +32,7 @@
#include "apklib/apklib.h"
#include "jni/jni.h"
+#include "imagelib/imagelib.h"
#define APKENV_NAME "apkenv"
#define APKENV_VERSION "42.4.0"
@@ -152,6 +153,7 @@ struct GlobalState {
read_file_to_jni_array_t read_file_to_jni_array;
recursive_mkdir_t recursive_mkdir;
lookup_resource_t lookup_resource;
+ image_loader_t image_loader;
};
#define VM(global_ptr) (&((global_ptr)->vm))
View
68 imagelib/imagelib.c
@@ -0,0 +1,68 @@
+
+/**
+ * apkenv
+ * Copyright (c) 2012, 2013, Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+
+#include "imagelib.h"
+#include "imagelib_priv.h"
+
+#include <stdio.h>
+#include <string.h>
+
+
+image_t *
+imagelib_load_from_disk(const char *filename, const imageloadersettings_t settings)
+{
+ FILE *fp = fopen(filename,"rb");
+ if (!fp) {
+ return NULL;
+ }
+
+ fseek(fp,0,SEEK_END);
+ size_t size = ftell(fp);
+ fseek(fp,0,SEEK_SET);
+
+ char* buf = malloc(sizeof(char)*size);
+ fread(buf,size,1,fp);
+ fclose(fp);
+
+ return imagelib_load_from_mem(buf, size, settings);
+}
+
+image_t *
+imagelib_load_from_mem(char *buf, size_t size, const imageloadersettings_t settings)
+{
+ const unsigned char png_magic[] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
+
+ if (memcmp(buf, png_magic, sizeof(png_magic)) == 0) {
+ return imagelib_load_png_from_mem(buf, size, settings);
+ } else {
+ return imagelib_load_jpeg_from_mem(buf, size, settings);
+ }
+}
View
25 imagelib/imagelib.h
@@ -1,19 +1,17 @@
-#ifndef IMAGELIB_H
-#define IMAGELIB_H
+#ifndef APKENV_IMAGELIB_H
+#define APKENV_IMAGELIB_H
/// image lib, (c) crow_riot 2013
#include <stddef.h>
-
typedef struct {
- unsigned char* data;
+ unsigned char *data;
int width;
int height;
int bpp;
} image_t;
-
typedef struct {
/// flip horizontally
int swapy;
@@ -26,19 +24,6 @@ typedef struct {
unsigned char alpha;
} imageloadersettings_t;
+typedef image_t *(*image_loader_t)(char *buf, size_t size, const imageloadersettings_t settings);
-/// load png from disk
-image_t* loadpng_disk(const char *filename, const imageloadersettings_t settings);
-
-/// load png from memory
-image_t* loadpng_mem(char* buf, size_t size, const imageloadersettings_t settings);
-
-/// load jpeg from disk
-/// please not that only rgb jpeg images are suported by the loader, no color indexed ones!
-image_t* loadjpeg_disk(const char *filename, const imageloadersettings_t settings);
-
-/// load jpeg from memory
-image_t* loadjpeg_mem(char* buf, size_t size, const imageloadersettings_t settings);
-
-
-#endif
+#endif /* APKENV_IMAGELIB_H */
View
43 imagelib/imagelib_priv.h
@@ -0,0 +1,43 @@
+
+/**
+ * apkenv
+ * Copyright (c) 2012, 2013, Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+
+#ifndef APKENV_IMAGELIB_PRIV_H
+#define APKENV_IMAGELIB_PRIV_H
+
+#include "imagelib.h"
+
+image_t *imagelib_load_from_disk(const char *filename, const imageloadersettings_t settings);
+image_t *imagelib_load_from_mem(char *buf, size_t size, const imageloadersettings_t settings);
+
+image_t *imagelib_load_png_from_mem(char* buf, size_t size, const imageloadersettings_t settings);
+image_t *imagelib_load_jpeg_from_mem(char* buf, size_t size, const imageloadersettings_t settings);
+
+#endif /* APKENV_IMAGELIB_PRIV_H */
View
24 imagelib/loadjpeg.c
@@ -1,8 +1,8 @@
/// jpeg loader, (C) crow_riot 2013
-
-
#include "imagelib.h"
+#include "imagelib_priv.h"
+
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
@@ -50,7 +50,7 @@ static void jpeg_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
src->next_input_byte = (JOCTET*)buffer;
}
-image_t* loadjpeg_mem(char* buf, size_t size, const imageloadersettings_t settings)
+image_t* imagelib_load_jpeg_from_mem(char* buf, size_t size, const imageloadersettings_t settings)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
@@ -127,21 +127,3 @@ image_t* loadjpeg_mem(char* buf, size_t size, const imageloadersettings_t settin
return image;
}
-
-
-image_t* loadjpeg_disk(const char *filename, const imageloadersettings_t settings)
-{
- FILE *fp = fopen(filename,"rb");
- if (!fp)
- return NULL;
-
- fseek(fp,0,SEEK_END);
- size_t size = ftell(fp);
- fseek(fp,0,SEEK_SET);
-
- char* buf = malloc(sizeof(char)*size);
- fread(buf,size,1,fp);
- fclose(fp);
-
- return loadjpeg_mem(buf,size,settings);
-}
View
28 imagelib/loadpng.c
@@ -1,9 +1,11 @@
/// png loader, (c) crow_riot 2013
+#include "imagelib.h"
+#include "imagelib_priv.h"
+
#include <stdio.h>
#include <libpng12/png.h>
#include <stdlib.h>
-#include "imagelib.h"
typedef struct
{
@@ -29,7 +31,7 @@ static void png_read_data(png_structp png_ptr, png_bytep outbuffer, png_size_t b
}
-image_t* loadpng_mem( char* buffer, size_t size, const imageloadersettings_t settings )
+image_t* imagelib_load_png_from_mem( char* buffer, size_t size, const imageloadersettings_t settings )
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if ( !png_ptr ) {
@@ -164,25 +166,3 @@ image_t* loadpng_mem( char* buffer, size_t size, const imageloadersettings_t set
return image;
}
-
-
-image_t* loadpng_disk( const char *filepath, const imageloadersettings_t settings)
-{
- FILE *fp = fopen(filepath,"rb");
- if (!fp)
- return NULL;
-
- fseek(fp,0,SEEK_END);
- size_t size = ftell(fp);
- fseek(fp,0,SEEK_SET);
-
- char* buf = malloc(sizeof(char)*size);
- fread(buf,size,1,fp);
- fclose(fp);
-
- image_t *image = loadpng_mem(buf,size,settings);
- free(buf);
-
- return image;
-}
-
View
11 modules/cuttherope.c
@@ -32,9 +32,6 @@
* Cut the Rope Lite - 0.1 crow_riot based on the works of thp
**/
-#include "../imagelib/imagelib.h"
-#include "../imagelib/loadjpeg.c"
-#include "../imagelib/loadpng.c"
#include "../apklib/keycodes.h"
#include <linux/limits.h>
#include <sys/stat.h>
@@ -288,13 +285,7 @@ cuttherope_loadImage(JNIEnv *env, const char *filename )
.swaprb = 1,
};
- if (strstr(filepath,".png")!=0) {
- img->image = loadpng_mem(buf,buf_size,loadsettings);
- }
- else
- if (strstr(filepath,".jpeg")!=0) {
- img->image = loadjpeg_mem(buf,buf_size,loadsettings);
- }
+ img->image = GLOBAL_J(env)->image_loader(buf, buf_size, loadsettings);
}
return img->image;
Please sign in to comment.
Something went wrong with that request. Please try again.