Skip to content

Commit

Permalink
Change getPixel to actually transfer a full pixel with all channels
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaustein committed Feb 21, 2024
1 parent 4cb242b commit 2fa6ece
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/tiv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ void printCodepoint(int codepoint) {

void printImage(const cimg_library::CImg<unsigned char> &image,
const int &flags) {
GetPixelFunction get_pixel = [&](int x, int y,
int channel) -> unsigned char {
return image(x, y, 0, channel);
GetPixelFunction get_pixel = [&](int x, int y) -> unsigned long {
return (((unsigned long) image(x, y, 0, 0)) << 16)
| (((unsigned long) image(x, y, 0, 1)) << 8)
| (((unsigned long) image(x, y, 0, 2)));
};

CharData lastCharData;
Expand Down
15 changes: 11 additions & 4 deletions src/tiv_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ constexpr unsigned int BITMAPS[] = {
0, 1 // End marker for extended TELETEXT mode.
};

// The channel indices are 0, 1, 2 for R, G, B
unsigned char get_channel(unsigned long rgb, int index) {
return (unsigned char) ((rgb >> ((2 - index) * 8)) & 255);
}

CharData createCharData(GetPixelFunction get_pixel, int x0, int y0,
int codepoint, int pattern) {
Expand All @@ -186,8 +190,9 @@ CharData createCharData(GetPixelFunction get_pixel, int x0, int y0,
avg = result.bgColor.data();
bg_count++;
}
long rgb = get_pixel(x0 + x, y0 + y);
for (int i = 0; i < 3; i++) {
avg[i] += get_pixel(x0 + x, y0 + y, i);
avg[i] += get_channel(rgb, i);
}
mask = mask >> 1;
}
Expand Down Expand Up @@ -215,8 +220,9 @@ CharData findCharData(GetPixelFunction get_pixel, int x0, int y0,
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 4; x++) {
long color = 0;
long rgb = get_pixel(x0 + x, y0 + y);
for (int i = 0; i < 3; i++) {
int d = get_pixel(x0 + x, y0 + y, i);
int d = get_channel(rgb, i);
min[i] = std::min(min[i], d);
max[i] = std::max(max[i], d);
color = (color << 8) | d;
Expand Down Expand Up @@ -248,11 +254,12 @@ CharData findCharData(GetPixelFunction get_pixel, int x0, int y0,
bits = bits << 1;
int d1 = 0;
int d2 = 0;
unsigned long rgb = get_pixel(x0 + x, y0 + y);
for (int i = 0; i < 3; i++) {
int shift = 16 - 8 * i;
int c1 = (max_count_color_1 >> shift) & 255;
int c2 = (max_count_color_2 >> shift) & 255;
int c = get_pixel(x0 + x, y0 + y, i);
int c = get_channel(rgb, i);
d1 += (c1 - c) * (c1 - c);
d2 += (c2 - c) * (c2 - c);
}
Expand Down Expand Up @@ -281,7 +288,7 @@ CharData findCharData(GetPixelFunction get_pixel, int x0, int y0,
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 4; x++) {
bits = bits << 1;
if (get_pixel(x0 + x, y0 + y, splitIndex) > splitValue) {
if (get_channel(get_pixel(x0 + x, y0 + y), splitIndex) > splitValue) {
bits |= 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tiv_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ constexpr int GRAYSCALE_STEPS[GRAYSCALE_STEP_COUNT] = {
0x80, 0x8a, 0x94, 0x9e, 0xa8, 0xb2, 0xbc, 0xc6, 0xd0, 0xda, 0xe4, 0xee};


typedef std::function<unsigned char(int, int, int)> GetPixelFunction;
typedef std::function<unsigned long(int, int)> GetPixelFunction;

int clamp_byte(int value);

Expand Down

0 comments on commit 2fa6ece

Please sign in to comment.