@@ -111,6 +111,7 @@ type ifd struct {
111111
112112 ntags uint64
113113 ntilesx , ntilesy uint64
114+ nplanes uint64 //1 if PlanarConfiguration==1, SamplesPerPixel if PlanarConfiguration==2
114115 tagsSize uint64
115116 strileSize uint64
116117 r tiff.BReader
@@ -163,10 +164,11 @@ func (ifd *ifd) AddMask(msk *ifd) error {
163164 return nil
164165}
165166
166- func (ifd * ifd ) structure (bigtiff bool ) (tagCount , ifdSize , strileSize uint64 ) {
167+ func (ifd * ifd ) structure (bigtiff bool ) (tagCount , ifdSize , strileSize , planeCount uint64 ) {
167168 cnt := uint64 (0 )
168169 size := uint64 (16 ) //8 for field count + 8 for next ifd offset
169170 tagSize := uint64 (20 )
171+ planeCount = 1
170172 if ! bigtiff {
171173 size = 6 // 2 for field count + 4 for next ifd offset
172174 tagSize = 12
@@ -209,6 +211,9 @@ func (ifd *ifd) structure(bigtiff bool) (tagCount, ifdSize, strileSize uint64) {
209211 cnt ++
210212 size += tagSize
211213 }
214+ if ifd .PlanarConfiguration == 2 {
215+ planeCount = uint64 (ifd .SamplesPerPixel )
216+ }
212217 if len (ifd .DateTime ) > 0 {
213218 cnt ++
214219 size += arrayFieldSize (ifd .DateTime , bigtiff )
@@ -295,7 +300,7 @@ func (ifd *ifd) structure(bigtiff bool) (tagCount, ifdSize, strileSize uint64) {
295300 cnt ++
296301 size += arrayFieldSize (ifd .RPCs , bigtiff )
297302 }
298- return cnt , size , strileSize
303+ return cnt , size , strileSize , planeCount
299304}
300305
301306type tagData struct {
@@ -366,13 +371,14 @@ const (
366371func (cog * cog ) computeStructure () {
367372 ifd := cog .ifd
368373 for ifd != nil {
369- ifd .ntags , ifd .tagsSize , ifd .strileSize = ifd .structure (cog .bigtiff )
374+ ifd .ntags , ifd .tagsSize , ifd .strileSize , ifd . nplanes = ifd .structure (cog .bigtiff )
370375 //ifd.ntilesx = uint64(math.Ceil(float64(ifd.ImageWidth) / float64(ifd.TileWidth)))
371376 //ifd.ntilesy = uint64(math.Ceil(float64(ifd.ImageLength) / float64(ifd.TileLength)))
372377 ifd .ntilesx = (ifd .ImageWidth + uint64 (ifd .TileWidth ) - 1 ) / uint64 (ifd .TileWidth )
373378 ifd .ntilesy = (ifd .ImageLength + uint64 (ifd .TileLength ) - 1 ) / uint64 (ifd .TileLength )
379+
374380 for _ , mifd := range ifd .masks {
375- mifd .ntags , mifd .tagsSize , mifd .strileSize = mifd .structure (cog .bigtiff )
381+ mifd .ntags , mifd .tagsSize , mifd .strileSize , mifd . nplanes = mifd .structure (cog .bigtiff )
376382 // mifd.ntilesx = uint64(math.Ceil(float64(mifd.ImageWidth) / float64(mifd.TileWidth)))
377383 // mifd.ntilesy = uint64(math.Ceil(float64(mifd.ImageLength) / float64(mifd.TileLength)))
378384 mifd .ntilesx = (mifd .ImageWidth + uint64 (mifd .TileWidth ) - 1 ) / uint64 (mifd .TileWidth )
@@ -425,7 +431,7 @@ func (cog *cog) computeImageryOffsets() error {
425431 datas := cog .dataInterlacing ()
426432 tiles := datas .tiles ()
427433 for tile := range tiles {
428- tileidx := tile .x + tile .y * tile .ifd .ntilesx
434+ tileidx := ( tile .x + tile .y * tile .ifd .ntilesx ) * tile . ifd . nplanes + tile . plane
429435 cnt := uint64 (tile .ifd .TileByteCounts [tileidx ])
430436 if cnt > 0 {
431437 if cog .bigtiff {
@@ -508,7 +514,7 @@ func (cog *cog) write(out io.Writer) error {
508514 buf := & bytes.Buffer {}
509515 for tile := range tiles {
510516 buf .Reset ()
511- idx := tile .x + tile .y * tile .ifd .ntilesx
517+ idx := ( tile .x + tile .y * tile .ifd .ntilesx ) * tile . ifd . nplanes + tile . plane
512518 if tile .ifd .TileByteCounts [idx ] > 0 {
513519 _ , err := tile .ifd .r .Seek (int64 (tile .ifd .OriginalTileOffsets [idx ]), io .SeekStart )
514520 if err != nil {
@@ -787,8 +793,9 @@ func (cog *cog) writeIFD(w io.Writer, ifd *ifd, offset uint64, striledata *tagDa
787793}
788794
789795type tile struct {
790- ifd * ifd
791- x , y uint64
796+ ifd * ifd
797+ x , y uint64
798+ plane uint64
792799}
793800
794801type datas [][]* ifd
@@ -822,10 +829,13 @@ func (d datas) tiles() chan tile {
822829 for y := uint64 (0 ); y < ovr [0 ].ntilesy ; y ++ {
823830 for x := uint64 (0 ); x < ovr [0 ].ntilesx ; x ++ {
824831 for _ , ifd := range ovr {
825- ch <- tile {
826- ifd : ifd ,
827- x : x ,
828- y : y ,
832+ for p := uint64 (0 ); p < ifd .nplanes ; p ++ {
833+ ch <- tile {
834+ ifd : ifd ,
835+ plane : p ,
836+ x : x ,
837+ y : y ,
838+ }
829839 }
830840 }
831841 }
0 commit comments