Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Images/ImageAccessor.cpp @ 4278:9279de56a405
avoid multiple calls to GetWidth() and GetHeight() on pixel loops
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 20:05:55 +0100 |
parents | 0034f855c023 |
children | b30a8de92ad9 |
comparison
equal
deleted
inserted
replaced
4277:c5ca798b158a | 4278:9279de56a405 |
---|---|
39 static void ToMatlabStringInternal(ChunkedBuffer& target, | 39 static void ToMatlabStringInternal(ChunkedBuffer& target, |
40 const ImageAccessor& source) | 40 const ImageAccessor& source) |
41 { | 41 { |
42 target.AddChunk("double([ "); | 42 target.AddChunk("double([ "); |
43 | 43 |
44 for (unsigned int y = 0; y < source.GetHeight(); y++) | 44 const unsigned int width = source.GetWidth(); |
45 const unsigned int height = source.GetHeight(); | |
46 | |
47 for (unsigned int y = 0; y < height; y++) | |
45 { | 48 { |
46 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); | 49 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); |
47 | 50 |
48 std::string s; | 51 std::string s; |
49 if (y > 0) | 52 if (y > 0) |
50 { | 53 { |
51 s = "; "; | 54 s = "; "; |
52 } | 55 } |
53 | 56 |
54 s.reserve(source.GetWidth() * 8); | 57 s.reserve(width * 8); |
55 | 58 |
56 for (unsigned int x = 0; x < source.GetWidth(); x++, p++) | 59 for (unsigned int x = 0; x < width; x++, p++) |
57 { | 60 { |
58 s += boost::lexical_cast<std::string>(static_cast<double>(*p)) + " "; | 61 s += boost::lexical_cast<std::string>(static_cast<double>(*p)) + " "; |
59 } | 62 } |
60 | 63 |
61 target.AddChunk(s); | 64 target.AddChunk(s); |
70 { | 73 { |
71 assert(source.GetFormat() == PixelFormat_RGB24); | 74 assert(source.GetFormat() == PixelFormat_RGB24); |
72 | 75 |
73 target.AddChunk("double(permute(reshape([ "); | 76 target.AddChunk("double(permute(reshape([ "); |
74 | 77 |
75 for (unsigned int y = 0; y < source.GetHeight(); y++) | 78 const unsigned int width = source.GetWidth(); |
79 const unsigned int height = source.GetHeight(); | |
80 | |
81 for (unsigned int y = 0; y < height; y++) | |
76 { | 82 { |
77 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 83 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
78 | 84 |
79 std::string s; | 85 std::string s; |
80 s.reserve(source.GetWidth() * 3 * 8); | 86 s.reserve(width * 3 * 8); |
81 | 87 |
82 for (unsigned int x = 0; x < 3 * source.GetWidth(); x++, p++) | 88 for (unsigned int x = 0; x < 3 * width; x++, p++) |
83 { | 89 { |
84 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " "; | 90 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " "; |
85 } | 91 } |
86 | 92 |
87 target.AddChunk(s); | 93 target.AddChunk(s); |
88 } | 94 } |
89 | 95 |
90 target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(source.GetHeight()) + | 96 target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(height) + |
91 " " + boost::lexical_cast<std::string>(source.GetWidth()) + " ]), [ 3 2 1 ]))"); | 97 " " + boost::lexical_cast<std::string>(width) + " ]), [ 3 2 1 ]))"); |
92 } | 98 } |
93 | 99 |
94 | 100 |
95 void* ImageAccessor::GetBuffer() | 101 void* ImageAccessor::GetBuffer() |
96 { | 102 { |