Skip to content

Commit

Permalink
Revert to previous version with CPP optimized Bitmap to CPCL/ZPL
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrudu committed Dec 20, 2024
1 parent d073246 commit eedcba5
Show file tree
Hide file tree
Showing 11 changed files with 937 additions and 5 deletions.
19 changes: 19 additions & 0 deletions PrintWrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ ext {
android {
namespace 'com.zebra.printwrapper'

ndkVersion '28.0.12674087 rc2'
compileSdkVersion 34

defaultConfig {
minSdkVersion 24
targetSdkVersion 34
versionCode 17
versionName "1.17"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"

externalNativeBuild {
cmake {
cppFlags "-std=c++14"
}
}
ndk {
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}

buildTypes {
Expand All @@ -33,6 +46,12 @@ android {
debuggable true
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.22.1"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
17 changes: 17 additions & 0 deletions PrintWrapper/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#-renamesourcefileattribute SourceFile
#noinspection ShrinkerUnresolvedReference
-keepclassmembers class **.RasterizationHelper {
native <methods>;
#noinspection ShrinkerUnresolvedReference
String getUtilsVersion();
#noinspection ShrinkerUnresolvedReference,ShrinkerUnresolvedReference
String createBitmapZPL(Bitmap);
#noinspection ShrinkerUnresolvedReference,ShrinkerUnresolvedReference
String createBitmapCPC(Bitmap);
}

# Parceler library
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
47 changes: 47 additions & 0 deletions PrintWrapper/src/main/cpp/CMakeLists.txt
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})
135 changes: 135 additions & 0 deletions PrintWrapper/src/main/cpp/CPCHelper.cpp
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;
}
43 changes: 43 additions & 0 deletions PrintWrapper/src/main/cpp/Internal.h
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
Loading

0 comments on commit eedcba5

Please sign in to comment.