Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] DjVu gamma correction #921

Merged
merged 3 commits into from
Jun 10, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions djvu.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "koptcrop.h"
#include "djvu.h"

#define ABS(x) ((x<0)?(-x):(x))

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

Expand Down Expand Up @@ -654,6 +656,20 @@ static int drawPage(lua_State *L) {
BlitBuffer *bb = (BlitBuffer*) lua_topointer(L, 3);
ddjvu_render_mode_t djvu_render_mode = (int) luaL_checkint(L, 6);
ddjvu_rect_t pagerect, renderrect;
// map KOReader gamma to djvulibre gamma
// djvulibre goes from 0.5 to 5.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when you give it < 0.5 or > 5.0 ? which can happen if one hacked koptoptions.lua to extend boundary values.

Which it seems I once did, I have that in my own patch:

                 name_text = S.CONTRAST,
                 buttonprogress = true,
-                values = {1/0.8, 1/1.0, 1/1.5, 1/2.0, 1/3.0, 1/4.0, 1/6.0, 1/9.0},
+                -- for pdf reflowing mode (kopt_contrast):
+                values = {1/0.8, 1/1.0, 1/1.5, 1/2.0, 1/4.0, 1/6.0, 1/10.0, 1/50.0},
                 default_pos = 2,
                 default_value = DKOPTREADER_CONFIG_CONTRAST,
                 event = "GammaUpdate",
-                args = {0.8, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, 9.0},
-                labels = {0.8, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, 9.0},
+                -- for pdf non-reflowing mode (mupdf):
+                args =   {0.8, 1.0, 1.5, 2.0, 4.0, 6.0, 10.0, 50.0},
+                labels = {0.8, 1.0, 1.5, 2.0, 4.0, 6.0, 10.0, 50.0},
+                -- see https://github.com/koreader/koreader/issues/1299#issuecomment-65183895
                 name_text_hold_callback = optionsutil.showValues,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I mean: I don't mind having my higher values limited and causing no change from previous values - just want to be sure there could be no crash or strange things.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

djvulibre ignores out of bound values.

https://gitlab.com/koreader/djvulibre/blob/9658b01431cd7ff6344d7787f855179e73fe81a7/libdjvu/ddjvuapi.cpp#L2166

(One wonders if it wouldn't make more sense to cap them instead of to ignore them completely, but that aside.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wouldn't make more sense to cap them instead of to ignore them

I think you should cap them, after your gamma = A - B*gamma s, as they could give out of bound values, so better to have them capped that suddenly getting back with no gamma change.

double gamma = ABS(dc->gamma); // not sure why, but 1 is given as -1?
if (gamma == 2) {
// default
gamma = 2.2;
} else if (gamma < 2) {
// with this function, 0.8 = 5, 2 = 2.2
gamma = 6.86666 - 2.33333 * gamma;
} else if (gamma > 2) {
// with this function, 9 = 0.5, 2 = 2.2
gamma = 2.68571 - 0.242856 * gamma;
}
ddjvu_format_set_gamma(page->doc->pixelformat, gamma);
int bbsize = (bb->w)*(bb->h)*page->doc->pixelsize;
uint8_t *imagebuffer = bb->data;

Expand Down Expand Up @@ -692,23 +708,6 @@ static int drawPage(lua_State *L) {
if (!ddjvu_page_render(page->page_ref, djvu_render_mode, &pagerect, &renderrect, page->doc->pixelformat, bb->w*page->doc->pixelsize, imagebuffer))
memset(imagebuffer, 0xFF, bbsize);

/* Gamma correction of the blitbuffer, do we need to build this into BlitBuffer? */
unsigned char gamma_map[256];
unsigned char *s = imagebuffer;
int k, x, y;

if (dc->gamma != -1.0) {
for (k = 0; k < 256; k++)
gamma_map[k] = pow(k / 255.0f, dc->gamma) * 255;

for (y = 0; y < bb->h; y++) {
for (x = 0; x < bb->w; x++) {
k = y*bb->pitch + x;
s[k] = gamma_map[s[k]];
}
}
}

return 0;
}

Expand Down