comparison Framework/Scene2D/Internals/CairoLookupTableTextureRenderer.cpp @ 1179:177e7d431cd1 broker

log scale in textures, remove redundant code for LUTs
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 20 Nov 2019 15:24:20 +0100
parents ba9db2ad317c
children 0ca50d275b9a
comparison
equal deleted inserted replaced
1178:3c7cdbf32e2a 1179:177e7d431cd1
35 const LookupTableTextureSceneLayer& l = dynamic_cast<const LookupTableTextureSceneLayer&>(layer); 35 const LookupTableTextureSceneLayer& l = dynamic_cast<const LookupTableTextureSceneLayer&>(layer);
36 36
37 textureTransform_ = l.GetTransform(); 37 textureTransform_ = l.GetTransform();
38 isLinearInterpolation_ = l.IsLinearInterpolation(); 38 isLinearInterpolation_ = l.IsLinearInterpolation();
39 39
40 const float a = l.GetMinValue();
41 float slope;
42
43 if (l.GetMinValue() >= l.GetMaxValue())
44 {
45 slope = 0;
46 }
47 else
48 {
49 slope = 256.0f / (l.GetMaxValue() - l.GetMinValue());
50 }
51
52 const Orthanc::ImageAccessor& source = l.GetTexture(); 40 const Orthanc::ImageAccessor& source = l.GetTexture();
53 const unsigned int width = source.GetWidth(); 41 const unsigned int width = source.GetWidth();
54 const unsigned int height = source.GetHeight(); 42 const unsigned int height = source.GetHeight();
55 texture_.SetSize(width, height, true /* alpha channel is enabled */); 43 texture_.SetSize(width, height, true /* alpha channel is enabled */);
56 44
57 Orthanc::ImageAccessor target; 45 Orthanc::ImageAccessor target;
58 texture_.GetWriteableAccessor(target); 46 texture_.GetWriteableAccessor(target);
59 47 l.Render(target);
60 const std::vector<uint8_t>& lut = l.GetLookupTable(); 48
61 if (lut.size() != 4 * 256)
62 {
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
64 }
65
66 assert(source.GetFormat() == Orthanc::PixelFormat_Float32 &&
67 target.GetFormat() == Orthanc::PixelFormat_BGRA32 &&
68 sizeof(float) == 4);
69
70 for (unsigned int y = 0; y < height; y++)
71 {
72 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y));
73 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
74
75 for (unsigned int x = 0; x < width; x++)
76 {
77 float v = (*p - a) * slope;
78 if (v <= 0)
79 {
80 v = 0;
81 }
82 else if (v >= 255)
83 {
84 v = 255;
85 }
86
87 if (1) //l.IsApplyLog())
88 {
89 // https://theailearner.com/2019/01/01/log-transformation/
90 v = 255.0f / log(1.0f + 255.0f * 1.5f) * log(1.0f + static_cast<float>(v));
91 if (v <= 0)
92 {
93 v = 0;
94 }
95 else if (v >= 255)
96 {
97 v = 255;
98 }
99 }
100
101 uint8_t vv = static_cast<uint8_t>(v);
102
103 q[0] = lut[4 * vv + 2]; // B
104 q[1] = lut[4 * vv + 1]; // G
105 q[2] = lut[4 * vv + 0]; // R
106 q[3] = lut[4 * vv + 3]; // A
107
108 p++;
109 q += 4;
110 }
111 }
112
113 cairo_surface_mark_dirty(texture_.GetObject()); 49 cairo_surface_mark_dirty(texture_.GetObject());
114 } 50 }
115 51
116 void CairoLookupTableTextureRenderer::Render(const AffineTransform2D& transform, 52 void CairoLookupTableTextureRenderer::Render(const AffineTransform2D& transform,
117 unsigned int canvasWidth, 53 unsigned int canvasWidth,