From 719dcaefe09ecf538546a54cc8e0a9646f7cb17e Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Wed, 3 Jan 2018 21:00:57 -0500 Subject: magicard: Implement gamma correction. The output looks pretty decent now! --- backend_magicard.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/backend_magicard.c b/backend_magicard.c index 150ebd3..6a60d8a 100644 --- a/backend_magicard.c +++ b/backend_magicard.c @@ -47,6 +47,66 @@ #define USB_VID_MAGICARD 0x0C1F #define USB_PID_MAGICARD_TANGO2E 0x1800 +/* Gamma tables computed with this perl program: + + my $input_bpp = 8; + my $output_bpp = 6; + my $gamma = 1/1.8; # or 1/2.2 or whatever. + + my $i; + + for (my $i = 0 ; $i < (2 ** $input_bpp) ; $i++) { + my $linear = $i / (2 ** $input_bpp); + my $gc = ($linear ** $gamma) * (2 ** $output_bpp); + $gc = int($gc); + print "$gc, "; + } + +*/ + +static uint8_t gammas[2][256] = { + /* Gamma = 2.2 */ + { + 0, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 15, 16, 17, + 17, 18, 18, 19, 19, 20, 20, 20, 21, 21, 22, 22, 23, 23, 23, + 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, + 29, 29, 29, 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, + 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36, + 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, + 39, 39, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42, + 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, + 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, + 47, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 50, + 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 52, 52, + 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, + 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, + 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58, + 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, + 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 61, 61, 62, + 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63, + }, + /* Gamma = 1.8 */ + { + 0, 2, 4, 5, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12, + 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 19, + 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, + 24, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28, + 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 32, + 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, + 35, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38, + 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, + 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 44, + 44, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, + 47, 47, 47, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, + 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 52, 52, 52, + 52, 52, 52, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, + 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 56, 57, 57, + 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59, + 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, + 61, 61, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, + } +}; + /* Private data structure */ struct magicard_ctx { struct libusb_device_handle *dev; @@ -397,9 +457,18 @@ static void downscale_and_extract(int gamma, uint32_t pixels, uint8_t b_shift; /* Downscale color planes from 8bpp -> 6bpp; */ - y = *y_i++ >> 2; - m = *m_i++ >> 2; - c = *c_i++ >> 2; + if (gamma) { + if (gamma > 2) + gamma = 2; + gamma--; + y = gammas[gamma][*y_i++]; + m = gammas[gamma][*m_i++]; + c = gammas[gamma][*c_i++]; + } else { + y = *y_i++ >> 2; + m = *m_i++ >> 2; + c = *c_i++ >> 2; + } /* Extract "true black" from ymc data, if enabled */ if (k_o && y == 0x3f && m == 0x3f && c == 0x3f) { @@ -763,7 +832,7 @@ static int magicard_cmdline_arg(void *vctx, int argc, char **argv) struct dyesub_backend magicard_backend = { .name = "Magicard family", - .version = "0.07", + .version = "0.08", .uri_prefix = "magicard", .cmdline_arg = magicard_cmdline_arg, .cmdline_usage = magicard_cmdline, @@ -913,7 +982,7 @@ struct dyesub_backend magicard_backend = { Commands consumed by backend: - ICC%d Gamma curve (0, 1, 2) -- NOT IMPLEMENTED YET + ICC%d Gamma curve (0, 1, 2) -- off, 2.2, or 1.8 respectively. X-GP-8 Raw data is 8bpp. needs to be converted. X-GP-RK Extract K channel from color data. -- cgit v1.2.1