1-
21// overwrites loadImg function to handle dicom sources.
2+
3+ // special: for 'sparse' tiles, show a debug overlay on render.
4+ showDebugTiles = false ;
35function DicomWebMods ( ) {
46 async function openSeries ( baseUrl , studyId , seriesId ) {
57 try {
@@ -55,11 +57,12 @@ function DicomWebMods() {
5557 }
5658 console . log ( instanceData ) ;
5759 // Transform result into OpenSeadragon-compatible format
58- const instanceResults = instanceData . map ( ( x ) => {
60+ var instanceResults = instanceData . map ( ( x ) => {
5961 try {
6062 let instanceHeight = x [ '00480007' ] ?. [ 'Value' ] ?. [ 0 ] ?? null ;
6163 let instanceWidth = x [ '00480006' ] ?. [ 'Value' ] ?. [ 0 ] ?? null ;
6264 let tileSize = x [ '00280010' ] ?. [ 'Value' ] ?. [ 0 ] ?? null ;
65+ console . info ( 'x, y' , x [ '00280010' ] ?. [ 'Value' ] ?. [ 0 ] , x [ '00280011' ] ?. [ 'Value' ] ?. [ 0 ] ) ;
6366 // instanceWidth = tileSize*Math.ceil(instanceWidth/tileSize)
6467 instanceHeight = tileSize * Math . ceil ( instanceHeight / tileSize ) ;
6568 let tileMap = { } ;
@@ -73,10 +76,12 @@ function DicomWebMods() {
7376 const item = frame [ '0048021A' ] ?. Value ?. [ 0 ] ;
7477 const col = item ?. [ '0048021E' ] ?. Value ?. [ 0 ] ;
7578 const row = item ?. [ '0048021F' ] ?. Value ?. [ 0 ] ;
79+ const physRow = item ?. [ '0040072A' ] ?. Value ?. [ 0 ] ;
80+ const physCol = item ?. [ '0040073A' ] ?. Value ?. [ 0 ] ;
7681 if ( col !== undefined && row !== undefined && tileSize ) {
77- const tileX = Math . floor ( col / tileSize ) ;
78- const tileY = Math . floor ( row / tileSize ) ;
79- tileMap [ `${ tileX } _${ tileY } ` ] = i + 1 ;
82+ const tileX = Math . floor ( ( col - 1 ) / tileSize ) ;
83+ const tileY = Math . floor ( ( row - 1 ) / tileSize ) ;
84+ tileMap [ `${ tileX } _${ tileY } ` ] = { 'idx' : i + 1 , 'col' : col - 1 , 'row' : row - 1 , 'physRow' : physRow , 'physCol' : physCol } ;
8085 // console.log(row, col, tileX, tileY, i+1)
8186 }
8287 }
@@ -125,6 +130,20 @@ function DicomWebMods() {
125130 item . order = index ;
126131 } ) ;
127132
133+ if ( showDebugTiles ) {
134+ let newInstanceResults = [ ] ;
135+
136+ for ( let item of instanceResults ) {
137+ // let item = instanceResults[x];
138+ newInstanceResults . push ( item ) ;
139+ let item2 = JSON . parse ( JSON . stringify ( item ) ) ;
140+ item2 . debug = true ;
141+ newInstanceResults . push ( item2 ) ;
142+ }
143+
144+ instanceResults = newInstanceResults ;
145+ }
146+
128147 // prep result for openseadragon
129148 let tilesources = instanceResults . map ( ( x ) => {
130149 return {
@@ -153,20 +172,46 @@ function DicomWebMods() {
153172 ) ;
154173 } ,
155174 getTileUrl : function ( level , xPos , yPos ) {
156- if ( level == x [ 'order' ] ) {
175+ const debugTile = x [ 'debug' ] || false ; // Toggle this to enable/disable debug mode
176+
177+ if ( level == x [ 'order' ] == 1 ) {
157178 const numRows = Math . ceil ( x [ 'height' ] / x [ 'tileSize' ] ) ;
158179 const numCols = Math . ceil ( x [ 'width' ] / x [ 'tileSize' ] ) ;
159- let numDir = numCols ;
160- let a = xPos ;
161- let b = yPos ;
162180
163181 if ( ! x [ 'tileMap' ] || Object . keys ( x [ 'tileMap' ] ) . length === 0 ) {
164- let frameIndex = b * numCols + a ;
182+ let frameIndex = yPos * numCols + xPos ;
165183 return `${ x [ 'url' ] } /frames/${ frameIndex + 1 } /rendered` ;
166- } else {
167- let tileIdx = x [ 'tileMap' ] [ `${ xPos } _${ yPos } ` ] ;
184+ } else if ( x [ 'tileMap' ] . hasOwnProperty ( `${ xPos } _${ yPos } ` ) ) {
185+ let tileIdx = x [ 'tileMap' ] [ `${ xPos } _${ yPos } ` ] [ 'idx' ] ;
186+ let tileRow = x [ 'tileMap' ] [ `${ xPos } _${ yPos } ` ] [ 'row' ] ;
187+ let tileCol = x [ 'tileMap' ] [ `${ xPos } _${ yPos } ` ] [ 'col' ] ;
188+ let physRow = x [ 'tileMap' ] [ `${ xPos } _${ yPos } ` ] [ 'physRow' ] ;
189+ let physCol = x [ 'tileMap' ] [ `${ xPos } _${ yPos } ` ] [ 'physCol' ] ;
168190 if ( tileIdx !== undefined ) {
169- return `${ x [ 'url' ] } /frames/${ tileIdx } /rendered` ;
191+ let tileUrl = `${ x [ 'url' ] } /frames/${ tileIdx } /rendered` ;
192+ let lgFont = 50 * ( x [ 'tileSize' ] / 1024 ) ;
193+ let smFont = 30 * ( x [ 'tileSize' ] / 1024 ) ;
194+ if ( debugTile ) {
195+ const svg = `
196+ <svg xmlns="http://www.w3.org/2000/svg" width="${ x [ 'tileSize' ] } " height="${ x [ 'tileSize' ] } ">
197+ <rect width="100%" height="100%" fill-opacity="0.5" fill="#ccc" stroke="#000" stroke-width="4"/>
198+ <text x="50%" y="20%" font-size="${ lgFont } " text-anchor="middle" fill="#000">idx: ${ tileIdx } </text>
199+ <text x="50%" y="30%" font-size="${ smFont } " text-anchor="middle" fill="#000">dcm R: ${ tileRow } </text>
200+ <text x="50%" y="35%" font-size="${ smFont } " text-anchor="middle" fill="#000">dcm C: ${ tileCol } </text>
201+ <text x="50%" y="40%" font-size="${ smFont } " text-anchor="middle" fill="#000">phys R: ${ physRow } </text>
202+ <text x="50%" y="45%" font-size="${ smFont } " text-anchor="middle" fill="#000">phys C: ${ physCol } </text>
203+ <text x="50%" y="60%" font-size="${ smFont } " text-anchor="middle" fill="#000">L: ${ level } </text>
204+ <text x="50%" y="65%" font-size="${ smFont } " text-anchor="middle" fill="#000">X: ${ xPos } </text>
205+ <text x="50%" y="70%" font-size="${ smFont } " text-anchor="middle" fill="#000">Y: ${ yPos } </text>
206+ </svg>
207+ ` ;
208+ const encoded = encodeURIComponent ( svg )
209+ . replace ( / ' / g, '%27' )
210+ . replace ( / " / g, '%22' ) ;
211+ return `data:image/svg+xml;charset=UTF-8,${ encoded } ` ;
212+ } else {
213+ return tileUrl ;
214+ }
170215 } else {
171216 return null ;
172217 }
@@ -175,9 +220,10 @@ function DicomWebMods() {
175220 return null ;
176221 }
177222 } ,
223+
178224 } ;
179225 } ) ;
180-
226+ console . log ( tilesources ) ;
181227 return tilesources ;
182228 } catch ( error ) {
183229 console . error ( 'Error in openSeries:' , error ) ;
0 commit comments