a
554325746@qq.com
2019-12-25 7340eb0b160eacbbd0f3c2289e3ac6150da235f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include <jni.h>
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <android/bitmap.h>
#include <cstring>
#include <unistd.h>
 
#define  LOG_TAG    "Applog"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
 
extern "C"
    {
//store
    JNIEXPORT jobject JNICALL Java_com_basic_security_utils_BitmapHolder_jniStoreBitmapData(
        JNIEnv * env, jobject obj, jobject bitmap);
//get
    JNIEXPORT jobject JNICALL Java_com_basic_security_utils_BitmapHolder_jniGetBitmapFromStoredBitmapData(
        JNIEnv * env, jobject obj, jobject handle);
//free
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniFreeBitmapData(
        JNIEnv * env, jobject obj, jobject handle);
//rotate 90 degrees CCW
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniRotateBitmapCcw90(
        JNIEnv * env, jobject obj, jobject handle);
//rotate 90 degrees CW
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniRotateBitmapCw90(
        JNIEnv * env, jobject obj, jobject handle);
//rotate 180 degrees
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniRotateBitmap180(
        JNIEnv * env, jobject obj, jobject handle);
//crop
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniCropBitmap(
        JNIEnv * env, jobject obj, jobject handle, uint32_t left,
        uint32_t top, uint32_t right, uint32_t bottom);
//scale using nearest neighbor
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniScaleNNBitmap(
        JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
        uint32_t newHeight);
 
//scale using Bilinear Interpolation
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniScaleBIBitmap(
        JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
        uint32_t newHeight);
 
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniFlipBitmapHorizontal(
        JNIEnv * env, jobject obj, jobject handle);
 
    JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniFlipBitmapVertical(
        JNIEnv * env, jobject obj, jobject handle);
    }
 
class JniBitmap
    {
public:
    uint32_t* _storedBitmapPixels;
    AndroidBitmapInfo _bitmapInfo;
    JniBitmap()
    {
    _storedBitmapPixels = NULL;
    }
    };
 
typedef struct
    {
    uint8_t alpha, red, green, blue;
    } ARGB;
 
int32_t convertArgbToInt(ARGB argb)
    {
    return (argb.alpha) | (argb.red << 24) | (argb.green << 16)
        | (argb.blue << 8);
    }
 
void convertIntToArgb(uint32_t pixel, ARGB* argb)
    {
    argb->red = ((pixel >> 24) & 0xff);
    argb->green = ((pixel >> 16) & 0xff);
    argb->blue = ((pixel >> 8) & 0xff);
    argb->alpha = (pixel & 0xff);
    }
 
/**crops the bitmap within to be smaller. note that no validations are done*/ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniCropBitmap(
    JNIEnv * env, jobject obj, jobject handle, uint32_t left, uint32_t top,
    uint32_t right, uint32_t bottom)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    uint32_t oldWidth = jniBitmap->_bitmapInfo.width;
    uint32_t newWidth = right - left, newHeight = bottom - top;
    uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
    uint32_t* whereToGet = previousData + left + top * oldWidth;
    uint32_t* whereToPut = newBitmapPixels;
    for (int y = top; y < bottom; ++y)
    {
    memcpy(whereToPut, whereToGet, sizeof(uint32_t) * newWidth);
    whereToGet += oldWidth;
    whereToPut += newWidth;
    }
    //done copying , so replace old data with new one
    delete[] previousData;
    jniBitmap->_storedBitmapPixels = newBitmapPixels;
    jniBitmap->_bitmapInfo.width = newWidth;
    jniBitmap->_bitmapInfo.height = newHeight;
    }
 
/**rotates the inner bitmap data by 90 degrees counter clock wise*/ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniRotateBitmapCcw90(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    uint32_t newWidth = jniBitmap->_bitmapInfo.height;
    uint32_t newHeight = jniBitmap->_bitmapInfo.width;
    jniBitmap->_bitmapInfo.width = newWidth;
    jniBitmap->_bitmapInfo.height = newHeight;
    uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
    int whereToGet = 0;
    // XY. ... ... ..X
    // ...>Y..>...>..Y
    // ... X.. .YX ...
    for (int x = 0; x < newWidth; ++x)
    for (int y = newHeight - 1; y >= 0; --y)
        {
        //take from each row (up to bottom), from left to right
        uint32_t pixel = previousData[whereToGet++];
        newBitmapPixels[newWidth * y + x] = pixel;
        }
    delete[] previousData;
    jniBitmap->_storedBitmapPixels = newBitmapPixels;
    }
 
/**rotates the inner bitmap data by 90 degrees clock wise*/ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniRotateBitmapCw90(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    uint32_t newWidth = jniBitmap->_bitmapInfo.height;
    uint32_t newHeight = jniBitmap->_bitmapInfo.width;
    jniBitmap->_bitmapInfo.width = newWidth;
    jniBitmap->_bitmapInfo.height = newHeight;
    uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
    int whereToGet = 0;
    // XY. ..X ... ...
    // ...>..Y>...>Y..
    // ... ... .YX X..
    jniBitmap->_storedBitmapPixels = newBitmapPixels;
    for (int x = newWidth - 1; x >= 0; --x)
    for (int y = 0; y < newHeight; ++y)
        {
        //take from each row (up to bottom), from left to right
        uint32_t pixel = previousData[whereToGet++];
        newBitmapPixels[newWidth * y + x] = pixel;
        }
    delete[] previousData;
    }
 
/**rotates the inner bitmap data by 180 degrees (*/ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniRotateBitmap180(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t* pixels = jniBitmap->_storedBitmapPixels;
    uint32_t* pixels2 = jniBitmap->_storedBitmapPixels;
    uint32_t width = jniBitmap->_bitmapInfo.width;
    uint32_t height = jniBitmap->_bitmapInfo.height;
    //no need to create a totally new bitmap - it's the exact same size as the original
    // 1234 fedc
    // 5678>ba09
    // 90ab>8765
    // cdef 4321
    int whereToGet = 0;
    for (int y = height - 1; y >= height / 2; --y)
    for (int x = width - 1; x >= 0; --x)
        {
        //take from each row (up to bottom), from left to right
        uint32_t tempPixel = pixels2[width * y + x];
        pixels2[width * y + x] = pixels[whereToGet];
        pixels[whereToGet] = tempPixel;
        ++whereToGet;
        }
    //if the height isn't even, flip the middle row :
    if (height % 2 == 1)
    {
    int y = height / 2;
    whereToGet = width * y;
    int lastXToHandle = width % 2 == 0 ? (width / 2) : (width / 2) - 1;
    for (int x = width - 1; x >= lastXToHandle; --x)
        {
        uint32_t tempPixel = pixels2[width * y + x];
        pixels2[width * y + x] = pixels[whereToGet];
        pixels[whereToGet] = tempPixel;
        ++whereToGet;
        }
    }
    }
 
/**free bitmap*/ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniFreeBitmapData(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    delete[] jniBitmap->_storedBitmapPixels;
    jniBitmap->_storedBitmapPixels = NULL;
    delete jniBitmap;
    }
 
/**restore java bitmap (from JNI data)*/ //
JNIEXPORT jobject JNICALL Java_com_basic_security_utils_BitmapHolder_jniGetBitmapFromStoredBitmapData(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    {
    LOGD("no bitmap data was stored. returning null...");
    return NULL;
    }
    //
    //creating a new bitmap to put the pixels into it - using Bitmap Bitmap.createBitmap (int width, int height, Bitmap.Config config) :
    //
    jclass bitmapCls = env->FindClass("android/graphics/Bitmap");
    jmethodID createBitmapFunction = env->GetStaticMethodID(bitmapCls,
        "createBitmap",
        "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
    jstring configName = env->NewStringUTF("ARGB_8888");
    jclass bitmapConfigClass = env->FindClass("android/graphics/Bitmap$Config");
    jmethodID valueOfBitmapConfigFunction = env->GetStaticMethodID(
        bitmapConfigClass, "valueOf",
        "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;");
    jobject bitmapConfig = env->CallStaticObjectMethod(bitmapConfigClass,
        valueOfBitmapConfigFunction, configName);
    jobject newBitmap = env->CallStaticObjectMethod(bitmapCls,
        createBitmapFunction, jniBitmap->_bitmapInfo.width,
        jniBitmap->_bitmapInfo.height, bitmapConfig);
    //
    // putting the pixels into the new bitmap:
    //
    int ret;
    void* bitmapPixels;
    if ((ret = AndroidBitmap_lockPixels(env, newBitmap, &bitmapPixels)) < 0)
    {
    LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    return NULL;
    }
    uint32_t* newBitmapPixels = (uint32_t*) bitmapPixels;
    int pixelsCount = jniBitmap->_bitmapInfo.height
        * jniBitmap->_bitmapInfo.width;
    memcpy(newBitmapPixels, jniBitmap->_storedBitmapPixels,
        sizeof(uint32_t) * pixelsCount);
    AndroidBitmap_unlockPixels(env, newBitmap);
    //LOGD("returning the new bitmap");
    return newBitmap;
    }
 
/**store java bitmap as JNI data*/ //
JNIEXPORT jobject JNICALL Java_com_basic_security_utils_BitmapHolder_jniStoreBitmapData(
    JNIEnv * env, jobject obj, jobject bitmap)
    {
    AndroidBitmapInfo bitmapInfo;
    uint32_t* storedBitmapPixels = NULL;
    //LOGD("reading bitmap info...");
    int ret;
    if ((ret = AndroidBitmap_getInfo(env, bitmap, &bitmapInfo)) < 0)
    {
    LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
    return NULL;
    }
    //LOGD("width:%d height:%d stride:%d", bitmapInfo.width, bitmapInfo.height, bitmapInfo.stride);
    if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
    {
    LOGE("Bitmap format is not RGBA_8888!");
    return NULL;
    }
    //
    //read pixels of bitmap into native memory :
    //
    //LOGD("reading bitmap pixels...");
    void* bitmapPixels;
    if ((ret = AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels)) < 0)
    {
    LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    return NULL;
    }
    uint32_t* src = (uint32_t*) bitmapPixels;
    storedBitmapPixels = new uint32_t[bitmapInfo.height * bitmapInfo.width];
    int pixelsCount = bitmapInfo.height * bitmapInfo.width;
    memcpy(storedBitmapPixels, src, sizeof(uint32_t) * pixelsCount);
    AndroidBitmap_unlockPixels(env, bitmap);
    JniBitmap *jniBitmap = new JniBitmap();
    jniBitmap->_bitmapInfo = bitmapInfo;
    jniBitmap->_storedBitmapPixels = storedBitmapPixels;
    return env->NewDirectByteBuffer(jniBitmap, 0);
    }
 
/**scales the image using the fastest, simplest algorithm called "nearest neighbor" */ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniScaleNNBitmap(
    JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
    uint32_t newHeight)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t oldWidth = jniBitmap->_bitmapInfo.width;
    uint32_t oldHeight = jniBitmap->_bitmapInfo.height;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
    int x2, y2;
    int whereToPut = 0;
    for (int y = 0; y < newHeight; ++y)
    {
    for (int x = 0; x < newWidth; ++x)
        {
        x2 = x * oldWidth / newWidth;
        if (x2 < 0)
        x2 = 0;
        else if (x2 >= oldWidth)
        x2 = oldWidth - 1;
        y2 = y * oldHeight / newHeight;
        if (y2 < 0)
        y2 = 0;
        else if (y2 >= oldHeight)
        y2 = oldHeight - 1;
        newBitmapPixels[whereToPut++] = previousData[(y2 * oldWidth) + x2];
        //same as : newBitmapPixels[(y * newWidth) + x] = previousData[(y2 * oldWidth) + x2];
        }
    }
 
    delete[] previousData;
    jniBitmap->_storedBitmapPixels = newBitmapPixels;
    jniBitmap->_bitmapInfo.width = newWidth;
    jniBitmap->_bitmapInfo.height = newHeight;
    }
 
/**scales the image using a high-quality algorithm called "Bilinear Interpolation"
 * code is based on old university code I've made in Java: http://stackoverflow.com/questions/23230047/trying-to-convert-bilinear-interpolation-code-from-java-to-c-c-on-android/23302384#23302384
 * */ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniScaleBIBitmap(
    JNIEnv * env, jobject obj, jobject handle, uint32_t newWidth,
    uint32_t newHeight)
    {
 
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t oldWidth = jniBitmap->_bitmapInfo.width;
    uint32_t oldHeight = jniBitmap->_bitmapInfo.height;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    uint32_t* newBitmapPixels = new uint32_t[newWidth * newHeight];
    // position of the top left pixel of the 4 pixels to use interpolation on
    int xTopLeft, yTopLeft;
    int x, y, lastTopLefty;
    float xRatio = (float) newWidth / (float) oldWidth, yratio =
        (float) newHeight / (float) oldHeight;
    // Y color ratio to use on left and right pixels for interpolation
    float ycRatio2 = 0, ycRatio1 = 0;
    // pixel target in the src
    float xt, yt;
    // X color ratio to use on left and right pixels for interpolation
    float xcRatio2 = 0, xcratio1 = 0;
    ARGB rgbTopLeft, rgbTopRight, rgbBottomLeft, rgbBottomRight, rgbTopMiddle,
        rgbBottomMiddle, result;
    for (x = 0; x < newWidth; ++x)
    {
    xTopLeft = (int) (xt = x / xRatio);
    // when meeting the most right edge, move left a little
    if (xTopLeft >= oldWidth - 1)
        xTopLeft--;
    if (xt <= xTopLeft + 1)
        {
        // we are between the left and right pixel
        xcratio1 = xt - xTopLeft;
        // color ratio in favor of the right pixel color
        xcRatio2 = 1 - xcratio1;
        }
    for (y = 0, lastTopLefty = -30000; y < newHeight; ++y)
        {
        yTopLeft = (int) (yt = y / yratio);
        // when meeting the most bottom edge, move up a little
        if (yTopLeft >= oldHeight - 1)
        --yTopLeft;
        if (lastTopLefty == yTopLeft - 1)
        {
        // we went down only one rectangle
        rgbTopLeft = rgbBottomLeft;
        rgbTopRight = rgbBottomRight;
        rgbTopMiddle = rgbBottomMiddle;
        //rgbBottomLeft=startingImageData[xTopLeft][yTopLeft+1];
        convertIntToArgb(
            previousData[((yTopLeft + 1) * oldWidth) + xTopLeft],
            &rgbBottomLeft);
        //rgbBottomRight=startingImageData[xTopLeft+1][yTopLeft+1];
        convertIntToArgb(
            previousData[((yTopLeft + 1) * oldWidth)
                + (xTopLeft + 1)], &rgbBottomRight);
        rgbBottomMiddle.alpha = rgbBottomLeft.alpha * xcRatio2
            + rgbBottomRight.alpha * xcratio1;
        rgbBottomMiddle.red = rgbBottomLeft.red * xcRatio2
            + rgbBottomRight.red * xcratio1;
        rgbBottomMiddle.green = rgbBottomLeft.green * xcRatio2
            + rgbBottomRight.green * xcratio1;
        rgbBottomMiddle.blue = rgbBottomLeft.blue * xcRatio2
            + rgbBottomRight.blue * xcratio1;
        }
        else if (lastTopLefty != yTopLeft)
        {
        // we went to a totally different rectangle (happens in every loop start,and might happen more when making the picture smaller)
        //rgbTopLeft=startingImageData[xTopLeft][yTopLeft];
        convertIntToArgb(previousData[(yTopLeft * oldWidth) + xTopLeft],
            &rgbTopLeft);
        //rgbTopRight=startingImageData[xTopLeft+1][yTopLeft];
        convertIntToArgb(
            previousData[((yTopLeft + 1) * oldWidth) + xTopLeft],
            &rgbTopRight);
        rgbTopMiddle.alpha = rgbTopLeft.alpha * xcRatio2
            + rgbTopRight.alpha * xcratio1;
        rgbTopMiddle.red = rgbTopLeft.red * xcRatio2
            + rgbTopRight.red * xcratio1;
        rgbTopMiddle.green = rgbTopLeft.green * xcRatio2
            + rgbTopRight.green * xcratio1;
        rgbTopMiddle.blue = rgbTopLeft.blue * xcRatio2
            + rgbTopRight.blue * xcratio1;
        //rgbBottomLeft=startingImageData[xTopLeft][yTopLeft+1];
        convertIntToArgb(
            previousData[((yTopLeft + 1) * oldWidth) + xTopLeft],
            &rgbBottomLeft);
        //rgbBottomRight=startingImageData[xTopLeft+1][yTopLeft+1];
        convertIntToArgb(
            previousData[((yTopLeft + 1) * oldWidth)
                + (xTopLeft + 1)], &rgbBottomRight);
        rgbBottomMiddle.alpha = rgbBottomLeft.alpha * xcRatio2
            + rgbBottomRight.alpha * xcratio1;
        rgbBottomMiddle.red = rgbBottomLeft.red * xcRatio2
            + rgbBottomRight.red * xcratio1;
        rgbBottomMiddle.green = rgbBottomLeft.green * xcRatio2
            + rgbBottomRight.green * xcratio1;
        rgbBottomMiddle.blue = rgbBottomLeft.blue * xcRatio2
            + rgbBottomRight.blue * xcratio1;
        }
        lastTopLefty = yTopLeft;
        if (yt <= yTopLeft + 1)
        {
        // color ratio in favor of the bottom pixel color
        ycRatio1 = yt - yTopLeft;
        ycRatio2 = 1 - ycRatio1;
        }
        // prepared all pixels to look at, so finally set the new pixel data
        result.alpha = rgbTopMiddle.alpha * ycRatio2
            + rgbBottomMiddle.alpha * ycRatio1;
        result.blue = rgbTopMiddle.blue * ycRatio2
            + rgbBottomMiddle.blue * ycRatio1;
        result.red = rgbTopMiddle.red * ycRatio2
            + rgbBottomMiddle.red * ycRatio1;
        result.green = rgbTopMiddle.green * ycRatio2
            + rgbBottomMiddle.green * ycRatio1;
        newBitmapPixels[(y * newWidth) + x] = convertArgbToInt(result);
        }
    }
    //get rid of old data, and replace it with new one
    delete[] previousData;
    jniBitmap->_storedBitmapPixels = newBitmapPixels;
    jniBitmap->_bitmapInfo.width = newWidth;
    jniBitmap->_bitmapInfo.height = newHeight;
    }
 
/**flips a bitmap horizontally, as such:
 *
 * 123    321
 * 456 => 654
 * 789    987
 *
 * */ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniFlipBitmapHorizontal(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    int width = jniBitmap->_bitmapInfo.width, middle = width / 2, height =
        jniBitmap->_bitmapInfo.height;
    for (int y = 0; y < height; ++y)
    {
    //for each row, switch between the first pixels and the last ones
    uint32_t* idx1 = previousData + width * y;
    uint32_t* idx2 = previousData + width * (y + 1) - 1;
    for (int x = 0; x < middle; ++x)
        {
        uint32_t pixel = *idx1; //pixel= previousData[rowStart + x];
        *idx1 = *idx2; //previousData[rowStart + x] =previousData[rowStart + (width - x - 1)];
        *idx2 = pixel; //previousData[rowStart + (width - x - 1)] = pixel;
        ++idx1;
        --idx2;
        }
    }
    }
 
/**flips a bitmap vertically, as such:
 *
 * 123    789
 * 456 => 456
 * 789    123
 *
 * */ //
JNIEXPORT void JNICALL Java_com_basic_security_utils_BitmapHolder_jniFlipBitmapVertical(
    JNIEnv * env, jobject obj, jobject handle)
    {
    JniBitmap* jniBitmap = (JniBitmap*) env->GetDirectBufferAddress(handle);
    if (jniBitmap == NULL || jniBitmap->_storedBitmapPixels == NULL)
    return;
    uint32_t* previousData = jniBitmap->_storedBitmapPixels;
    int width = jniBitmap->_bitmapInfo.width, height =
        jniBitmap->_bitmapInfo.height, middle = height / 2;
    for (int y = 0; y < middle; ++y)
    {
    //for each row till the middle row, switch its pixels with the one at the bottom
    uint32_t* idx1 = previousData + width * y;
    uint32_t* idx2 = previousData + width * (height - y - 1);
    for (int x = 0; x < width; ++x)
        {
        uint32_t pixel =*idx1;
        *idx1=*idx2;
        *idx2=pixel;
        ++idx2;
        ++idx1;
        }
    }
    }