Skip to content

Commit

Permalink
added gamut table check
Browse files Browse the repository at this point in the history
check if the gamut table is valid before just blindly accepting it.
make parameters const.
  • Loading branch information
peterzuger committed Feb 1, 2021
1 parent 0dca477 commit d9db52d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions tlc5947/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,25 @@ rgb12 rgb8torgb12(rgb8 c){
return _c;
}

rgb12 rgb12_white_balance(rgb12 c, float m[3]){
rgb12 rgb12_white_balance(rgb12 c, const float m[3]){
rgb12 _c;
_c.r = (uint16_t)(c.r * m[0]);
_c.g = (uint16_t)(c.g * m[1]);
_c.b = (uint16_t)(c.b * m[2]);
return _c;
}

rgb12 rgb12_gamut(rgb12 c, float m[3][3]){

#define ADD_F(a, b) ((float)(((float)a)+((float)b)))
#define ADD3_F(a, b, c) ((float)(ADD_F(ADD_F(a, b), ((float)(c)))))
bool gamut_matrix_valid(const float m[3][3]){
for(uint8_t i = 0; i < 3; ++i)
if( ADD3_F(m[i][0], m[i][1], m[i][2]) > 1.0 )
return false;
return true;
}

rgb12 rgb12_gamut(rgb12 c, const float m[3][3]){
rgb12 _c;
_c.r = (uint16_t)((c.r * m[0][0]) + (c.g * m[0][1]) + (c.b * m[0][2]));
_c.g = (uint16_t)((c.r * m[1][0]) + (c.g * m[1][1]) + (c.b * m[1][2]));
Expand Down
6 changes: 4 additions & 2 deletions tlc5947/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define TLC5947_COLOR_H

#include <stdint.h>
#include <stdbool.h>

typedef struct{
uint16_t r:12; /*< red [0 -> 4095] */
Expand Down Expand Up @@ -74,9 +75,10 @@ rgb8 rgb12torgb8(rgb12 c)__attribute__ ((const));

rgb12 rgb8torgb12(rgb8 c)__attribute__ ((const));

rgb12 rgb12_white_balance(rgb12 c, float m[3]);
rgb12 rgb12_white_balance(rgb12 c, const float m[3]);

rgb12 rgb12_gamut(rgb12 c, float m[3][3]);
bool gamut_matrix_valid(const float m[3][3]);
rgb12 rgb12_gamut(rgb12 c, const float m[3][3]);

#if defined(__cplusplus)
}
Expand Down
8 changes: 8 additions & 0 deletions tlc5947/tlc5947.c
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,14 @@ STATIC mp_obj_t tlc5947_tlc5947_set_gamut(mp_obj_t self_in, mp_obj_t matrix_in){
}
}
}

if(!gamut_matrix_valid(self->gamut_m)){
memset(self->gamut_m, 0, 9 * sizeof(float));
for(uint32_t k = 0; k < 3; k++)
self->gamut_m[k][k] = 1.0;
mp_raise_ValueError(MP_ERROR_TEXT("invalid matrix"));
}

return mp_const_none;
}

Expand Down

0 comments on commit d9db52d

Please sign in to comment.