Skip to content

Commit 7233305

Browse files
NiLuJeFrenzie
authored andcommitted
A few blitting changes for better BB8 performance (koreader#816)
* Avoid a temporary copy & conversion for 8bpp fbs in mupdf.scaleBlitBuffer Now that we can tell MuPDF the exact stride and whether an alpha channel is present, this *should* behave. We still have a few BB types that needs the temporary copy, mostly because they're terrible to begin with, so we can't blame mupdf for not supporting 'em ;). (4bpp, because it's, err, fun to address...) (RGB565, because HAHAHAHAHAHAHAHAHAH). * Bump LodePNG to 20190210 * Treat PNGs paletted to 16c as grayscale There's a good chance they're properly dithered to the eInk palette. (Otherwise, paletted PNGs are expanded to RGB/RGBA).
1 parent 45e96d3 commit 7233305

File tree

11 files changed

+441
-99
lines changed

11 files changed

+441
-99
lines changed

blitbuffer.c

+129-56
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,14 @@ void BB_alpha_blit_from(BlitBuffer *dst, BlitBuffer *src,
850850
}
851851
o_y += 1;
852852
}
853-
} else if (dbb_type == TYPE_BB8A && sbb_type == TYPE_BB8A) {
854-
Color8A *dstptr, *srcptr;
853+
} else if (dbb_type == TYPE_BB8 && sbb_type == TYPE_BB8A) {
854+
Color8A *srcptr;
855+
Color8 *dstptr;
855856
o_y = offs_y;
856857
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
857858
o_x = offs_x;
858859
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
859-
BB_GET_PIXEL(dst, dbb_rotation, Color8A, d_x, d_y, &dstptr);
860+
BB_GET_PIXEL(dst, dbb_rotation, Color8, d_x, d_y, &dstptr);
860861
BB_GET_PIXEL(src, sbb_rotation, Color8A, o_x, o_y, &srcptr);
861862
alpha = srcptr->alpha;
862863
ainv = 0xFF - alpha;
@@ -865,124 +866,109 @@ void BB_alpha_blit_from(BlitBuffer *dst, BlitBuffer *src,
865866
}
866867
o_y += 1;
867868
}
868-
} else if (dbb_type == TYPE_BBRGB16 && sbb_type == TYPE_BBRGB16) {
869-
ColorRGB16 *dstptr, *srcptr;
869+
} else if (dbb_type == TYPE_BB8 && sbb_type == TYPE_BBRGB16) {
870+
ColorRGB16 *srcptr;
871+
Color8 *dstptr;
870872
o_y = offs_y;
871873
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
872874
o_x = offs_x;
873875
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
874-
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB16, d_x, d_y, &dstptr);
876+
BB_GET_PIXEL(dst, dbb_rotation, Color8, d_x, d_y, &dstptr);
875877
BB_GET_PIXEL(src, sbb_rotation, ColorRGB16, o_x, o_y, &srcptr);
876-
*dstptr = *srcptr;
878+
dstptr->a = ColorRGB16_To_A(srcptr->v);
877879
o_x += 1;
878880
}
879881
o_y += 1;
880882
}
881-
} else if (dbb_type == TYPE_BBRGB24 && sbb_type == TYPE_BBRGB24) {
882-
ColorRGB24 *dstptr, *srcptr;
883+
} else if (dbb_type == TYPE_BB8 && sbb_type == TYPE_BBRGB24) {
884+
ColorRGB24 *srcptr;
885+
Color8 *dstptr;
883886
o_y = offs_y;
884887
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
885888
o_x = offs_x;
886889
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
887-
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB24, d_x, d_y, &dstptr);
890+
BB_GET_PIXEL(dst, dbb_rotation, Color8, d_x, d_y, &dstptr);
888891
BB_GET_PIXEL(src, sbb_rotation, ColorRGB24, o_x, o_y, &srcptr);
889-
*dstptr = *srcptr;
892+
dstptr->a = RGB_To_A(srcptr->r, srcptr->g, srcptr->b);
890893
o_x += 1;
891894
}
892895
o_y += 1;
893896
}
894-
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BBRGB32) {
895-
ColorRGB32 *dstptr, *srcptr;
897+
} else if (dbb_type == TYPE_BB8 && sbb_type == TYPE_BBRGB32) {
898+
ColorRGB32 *srcptr;
899+
Color8 *dstptr;
900+
uint8_t srca;
896901
o_y = offs_y;
897902
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
898903
o_x = offs_x;
899904
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
900-
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
905+
BB_GET_PIXEL(dst, dbb_rotation, Color8, d_x, d_y, &dstptr);
901906
BB_GET_PIXEL(src, sbb_rotation, ColorRGB32, o_x, o_y, &srcptr);
902907
alpha = srcptr->alpha;
903908
ainv = 0xFF - alpha;
904-
dstptr->r = DIV_255(dstptr->r * ainv + srcptr->r * alpha);
905-
dstptr->g = DIV_255(dstptr->g * ainv + srcptr->g * alpha);
906-
dstptr->b = DIV_255(dstptr->b * ainv + srcptr->b * alpha);
909+
srca = RGB_To_A(srcptr->r, srcptr->g, srcptr->b);
910+
dstptr->a = DIV_255(dstptr->a * ainv + srca * alpha);
907911
o_x += 1;
908912
}
909913
o_y += 1;
910914
}
911-
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BBRGB24) {
912-
ColorRGB32 *dstptr;
913-
ColorRGB24 *srcptr;
915+
} else if (dbb_type == TYPE_BB8A && sbb_type == TYPE_BB8A) {
916+
Color8A *dstptr, *srcptr;
914917
o_y = offs_y;
915918
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
916919
o_x = offs_x;
917920
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
918-
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
919-
BB_GET_PIXEL(src, sbb_rotation, ColorRGB24, o_x, o_y, &srcptr);
920-
dstptr->r = srcptr->r;
921-
dstptr->g = srcptr->g;
922-
dstptr->b = srcptr->b;
923-
dstptr->alpha = 0xFF;
921+
BB_GET_PIXEL(dst, dbb_rotation, Color8A, d_x, d_y, &dstptr);
922+
BB_GET_PIXEL(src, sbb_rotation, Color8A, o_x, o_y, &srcptr);
923+
alpha = srcptr->alpha;
924+
ainv = 0xFF - alpha;
925+
dstptr->a = DIV_255(dstptr->a * ainv + srcptr->a * alpha);
924926
o_x += 1;
925927
}
926928
o_y += 1;
927929
}
928-
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BB8) {
929-
ColorRGB32 *dstptr;
930+
} else if (dbb_type == TYPE_BBRGB16 && sbb_type == TYPE_BB8) {
930931
Color8 *srcptr;
932+
ColorRGB16 *dstptr;
931933
o_y = offs_y;
932934
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
933935
o_x = offs_x;
934936
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
935-
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
937+
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB16, d_x, d_y, &dstptr);
936938
BB_GET_PIXEL(src, sbb_rotation, Color8, o_x, o_y, &srcptr);
937-
dstptr->r = srcptr->a;
938-
dstptr->g = srcptr->a;
939-
dstptr->b = srcptr->a;
940-
dstptr->alpha = 0xFF;
941-
o_x += 1;
942-
}
943-
o_y += 1;
944-
}
945-
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BB8A) {
946-
ColorRGB32 *dstptr;
947-
Color8A *srcptr;
948-
o_y = offs_y;
949-
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
950-
o_x = offs_x;
951-
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
952-
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
953-
BB_GET_PIXEL(src, sbb_rotation, Color8A, o_x, o_y, &srcptr);
954-
dstptr->r = srcptr->a;
955-
dstptr->g = srcptr->a;
956-
dstptr->b = srcptr->a;
957-
dstptr->alpha = srcptr->alpha; // if bad result, try: 0xFF - srcptr->alpha
939+
dstptr->v = RGB_To_RGB16(srcptr->a, srcptr->a, srcptr->a);
958940
o_x += 1;
959941
}
960942
o_y += 1;
961943
}
962944
} else if (dbb_type == TYPE_BBRGB16 && sbb_type == TYPE_BB8A) {
963945
Color8A *srcptr;
964946
ColorRGB16 *dstptr;
947+
uint8_t dsta, bdsta;
965948
o_y = offs_y;
966949
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
967950
o_x = offs_x;
968951
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
969952
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB16, d_x, d_y, &dstptr);
970953
BB_GET_PIXEL(src, sbb_rotation, Color8A, o_x, o_y, &srcptr);
971-
dstptr->v = RGB_To_RGB16(srcptr->a, srcptr->a, srcptr->a);
954+
alpha = srcptr->alpha;
955+
ainv = 0xFF - alpha;
956+
dsta = ColorRGB16_To_A(dstptr->v);
957+
bdsta = DIV_255(dsta * ainv + srcptr->a * alpha);
958+
dstptr->v = RGB_To_RGB16(bdsta, bdsta, bdsta);
972959
o_x += 1;
973960
}
974961
o_y += 1;
975962
}
976-
} else if (dbb_type == TYPE_BBRGB16 && sbb_type == TYPE_BB8) {
977-
Color8 *srcptr;
978-
ColorRGB16 *dstptr;
963+
} else if (dbb_type == TYPE_BBRGB16 && sbb_type == TYPE_BBRGB16) {
964+
ColorRGB16 *dstptr, *srcptr;
979965
o_y = offs_y;
980966
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
981967
o_x = offs_x;
982968
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
983969
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB16, d_x, d_y, &dstptr);
984-
BB_GET_PIXEL(src, sbb_rotation, Color8, o_x, o_y, &srcptr);
985-
dstptr->v = RGB_To_RGB16(srcptr->a, srcptr->a, srcptr->a);
970+
BB_GET_PIXEL(src, sbb_rotation, ColorRGB16, o_x, o_y, &srcptr);
971+
*dstptr = *srcptr;
986972
o_x += 1;
987973
}
988974
o_y += 1;
@@ -1021,6 +1007,93 @@ void BB_alpha_blit_from(BlitBuffer *dst, BlitBuffer *src,
10211007
}
10221008
o_y += 1;
10231009
}
1010+
} else if (dbb_type == TYPE_BBRGB24 && sbb_type == TYPE_BBRGB24) {
1011+
ColorRGB24 *dstptr, *srcptr;
1012+
o_y = offs_y;
1013+
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
1014+
o_x = offs_x;
1015+
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
1016+
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB24, d_x, d_y, &dstptr);
1017+
BB_GET_PIXEL(src, sbb_rotation, ColorRGB24, o_x, o_y, &srcptr);
1018+
*dstptr = *srcptr;
1019+
o_x += 1;
1020+
}
1021+
o_y += 1;
1022+
}
1023+
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BBRGB32) {
1024+
ColorRGB32 *dstptr, *srcptr;
1025+
o_y = offs_y;
1026+
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
1027+
o_x = offs_x;
1028+
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
1029+
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
1030+
BB_GET_PIXEL(src, sbb_rotation, ColorRGB32, o_x, o_y, &srcptr);
1031+
alpha = srcptr->alpha;
1032+
ainv = 0xFF - alpha;
1033+
dstptr->r = DIV_255(dstptr->r * ainv + srcptr->r * alpha);
1034+
dstptr->g = DIV_255(dstptr->g * ainv + srcptr->g * alpha);
1035+
dstptr->b = DIV_255(dstptr->b * ainv + srcptr->b * alpha);
1036+
//dstptr->alpha = dstptr->alpha;
1037+
o_x += 1;
1038+
}
1039+
o_y += 1;
1040+
}
1041+
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BBRGB24) {
1042+
ColorRGB32 *dstptr;
1043+
ColorRGB24 *srcptr;
1044+
o_y = offs_y;
1045+
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
1046+
o_x = offs_x;
1047+
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
1048+
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
1049+
BB_GET_PIXEL(src, sbb_rotation, ColorRGB24, o_x, o_y, &srcptr);
1050+
dstptr->r = srcptr->r;
1051+
dstptr->g = srcptr->g;
1052+
dstptr->b = srcptr->b;
1053+
//dstptr->alpha = dstptr->alpha;
1054+
o_x += 1;
1055+
}
1056+
o_y += 1;
1057+
}
1058+
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BB8) {
1059+
ColorRGB32 *dstptr;
1060+
Color8 *srcptr;
1061+
o_y = offs_y;
1062+
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
1063+
o_x = offs_x;
1064+
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
1065+
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
1066+
BB_GET_PIXEL(src, sbb_rotation, Color8, o_x, o_y, &srcptr);
1067+
dstptr->r = srcptr->a;
1068+
dstptr->g = srcptr->a;
1069+
dstptr->b = srcptr->a;
1070+
//dstptr->alpha = dstptr->alpha;
1071+
o_x += 1;
1072+
}
1073+
o_y += 1;
1074+
}
1075+
} else if (dbb_type == TYPE_BBRGB32 && sbb_type == TYPE_BB8A) {
1076+
ColorRGB32 *dstptr;
1077+
Color8A *srcptr;
1078+
uint8_t dsta, bdsta;
1079+
o_y = offs_y;
1080+
for (d_y = dest_y; d_y < dest_y + h; d_y++) {
1081+
o_x = offs_x;
1082+
for (d_x = dest_x; d_x < dest_x + w; d_x++) {
1083+
BB_GET_PIXEL(dst, dbb_rotation, ColorRGB32, d_x, d_y, &dstptr);
1084+
BB_GET_PIXEL(src, sbb_rotation, Color8A, o_x, o_y, &srcptr);
1085+
alpha = srcptr->alpha;
1086+
ainv = 0xFF - alpha;
1087+
dsta = RGB_To_A(dstptr->r, dstptr->g, dstptr->b);
1088+
bdsta = DIV_255(dsta * ainv + srcptr->a * alpha);
1089+
dstptr->r = bdsta;
1090+
dstptr->g = bdsta;
1091+
dstptr->b = bdsta;
1092+
//dstptr->alpha = dstptr->alpha;
1093+
o_x += 1;
1094+
}
1095+
o_y += 1;
1096+
}
10241097
} else {
10251098
fprintf(stderr, "incompatible bb (dst: %d, src: %d) in file %s, line %d!\r\n",
10261099
dbb_type, sbb_type, __FILE__, __LINE__); exit(1);

ffi-cdecl/lodepng_decl.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
#include "ffi-cdecl.h"
44

5-
cdecl_type(LodePNGColorType)
65
cdecl_enum(LodePNGColorType)
6+
cdecl_type(LodePNGColorType)
7+
cdecl_type(LodePNGDecompressSettings)
8+
cdecl_struct(LodePNGDecompressSettings)
9+
cdecl_struct(LodePNGDecoderSettings)
10+
cdecl_type(LodePNGDecoderSettings)
11+
cdecl_enum(LodePNGFilterStrategy)
12+
cdecl_type(LodePNGFilterStrategy)
13+
cdecl_type(LodePNGCompressSettings)
14+
cdecl_struct(LodePNGCompressSettings)
15+
cdecl_struct(LodePNGEncoderSettings)
16+
cdecl_type(LodePNGEncoderSettings)
17+
cdecl_struct(LodePNGColorMode)
18+
cdecl_type(LodePNGColorMode)
19+
cdecl_struct(LodePNGTime)
20+
cdecl_type(LodePNGTime)
21+
cdecl_struct(LodePNGInfo)
22+
cdecl_type(LodePNGInfo)
23+
cdecl_struct(LodePNGState)
24+
cdecl_type(LodePNGState)
725
cdecl_func(lodepng_error_text)
826
cdecl_func(lodepng_decode32_file)
927
cdecl_func(lodepng_decode32)
@@ -12,3 +30,9 @@ cdecl_func(lodepng_decode24)
1230
cdecl_func(lodepng_decode_memory)
1331
cdecl_func(lodepng_decode_file)
1432
cdecl_func(lodepng_encode32_file)
33+
cdecl_func(lodepng_state_init)
34+
cdecl_func(lodepng_state_cleanup)
35+
cdecl_func(lodepng_state_copy)
36+
cdecl_func(lodepng_decode)
37+
cdecl_func(lodepng_inspect)
38+
cdecl_func(lodepng_encode)

ffi/SDL2_0.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ function S.setWindowIcon(icon)
375375
if not icon then error("setWindowIcon: no icon path given") end
376376

377377
local Png = require("ffi/png")
378-
local ok, re = Png.decodeFromFile(icon)
378+
local ok, re = Png.decodeFromFile(icon, 4)
379379
if not ok then
380380
error(re.." ("..icon..")")
381381
end

ffi/framebuffer_linux.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ function framebuffer:clear(w, h)
249249
-- and regions of memory outside of the visible screen *may* be used by other things in the system,
250250
-- for performance reasons, and we do not want to screw them over ;).
251251
-- NOTE: This should be equivalent to line_length * yres
252-
C.memset(self.data, 0xFF, w * (self.fb_bpp / 8) * h)
252+
ffi.fill(self.data, w * (self.fb_bpp / 8) * h, 0xFF)
253253
end
254254
end
255255

0 commit comments

Comments
 (0)