/// <summary> /// Get a strip-organized image with /// SamplesPerPixel > 1 /// PlanarConfiguration separated /// We assume that all such images are RGB. /// </summary> private static bool gtStripSeparate(TiffRgbaImage img, int[] raster, int offset, int width, int height) { int stripsize = img.tif.StripSize(); byte[] buf = new byte[(img.alpha != 0 ? 4 : 3) * stripsize]; int p0 = 0; int p1 = p0 + stripsize; int p2 = p1 + stripsize; int pa = p2 + stripsize; pa = (img.alpha != 0 ? (p2 + stripsize) : -1); int flip = img.setorientation(); int y; int rasterShift; if ((flip & FLIP_VERTICALLY) != 0) { y = height - 1; rasterShift = -(width + width); } else { y = 0; rasterShift = -(width - width); } FieldValue[] result = img.tif.GetFieldDefaulted(TiffTag.RowsPerStrip); int rowsperstrip = result[0].ToInt(); int scanline = img.tif.ScanlineSize(); int bufferShift = (width < img.width ? img.width - width : 0); bool ret = true; for (int row = 0; row < height; ) { int rowstoread = rowsperstrip - (row + img.row_offset) % rowsperstrip; int nrow = (row + rowstoread > height ? height - row : rowstoread); int offset_row = row + img.row_offset; if (img.tif.ReadEncodedStrip(img.tif.ComputeStrip(offset_row, 0), buf, p0, ((row + img.row_offset) % rowsperstrip + nrow) * scanline) < 0 && img.stoponerr) { ret = false; break; } if (img.tif.ReadEncodedStrip(img.tif.ComputeStrip(offset_row, 1), buf, p1, ((row + img.row_offset) % rowsperstrip + nrow) * scanline) < 0 && img.stoponerr) { ret = false; break; } if (img.tif.ReadEncodedStrip(img.tif.ComputeStrip(offset_row, 2), buf, p2, ((row + img.row_offset) % rowsperstrip + nrow) * scanline) < 0 && img.stoponerr) { ret = false; break; } if (img.alpha != 0) { if ((img.tif.ReadEncodedStrip(img.tif.ComputeStrip(offset_row, 3), buf, pa, ((row + img.row_offset) % rowsperstrip + nrow) * scanline) < 0 && img.stoponerr)) { ret = false; break; } } int pos = ((row + img.row_offset) % rowsperstrip) * scanline; img.putSeparate(img, raster, offset + y * width, rasterShift, 0, y, width, nrow, buf, p0 + pos, p1 + pos, p2 + pos, img.alpha != 0 ? (pa + pos) : -1, bufferShift); y += (flip & FLIP_VERTICALLY) != 0 ? -nrow : nrow; row += nrow; } if ((flip & FLIP_HORIZONTALLY) != 0) { for (int line = 0; line < height; line++) { int left = offset + line * width; int right = left + width - 1; while (left < right) { int temp = raster[left]; raster[left] = raster[right]; raster[right] = temp; left++; right--; } } } return ret; }
/// <summary> /// Get an tile-organized image that has /// SamplesPerPixel > 1 /// PlanarConfiguration separated /// We assume that all such images are RGB. /// </summary> private static bool gtTileSeparate(TiffRgbaImage img, int[] raster, int offset, int width, int height) { int tilesize = img.tif.TileSize(); byte[] buf = new byte[(img.alpha != 0 ? 4 : 3) * tilesize]; int p0 = 0; int p1 = p0 + tilesize; int p2 = p1 + tilesize; int pa = (img.alpha != 0 ? (p2 + tilesize) : -1); FieldValue[] result = img.tif.GetField(TiffTag.TileWidth); int tileWidth = result[0].ToInt(); result = img.tif.GetField(TiffTag.TileLength); int tileHeight = result[0].ToInt(); int flip = img.setorientation(); int y; int rasterShift; if ((flip & FLIP_VERTICALLY) != 0) { y = height - 1; rasterShift = -(tileWidth + width); } else { y = 0; rasterShift = -(tileWidth - width); } bool ret = true; for (int row = 0; row < height; ) { int rowstoread = tileHeight - (row + img.row_offset) % tileHeight; int nrow = (row + rowstoread > height ? height - row : rowstoread); for (int col = 0; col < width; col += tileWidth) { if (img.tif.ReadTile(buf, p0, col + img.col_offset, row + img.row_offset, 0, 0) < 0 && img.stoponerr) { ret = false; break; } if (img.tif.ReadTile(buf, p1, col + img.col_offset, row + img.row_offset, 0, 1) < 0 && img.stoponerr) { ret = false; break; } if (img.tif.ReadTile(buf, p2, col + img.col_offset, row + img.row_offset, 0, 2) < 0 && img.stoponerr) { ret = false; break; } if (img.alpha != 0) { if (img.tif.ReadTile(buf, pa, col + img.col_offset, row + img.row_offset, 0, 3) < 0 && img.stoponerr) { ret = false; break; } } int pos = ((row + img.row_offset) % tileHeight) * img.tif.TileRowSize(); if (col + tileWidth > width) { // Tile is clipped horizontally. // Calculate visible portion and skewing factors. int npix = width - col; int bufferShift = tileWidth - npix; img.putSeparate(img, raster, offset + y * width + col, rasterShift + bufferShift, col, y, npix, nrow, buf, p0 + pos, p1 + pos, p2 + pos, img.alpha != 0 ? (pa + pos) : -1, bufferShift); } else { img.putSeparate(img, raster, offset + y * width + col, rasterShift, col, y, tileWidth, nrow, buf, p0 + pos, p1 + pos, p2 + pos, img.alpha != 0 ? (pa + pos) : -1, 0); } } y += ((flip & FLIP_VERTICALLY) != 0 ? -nrow : nrow); row += nrow; } if ((flip & FLIP_HORIZONTALLY) != 0) { for (int line = 0; line < height; line++) { int left = offset + line * width; int right = left + width - 1; while (left < right) { int temp = raster[left]; raster[left] = raster[right]; raster[right] = temp; left++; right--; } } } return ret; }