@@ -297,22 +297,33 @@ export class GraphicMask extends PIXIContainer {
297297 * Update panoptic mask with a localised submask and a panoptic value
298298 * @param mask binary flatten array
299299 * @param box [l,t,r,b] search area
300- * @param newVal
300+ * @param newVal new pixel/class value (corresponds to color for rendering)
301301 */
302302 public updateByMaskInRoi ( mask : Float32Array , box : [ number , number , number , number ] ,
303303 newVal : [ number , number , number ] , fillType : 'add' | 'remove' = 'add' ) {
304304 const pixels = this . ctx . getImageData ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
305+
306+ //respect image boundaries
305307 const width = box [ 2 ] - box [ 0 ] ;
306- const height = box [ 3 ] - box [ 1 ] ;
308+ const roi = [ ( box [ 0 ] > 0 ) ? box [ 0 ] : 0 ,
309+ ( box [ 1 ] > 0 ) ? box [ 1 ] : 0 ,
310+ ( box [ 2 ] < this . canvas . width ) ? box [ 2 ] : this . canvas . width ,
311+ ( box [ 3 ] < this . canvas . height ) ? box [ 3 ] : this . canvas . height ] ;
312+ const widthroi = roi [ 2 ] - roi [ 0 ] ;
313+ const heightroi = roi [ 3 ] - roi [ 1 ] ;
314+ const decx = ( box [ 0 ] < 0 ) ? - box [ 0 ] : 0 ;
315+ const decy = ( box [ 1 ] < 0 ) ? - box [ 1 ] : 0 ;
316+
317+
307318 const color = this . pixelToColor ( ...newVal ) ;
308319 const alpha = ( Math . max ( ...color ) === 0 ) ? 0 : MASK_ALPHA_VALUE ;
309320 const [ id1 , id2 , cls ] = newVal ;
310321 if ( fillType === 'add' ) {
311- for ( let x = 0 ; x < width ; x ++ ) {
312- for ( let y = 0 ; y < height ; y ++ ) {
313- const idx = ( x + box [ 0 ] + ( y + box [ 1 ] ) * this . canvas . width ) ;
322+ for ( let x = 0 ; x < widthroi ; x ++ ) {
323+ for ( let y = 0 ; y < heightroi ; y ++ ) {
324+ const idx = ( x + roi [ 0 ] + ( y + roi [ 1 ] ) * this . canvas . width ) ;
314325 const pixId = this . pixelId ( idx ) ;
315- if ( mask [ y * width + x ] === 1 && ! this . lockedInstances . has ( fuseId ( pixId ) ) ) {
326+ if ( mask [ ( y + decy ) * width + ( x + decx ) ] === 1 && ! this . lockedInstances . has ( fuseId ( pixId ) ) ) {
316327 this . orig ! . data [ 4 * idx ] = id1 ;
317328 this . orig ! . data [ 4 * idx + 1 ] = id2 ;
318329 this . orig ! . data [ 4 * idx + 2 ] = cls ;
@@ -326,11 +337,11 @@ export class GraphicMask extends PIXIContainer {
326337 }
327338 } else if ( fillType === 'remove' ) {
328339 const fusedVal = fuseId ( newVal ) ;
329- for ( let x = 0 ; x < width ; x ++ ) {
330- for ( let y = 0 ; y < height ; y ++ ) {
331- const idx = ( x + box [ 0 ] + ( y + box [ 1 ] ) * this . canvas . width ) ;
340+ for ( let x = 0 ; x < widthroi ; x ++ ) {
341+ for ( let y = 0 ; y < heightroi ; y ++ ) {
342+ const idx = ( x + roi [ 0 ] + ( y + roi [ 1 ] ) * this . canvas . width ) ;
332343 const pixId = this . pixelId ( idx ) ;
333- if ( mask [ y * width + x ] === 1 && ! this . lockedInstances . has ( fuseId ( pixId ) )
344+ if ( mask [ ( y + decy ) * width + ( x + decx ) ] === 1 && ! this . lockedInstances . has ( fuseId ( pixId ) )
334345 && fuseId ( pixId ) == fusedVal ) {
335346 this . orig ! . data [ 4 * idx ] = 0 ;
336347 this . orig ! . data [ 4 * idx + 1 ] = 0 ;
@@ -350,13 +361,19 @@ export class GraphicMask extends PIXIContainer {
350361
351362 /**
352363 * Update panoptic mask with a polygon to be filled with given panoptic value
353- * @param polygon
354- * @param id
355- * @param fillType
364+ * @param polygon an array of points (points must be represented by integers)
365+ * @param id new pixel/class value (corresponds to color for rendering)
366+ * @param fillType 'add' or 'remove'
356367 */
357- public updateByPolygon ( polygon : Point [ ] , id : [ number , number , number ] , fillType = 'add' ) {
368+ public updateByPolygon ( polygon : Point [ ] , id : [ number , number , number ] , fillType : 'add' | 'remove' = 'add' ) {
358369 const pixels = this . ctx . getImageData ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
359- const [ xMin , yMin , xMax , yMax ] = getPolygonExtrema ( polygon ) ;
370+ let [ xMin , yMin , xMax , yMax ] = getPolygonExtrema ( polygon ) ;
371+ //respect image boundaries
372+ if ( xMin < 0 ) xMin = 0 ;
373+ if ( yMin < 0 ) yMin = 0 ;
374+ if ( xMax > this . canvas . width ) xMax = this . canvas . width - 1 ;
375+ if ( yMax > this . canvas . height ) yMax = this . canvas . height - 1 ;
376+
360377 if ( this . lockedInstances . has ( fuseId ( id ) ) ) {
361378 // do not update locked instances
362379 return ;
0 commit comments