15 #include "pnmFileTypeAndroid.h" 19 #include "config_pnmimagetypes.h" 20 #include "config_express.h" 22 #include <android/bitmap.h> 26 static uint8_t scale_table_4[16];
27 static uint8_t scale_table_5[32];
28 static uint8_t scale_table_6[64];
30 static void init_scale_tables() {
31 static bool initialized =
false;
34 for (i = 0; i < 16; ++i) {
35 scale_table_4[i] = 255 * i / 15;
37 for (i = 0; i < 32; ++i) {
38 scale_table_5[i] = 255 * i / 31;
40 for (i = 0; i < 64; ++i) {
41 scale_table_6[i] = 255 * i / 63;
47 static void conv_rgb565(uint16_t in,
xel &out) {
48 out.r = scale_table_5[(in >> 11) & 31];
49 out.g = scale_table_6[(in >> 5) & 63];
50 out.b = scale_table_5[in & 31];
53 static void conv_rgba4444(uint16_t in,
xel &rgb, xelval &alpha) {
54 rgb.r = scale_table_4[(in >> 12) & 0xF];
55 rgb.g = scale_table_4[(in >> 8) & 0xF];
56 rgb.b = scale_table_4[(in >> 4) & 0xF];
57 alpha = scale_table_4[in & 0xF];
65 PNMFileTypeAndroid::Reader::
66 Reader(
PNMFileType *type, istream *file,
bool owns_file,
string magic_number) :
67 PNMReader(type, file, owns_file), _bitmap(NULL)
70 for (string::reverse_iterator mi = magic_number.rbegin();
71 mi != magic_number.rend(); ++mi) {
76 <<
"Unable to put back magic number.\n";
81 streampos pos = _file->tellg();
83 jobject opts = _env->CallStaticObjectMethod(jni_PandaActivity,
84 jni_PandaActivity_readBitmapSize,
87 if (_file->tellg() != pos) {
89 <<
"Unable to seek back to beginning.\n";
94 _x_size = _env->GetIntField(opts, jni_BitmapFactory_Options_outWidth);
95 _y_size = _env->GetIntField(opts, jni_BitmapFactory_Options_outHeight);
97 if (_x_size < 0 || _y_size < 0) {
99 <<
"Failed to read header of " << *
this <<
"\n";
107 if (android_cat.is_debug()) {
109 <<
"Reading " << *
this <<
"\n";
118 PNMFileTypeAndroid::Reader::
120 if (_bitmap != NULL) {
121 _env->DeleteGlobalRef(_bitmap);
137 void PNMFileTypeAndroid::Reader::
140 _orig_x_size = _x_size;
141 _orig_y_size = _y_size;
143 if (_has_read_size && _read_x_size != 0 && _read_y_size != 0) {
144 int x_reduction = _orig_x_size / _read_x_size;
145 int y_reduction = _orig_y_size / _read_y_size;
147 _sample_size = max(min(x_reduction, y_reduction), 1);
150 _bitmap = _env->CallStaticObjectMethod(jni_PandaActivity,
151 jni_PandaActivity_readBitmap,
152 (jlong) _file, _sample_size);
154 if (_bitmap == NULL) {
156 <<
"Failed to read " << *
this <<
"\n";
161 _bitmap = _env->NewGlobalRef(_bitmap);
163 AndroidBitmapInfo info;
164 if (AndroidBitmap_getInfo(_env, _bitmap, &info) < 0) {
166 <<
"Failed to get info of " << *
this <<
"\n";
171 _x_size = info.width;
172 _y_size = info.height;
173 _format = info.format;
174 _stride = info.stride;
181 switch (info.format) {
182 case ANDROID_BITMAP_FORMAT_RGBA_8888:
185 <<
"Bitmap has format RGBA_8888\n";
187 case ANDROID_BITMAP_FORMAT_RGB_565:
190 <<
"Bitmap has format RGB_565\n";
192 case ANDROID_BITMAP_FORMAT_RGBA_4444:
195 <<
"Bitmap has format RGBA_4444\n";
197 case ANDROID_BITMAP_FORMAT_A_8:
200 <<
"Bitmap has format A_8\n";
204 <<
"Unsupported bitmap format!\n";
224 int PNMFileTypeAndroid::Reader::
225 read_data(
xel *rgb, xelval *alpha) {
230 if (AndroidBitmap_lockPixels(_env, _bitmap, &ptr) < 0) {
232 <<
"Failed to lock bitmap for reading.\n";
237 case ANDROID_BITMAP_FORMAT_RGBA_8888: {
238 nassertr(_stride == _x_size * 4, 0);
239 uint8_t *data = (uint8_t *) ptr;
240 for (
int y = 0; y < _y_size; ++y) {
241 for (
int x = 0; x < _x_size; ++x) {
253 case ANDROID_BITMAP_FORMAT_RGB_565: {
254 nassertr(_stride == _x_size * 2, 0);
257 uint16_t *data = (uint16_t *) ptr;
258 for (
int y = 0; y < _y_size; ++y) {
259 for (
int x = 0; x < _x_size; ++x) {
260 conv_rgb565(data[x], rgb[x]);
267 case ANDROID_BITMAP_FORMAT_RGBA_4444: {
268 nassertr(_stride == _x_size * 2, 0);
271 uint16_t *data = (uint16_t *) ptr;
272 for (
int y = 0; y < _y_size; ++y) {
273 for (
int x = 0; x < _x_size; ++x) {
274 conv_rgba4444(data[x], rgb[x], alpha[x]);
282 case ANDROID_BITMAP_FORMAT_A_8: {
283 nassertr(_stride == _x_size, 0);
284 uint8_t *data = (uint8_t *) ptr;
285 for (
int y = 0; y < _y_size; ++y) {
286 for (
int x = 0; x < _x_size; ++x) {
295 AndroidBitmap_unlockPixels(_env, _bitmap);
299 AndroidBitmap_unlockPixels(_env, _bitmap);
This is the base class of a family of classes that represent particular image file types that PNMImag...
This is an abstract base class that defines the interface for reading image files of various types...