-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert to previous version with CPP optimized Bitmap to CPCL/ZPL
- Loading branch information
Showing
11 changed files
with
937 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# For more information about using CMake with Android Studio, read the | ||
# documentation: https://d.android.com/studio/projects/add-native-code.html | ||
|
||
# Sets the minimum version of CMake required to build the native library. | ||
|
||
cmake_minimum_required(VERSION 3.4.1) | ||
|
||
add_definitions(-DANDROID) | ||
|
||
if (CMAKE_BUILD_TYPE STREQUAL "Release") | ||
add_definitions(-DNDEBUG) | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -ffunction-sections -fdata-sections") | ||
set(LINKER_FLAGS "${LINKER_FLAGS} --strip-all") | ||
Message("--- Release Mode ---") | ||
endif() | ||
|
||
include_directories(src/main/cpp/) | ||
|
||
add_library( # Sets the name of the library. | ||
ZebraUtils | ||
|
||
# Sets the library as a shared library. | ||
SHARED | ||
|
||
# Provides a relative path to your source file(s). | ||
jni.cpp | ||
ZPLHelper.cpp | ||
CPCHelper.cpp | ||
Internal.h) | ||
|
||
find_library( # Sets the name of the path variable. | ||
log-lib | ||
z | ||
jnigraphics | ||
# Specifies the name of the NDK library that | ||
# you want CMake to locate. | ||
log) | ||
|
||
target_link_libraries( # Specifies the target library. | ||
ZebraUtils | ||
-ljnigraphics | ||
-lz | ||
-llog | ||
|
||
# Links the target library to the log library | ||
# included in the NDK. | ||
${log-lib}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#include "Internal.h" | ||
|
||
const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; | ||
|
||
|
||
bool CPCconvertImage(AndroidBitmapInfo *info, void *pixels, char **out) | ||
{ | ||
uint32_t iWidth = info->width; | ||
uint32_t iHeight = info->height; | ||
uint32_t iMonoSize = ((iWidth * iHeight) / 8); | ||
uint32_t iGreySize = iHeight * iWidth * sizeof(int16_t); | ||
void *pPtr = pixels; | ||
|
||
*out = nullptr; | ||
int16_t* bGreyImg = (int16_t*) malloc(iGreySize); | ||
if (bGreyImg == nullptr) | ||
{ | ||
LOGE("Memory allocation failed (GreyImage)"); | ||
return false; | ||
} | ||
int8_t* bMonoImg = (int8_t *) malloc(iMonoSize); | ||
if (bMonoImg == nullptr) | ||
{ | ||
LOGE("Memory allocation failed (Mono Image)"); | ||
free(bGreyImg); | ||
return false; | ||
} | ||
|
||
uint8_t bValue = 0; | ||
int oldpixel, newpixel, error; | ||
bool nbottom, nleft, nright; | ||
|
||
//RGB888 Format | ||
if (info->format == ANDROID_BITMAP_FORMAT_RGBA_8888) | ||
{ | ||
for (int y=0; y < iHeight; y++) | ||
{ | ||
uint32_t *line = (uint32_t *) pPtr; | ||
for (int x=0; x<iWidth; x++) | ||
{ | ||
uint32_t col = line[x]; | ||
int a = (col & 0xff000000 ) >> 24; | ||
int r = (col & 0xff0000 ) >> 16; | ||
int g = (col & 0x00ff00 ) >> 8; | ||
int b = (col & 0x0000ff ); | ||
int m = (int)(0.299 * (double)r + 0.587 * (double)g + 0.114 * (double)b); | ||
m = ((a * m) + ((255-a) * 255)) / 255; | ||
if (m > 255) m = 255; | ||
if (m < 0) m = 0; | ||
bGreyImg[AT(x,y)] = m; | ||
} | ||
pPtr = (char *) pPtr + info->stride; | ||
} | ||
} | ||
// RGB565 Format | ||
else if (info->format == ANDROID_BITMAP_FORMAT_RGB_565) | ||
{ | ||
for (int y=0; y < iHeight; y++) | ||
{ | ||
uint16_t *line = (uint16_t *) pPtr; | ||
for (int x=0; x<iWidth; x++) | ||
{ | ||
uint16_t col = line[x]; | ||
int r = RED(col); | ||
int g = GREEN(col); | ||
int b = BLUE(col); | ||
int m = (int)(0.299 * (double)r + 0.587 * (double)g + 0.114 * (double)b); | ||
if (m > 255) m = 255; | ||
if (m < 0) m = 0; | ||
bGreyImg[AT(x,y)] = m; | ||
} | ||
pPtr = (char *) pPtr + info->stride; | ||
} | ||
}else { | ||
LOGE("Bitmap format not supported"); | ||
free(bGreyImg); | ||
free(bMonoImg); | ||
return false; | ||
} | ||
|
||
// Dither the Image | ||
for (int y = 0; y < iHeight; y++) | ||
{ | ||
nbottom = y < iHeight - 1; | ||
for (int x = 0; x < iWidth; x++) | ||
{ | ||
nleft = x > 0; | ||
nright = x < iWidth - 1; | ||
|
||
oldpixel = bGreyImg[AT(x,y)]; | ||
newpixel = oldpixel < 128 ? 0 : 255; | ||
bGreyImg[AT(x,y)] = newpixel; | ||
|
||
error = oldpixel - newpixel; | ||
if (nright) bGreyImg[AT(x+1,y)] += (int) (error * (7. / 16)); | ||
if (nleft & nbottom) bGreyImg[AT(x-1,y+1)] += (int) (error * (3. / 16)); | ||
if (nbottom) bGreyImg[AT(x,y+1)] += (int) (error * (5. / 16)); | ||
if (nright && nbottom) bGreyImg[AT(x+1,y+1)] += (int) (error * (1. / 16)); | ||
} | ||
} | ||
|
||
//Convert to Mono | ||
int iPos = 0; | ||
for (int y = 0; y < iHeight; y++) | ||
{ | ||
for (int x = 0; x < iWidth; x++) | ||
{ | ||
bValue <<= 1; | ||
if (bGreyImg[AT(x,y)] < 128) bValue |=1; | ||
if (iPos % 8 == 7) bMonoImg[iPos >> 3] = (bValue & 0xff); | ||
iPos++; | ||
} | ||
} | ||
|
||
//Free some space | ||
free(bGreyImg); | ||
|
||
char *outPtr = (char *) malloc((iMonoSize << 1) + 2); | ||
if (outPtr == nullptr) | ||
{ | ||
LOGE("Memory allocation failed (CPC Data)"); | ||
free(bGreyImg); | ||
} | ||
memset(outPtr,0,((iMonoSize << 1) + 2)); | ||
*out = outPtr; | ||
|
||
//Return CPC data | ||
for (int p=0; p < iMonoSize; p++) | ||
{ | ||
outPtr[p * 2] = hexmap[(bMonoImg[p] & 0xF0) >> 4]; | ||
outPtr[p * 2 + 1] = hexmap[(bMonoImg[p] & 0x0F)]; | ||
} | ||
free(bMonoImg); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef ZEBRA_UTILS_INTERNAL_H | ||
#define ZEBRA_UTILS_INTERNAL_H | ||
|
||
#include <jni.h> | ||
#include <android/log.h> | ||
#include <android/bitmap.h> | ||
#include <string> | ||
#include <pthread.h> | ||
#include <mutex> | ||
#include <stdint.h> | ||
#include <math.h> | ||
#include <thread> | ||
#include <zlib.h> | ||
#include "base64.h" | ||
|
||
//Globals | ||
extern JavaVM* g_JVM; | ||
|
||
#ifdef NDEBUG | ||
#define ENGINE_VERSION "1.1.0.0 ( " __DATE__ " )" | ||
#else | ||
#define ENGINE_VERSION "1.1.0.0 ( " __DATE__ " ) - Debug" | ||
#endif | ||
|
||
//Useful defines | ||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__) | ||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG,__VA_ARGS__) | ||
#define SIZE(s) (sizeof(s) / sizeof(s[0])) | ||
#define ZPLHELPER_CLASS "com/zebra/printwrapper/RasterizationHelper" | ||
#define LOG_TAG "Zebra-Utils" | ||
#define RED(a) (((((a >> 11) & 0x1F) * 527) + 23) >> 6) | ||
#define GREEN(a) (((((a >> 5) & 0x3F) * 259) + 33) >> 6) | ||
#define BLUE(a) ((((a & 0x1F) * 527) + 23) >> 6) | ||
#define AT(x,y) ((iWidth * (y)) + (x)) | ||
|
||
//RasterizationHelper.cpp | ||
bool ZPLconvertImage(AndroidBitmapInfo *info, void *pixels, char **out); | ||
|
||
//CPCHelper.cpp | ||
bool CPCconvertImage(AndroidBitmapInfo *info, void *pixels, char **out); | ||
|
||
|
||
#endif //ZEBRA_UTILS_INTERNAL_H |
Oops, something went wrong.