You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

reader.go 18 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate go run gen.go
  5. // Package ccitt implements a CCITT (fax) image decoder.
  6. package ccitt
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "image"
  11. "io"
  12. "math/bits"
  13. )
  14. var (
  15. errInvalidBounds = errors.New("ccitt: invalid bounds")
  16. errInvalidCode = errors.New("ccitt: invalid code")
  17. errInvalidMode = errors.New("ccitt: invalid mode")
  18. errInvalidOffset = errors.New("ccitt: invalid offset")
  19. errMissingEOL = errors.New("ccitt: missing End-of-Line")
  20. errRunLengthOverflowsWidth = errors.New("ccitt: run length overflows width")
  21. errRunLengthTooLong = errors.New("ccitt: run length too long")
  22. errUnsupportedMode = errors.New("ccitt: unsupported mode")
  23. errUnsupportedSubFormat = errors.New("ccitt: unsupported sub-format")
  24. errUnsupportedWidth = errors.New("ccitt: unsupported width")
  25. )
  26. // Order specifies the bit ordering in a CCITT data stream.
  27. type Order uint32
  28. const (
  29. // LSB means Least Significant Bits first.
  30. LSB Order = iota
  31. // MSB means Most Significant Bits first.
  32. MSB
  33. )
  34. // SubFormat represents that the CCITT format consists of a number of
  35. // sub-formats. Decoding or encoding a CCITT data stream requires knowing the
  36. // sub-format context. It is not represented in the data stream per se.
  37. type SubFormat uint32
  38. const (
  39. Group3 SubFormat = iota
  40. Group4
  41. )
  42. // Options are optional parameters.
  43. type Options struct {
  44. // Align means that some variable-bit-width codes are byte-aligned.
  45. Align bool
  46. // Invert means that black is the 1 bit or 0xFF byte, and white is 0.
  47. Invert bool
  48. }
  49. // maxWidth is the maximum (inclusive) supported width. This is a limitation of
  50. // this implementation, to guard against integer overflow, and not anything
  51. // inherent to the CCITT format.
  52. const maxWidth = 1 << 20
  53. func invertBytes(b []byte) {
  54. for i, c := range b {
  55. b[i] = ^c
  56. }
  57. }
  58. func reverseBitsWithinBytes(b []byte) {
  59. for i, c := range b {
  60. b[i] = bits.Reverse8(c)
  61. }
  62. }
  63. // highBits writes to dst (1 bit per pixel, most significant bit first) the
  64. // high (0x80) bits from src (1 byte per pixel). It returns the number of bytes
  65. // written and read such that dst[:d] is the packed form of src[:s].
  66. //
  67. // For example, if src starts with the 8 bytes [0x7D, 0x7E, 0x7F, 0x80, 0x81,
  68. // 0x82, 0x00, 0xFF] then 0x1D will be written to dst[0].
  69. //
  70. // If src has (8 * len(dst)) or more bytes then only len(dst) bytes are
  71. // written, (8 * len(dst)) bytes are read, and invert is ignored.
  72. //
  73. // Otherwise, if len(src) is not a multiple of 8 then the final byte written to
  74. // dst is padded with 1 bits (if invert is true) or 0 bits. If inverted, the 1s
  75. // are typically temporary, e.g. they will be flipped back to 0s by an
  76. // invertBytes call in the highBits caller, reader.Read.
  77. func highBits(dst []byte, src []byte, invert bool) (d int, s int) {
  78. // Pack as many complete groups of 8 src bytes as we can.
  79. n := len(src) / 8
  80. if n > len(dst) {
  81. n = len(dst)
  82. }
  83. dstN := dst[:n]
  84. for i := range dstN {
  85. src8 := src[i*8 : i*8+8]
  86. dstN[i] = ((src8[0] & 0x80) >> 0) |
  87. ((src8[1] & 0x80) >> 1) |
  88. ((src8[2] & 0x80) >> 2) |
  89. ((src8[3] & 0x80) >> 3) |
  90. ((src8[4] & 0x80) >> 4) |
  91. ((src8[5] & 0x80) >> 5) |
  92. ((src8[6] & 0x80) >> 6) |
  93. ((src8[7] & 0x80) >> 7)
  94. }
  95. d, s = n, 8*n
  96. dst, src = dst[d:], src[s:]
  97. // Pack up to 7 remaining src bytes, if there's room in dst.
  98. if (len(dst) > 0) && (len(src) > 0) {
  99. dstByte := byte(0)
  100. if invert {
  101. dstByte = 0xFF >> uint(len(src))
  102. }
  103. for n, srcByte := range src {
  104. dstByte |= (srcByte & 0x80) >> uint(n)
  105. }
  106. dst[0] = dstByte
  107. d, s = d+1, s+len(src)
  108. }
  109. return d, s
  110. }
  111. type bitReader struct {
  112. r io.Reader
  113. // readErr is the error returned from the most recent r.Read call. As the
  114. // io.Reader documentation says, when r.Read returns (n, err), "always
  115. // process the n > 0 bytes returned before considering the error err".
  116. readErr error
  117. // order is whether to process r's bytes LSB first or MSB first.
  118. order Order
  119. // The high nBits bits of the bits field hold upcoming bits in MSB order.
  120. bits uint64
  121. nBits uint32
  122. // bytes[br:bw] holds bytes read from r but not yet loaded into bits.
  123. br uint32
  124. bw uint32
  125. bytes [1024]uint8
  126. }
  127. func (b *bitReader) alignToByteBoundary() {
  128. n := b.nBits & 7
  129. b.bits <<= n
  130. b.nBits -= n
  131. }
  132. // nextBitMaxNBits is the maximum possible value of bitReader.nBits after a
  133. // bitReader.nextBit call, provided that bitReader.nBits was not more than this
  134. // value before that call.
  135. //
  136. // Note that the decode function can unread bits, which can temporarily set the
  137. // bitReader.nBits value above nextBitMaxNBits.
  138. const nextBitMaxNBits = 31
  139. func (b *bitReader) nextBit() (uint64, error) {
  140. for {
  141. if b.nBits > 0 {
  142. bit := b.bits >> 63
  143. b.bits <<= 1
  144. b.nBits--
  145. return bit, nil
  146. }
  147. if available := b.bw - b.br; available >= 4 {
  148. // Read 32 bits, even though b.bits is a uint64, since the decode
  149. // function may need to unread up to maxCodeLength bits, putting
  150. // them back in the remaining (64 - 32) bits. TestMaxCodeLength
  151. // checks that the generated maxCodeLength constant fits.
  152. //
  153. // If changing the Uint32 call, also change nextBitMaxNBits.
  154. b.bits = uint64(binary.BigEndian.Uint32(b.bytes[b.br:])) << 32
  155. b.br += 4
  156. b.nBits = 32
  157. continue
  158. } else if available > 0 {
  159. b.bits = uint64(b.bytes[b.br]) << (7 * 8)
  160. b.br++
  161. b.nBits = 8
  162. continue
  163. }
  164. if b.readErr != nil {
  165. return 0, b.readErr
  166. }
  167. n, err := b.r.Read(b.bytes[:])
  168. b.br = 0
  169. b.bw = uint32(n)
  170. b.readErr = err
  171. if b.order != MSB {
  172. reverseBitsWithinBytes(b.bytes[:b.bw])
  173. }
  174. }
  175. }
  176. func decode(b *bitReader, decodeTable [][2]int16) (uint32, error) {
  177. nBitsRead, bitsRead, state := uint32(0), uint64(0), int32(1)
  178. for {
  179. bit, err := b.nextBit()
  180. if err != nil {
  181. return 0, err
  182. }
  183. bitsRead |= bit << (63 - nBitsRead)
  184. nBitsRead++
  185. // The "&1" is redundant, but can eliminate a bounds check.
  186. state = int32(decodeTable[state][bit&1])
  187. if state < 0 {
  188. return uint32(^state), nil
  189. } else if state == 0 {
  190. // Unread the bits we've read, then return errInvalidCode.
  191. b.bits = (b.bits >> nBitsRead) | bitsRead
  192. b.nBits += nBitsRead
  193. return 0, errInvalidCode
  194. }
  195. }
  196. }
  197. type reader struct {
  198. br bitReader
  199. subFormat SubFormat
  200. // width is the image width in pixels.
  201. width int
  202. // rowsRemaining starts at the image height in pixels, when the reader is
  203. // driven through the io.Reader interface, and decrements to zero as rows
  204. // are decoded. When driven through DecodeIntoGray, this field is unused.
  205. rowsRemaining int
  206. // curr and prev hold the current and previous rows. Each element is either
  207. // 0x00 (black) or 0xFF (white).
  208. //
  209. // prev may be nil, when processing the first row.
  210. curr []byte
  211. prev []byte
  212. // ri is the read index. curr[:ri] are those bytes of curr that have been
  213. // passed along via the Read method.
  214. //
  215. // When the reader is driven through DecodeIntoGray, instead of through the
  216. // io.Reader interface, this field is unused.
  217. ri int
  218. // wi is the write index. curr[:wi] are those bytes of curr that have
  219. // already been decoded via the decodeRow method.
  220. //
  221. // What this implementation calls wi is roughly equivalent to what the spec
  222. // calls the a0 index.
  223. wi int
  224. // These fields are copied from the *Options (which may be nil).
  225. align bool
  226. invert bool
  227. // atStartOfRow is whether we have just started the row. Some parts of the
  228. // spec say to treat this situation as if "wi = -1".
  229. atStartOfRow bool
  230. // penColorIsWhite is whether the next run is black or white.
  231. penColorIsWhite bool
  232. // seenStartOfImage is whether we've called the startDecode method.
  233. seenStartOfImage bool
  234. // readErr is a sticky error for the Read method.
  235. readErr error
  236. }
  237. func (z *reader) Read(p []byte) (int, error) {
  238. if z.readErr != nil {
  239. return 0, z.readErr
  240. }
  241. originalP := p
  242. for len(p) > 0 {
  243. // Allocate buffers (and decode any start-of-image codes), if
  244. // processing the first or second row.
  245. if z.curr == nil {
  246. if !z.seenStartOfImage {
  247. if z.readErr = z.startDecode(); z.readErr != nil {
  248. break
  249. }
  250. z.atStartOfRow = true
  251. }
  252. z.curr = make([]byte, z.width)
  253. }
  254. // Decode the next row, if necessary.
  255. if z.atStartOfRow {
  256. if z.rowsRemaining <= 0 {
  257. if z.readErr = z.finishDecode(); z.readErr != nil {
  258. break
  259. }
  260. z.readErr = io.EOF
  261. break
  262. }
  263. if z.readErr = z.decodeRow(); z.readErr != nil {
  264. break
  265. }
  266. z.rowsRemaining--
  267. }
  268. // Pack from z.curr (1 byte per pixel) to p (1 bit per pixel).
  269. packD, packS := highBits(p, z.curr[z.ri:], z.invert)
  270. p = p[packD:]
  271. z.ri += packS
  272. // Prepare to decode the next row, if necessary.
  273. if z.ri == len(z.curr) {
  274. z.ri, z.curr, z.prev = 0, z.prev, z.curr
  275. z.atStartOfRow = true
  276. }
  277. }
  278. n := len(originalP) - len(p)
  279. if z.invert {
  280. invertBytes(originalP[:n])
  281. }
  282. return n, z.readErr
  283. }
  284. func (z *reader) penColor() byte {
  285. if z.penColorIsWhite {
  286. return 0xFF
  287. }
  288. return 0x00
  289. }
  290. func (z *reader) startDecode() error {
  291. switch z.subFormat {
  292. case Group3:
  293. if err := z.decodeEOL(); err != nil {
  294. return err
  295. }
  296. case Group4:
  297. // No-op.
  298. default:
  299. return errUnsupportedSubFormat
  300. }
  301. z.seenStartOfImage = true
  302. return nil
  303. }
  304. func (z *reader) finishDecode() error {
  305. numberOfEOLs := 0
  306. switch z.subFormat {
  307. case Group3:
  308. // The stream ends with a RTC (Return To Control) of 6 consecutive
  309. // EOL's, but we should have already just seen an EOL, either in
  310. // z.startDecode (for a zero-height image) or in z.decodeRow.
  311. numberOfEOLs = 5
  312. case Group4:
  313. // The stream ends with two EOL's, the first of which is possibly
  314. // byte-aligned.
  315. numberOfEOLs = 2
  316. if err := z.decodeEOL(); err == nil {
  317. numberOfEOLs--
  318. } else if err == errInvalidCode {
  319. // Try again, this time starting from a byte boundary.
  320. z.br.alignToByteBoundary()
  321. } else {
  322. return err
  323. }
  324. default:
  325. return errUnsupportedSubFormat
  326. }
  327. for ; numberOfEOLs > 0; numberOfEOLs-- {
  328. if err := z.decodeEOL(); err != nil {
  329. return err
  330. }
  331. }
  332. return nil
  333. }
  334. func (z *reader) decodeEOL() error {
  335. // TODO: EOL doesn't have to be in the modeDecodeTable. It could be in its
  336. // own table, or we could just hard-code it, especially if we might need to
  337. // cater for optional byte-alignment, or an arbitrary number (potentially
  338. // more than 8) of 0-valued padding bits.
  339. if mode, err := decode(&z.br, modeDecodeTable[:]); err != nil {
  340. return err
  341. } else if mode != modeEOL {
  342. return errMissingEOL
  343. }
  344. return nil
  345. }
  346. func (z *reader) decodeRow() error {
  347. z.wi = 0
  348. z.atStartOfRow = true
  349. z.penColorIsWhite = true
  350. if z.align {
  351. z.br.alignToByteBoundary()
  352. }
  353. switch z.subFormat {
  354. case Group3:
  355. for ; z.wi < len(z.curr); z.atStartOfRow = false {
  356. if err := z.decodeRun(); err != nil {
  357. return err
  358. }
  359. }
  360. return z.decodeEOL()
  361. case Group4:
  362. for ; z.wi < len(z.curr); z.atStartOfRow = false {
  363. mode, err := decode(&z.br, modeDecodeTable[:])
  364. if err != nil {
  365. return err
  366. }
  367. rm := readerMode{}
  368. if mode < uint32(len(readerModes)) {
  369. rm = readerModes[mode]
  370. }
  371. if rm.function == nil {
  372. return errInvalidMode
  373. }
  374. if err := rm.function(z, rm.arg); err != nil {
  375. return err
  376. }
  377. }
  378. return nil
  379. }
  380. return errUnsupportedSubFormat
  381. }
  382. func (z *reader) decodeRun() error {
  383. table := blackDecodeTable[:]
  384. if z.penColorIsWhite {
  385. table = whiteDecodeTable[:]
  386. }
  387. total := 0
  388. for {
  389. n, err := decode(&z.br, table)
  390. if err != nil {
  391. return err
  392. }
  393. if n > maxWidth {
  394. panic("unreachable")
  395. }
  396. total += int(n)
  397. if total > maxWidth {
  398. return errRunLengthTooLong
  399. }
  400. // Anything 0x3F or below is a terminal code.
  401. if n <= 0x3F {
  402. break
  403. }
  404. }
  405. if total > (len(z.curr) - z.wi) {
  406. return errRunLengthOverflowsWidth
  407. }
  408. dst := z.curr[z.wi : z.wi+total]
  409. penColor := z.penColor()
  410. for i := range dst {
  411. dst[i] = penColor
  412. }
  413. z.wi += total
  414. z.penColorIsWhite = !z.penColorIsWhite
  415. return nil
  416. }
  417. // The various modes' semantics are based on determining a row of pixels'
  418. // "changing elements": those pixels whose color differs from the one on its
  419. // immediate left.
  420. //
  421. // The row above the first row is implicitly all white. Similarly, the column
  422. // to the left of the first column is implicitly all white.
  423. //
  424. // For example, here's Figure 1 in "ITU-T Recommendation T.6", where the
  425. // current and previous rows contain black (B) and white (w) pixels. The a?
  426. // indexes point into curr, the b? indexes point into prev.
  427. //
  428. // b1 b2
  429. // v v
  430. // prev: BBBBBwwwwwBBBwwwww
  431. // curr: BBBwwwwwBBBBBBwwww
  432. // ^ ^ ^
  433. // a0 a1 a2
  434. //
  435. // a0 is the "reference element" or current decoder position, roughly
  436. // equivalent to what this implementation calls reader.wi.
  437. //
  438. // a1 is the next changing element to the right of a0, on the "coding line"
  439. // (the current row).
  440. //
  441. // a2 is the next changing element to the right of a1, again on curr.
  442. //
  443. // b1 is the first changing element on the "reference line" (the previous row)
  444. // to the right of a0 and of opposite color to a0.
  445. //
  446. // b2 is the next changing element to the right of b1, again on prev.
  447. //
  448. // The various modes calculate a1 (and a2, for modeH):
  449. // - modePass calculates that a1 is at or to the right of b2.
  450. // - modeH calculates a1 and a2 without considering b1 or b2.
  451. // - modeV* calculates a1 to be b1 plus an adjustment (between -3 and +3).
  452. const (
  453. findB1 = false
  454. findB2 = true
  455. )
  456. // findB finds either the b1 or b2 value.
  457. func (z *reader) findB(whichB bool) int {
  458. // The initial row is a special case. The previous row is implicitly all
  459. // white, so that there are no changing pixel elements. We return b1 or b2
  460. // to be at the end of the row.
  461. if len(z.prev) != len(z.curr) {
  462. return len(z.curr)
  463. }
  464. i := z.wi
  465. if z.atStartOfRow {
  466. // a0 is implicitly at -1, on a white pixel. b1 is the first black
  467. // pixel in the previous row. b2 is the first white pixel after that.
  468. for ; (i < len(z.prev)) && (z.prev[i] == 0xFF); i++ {
  469. }
  470. if whichB == findB2 {
  471. for ; (i < len(z.prev)) && (z.prev[i] == 0x00); i++ {
  472. }
  473. }
  474. return i
  475. }
  476. // As per figure 1 above, assume that the current pen color is white.
  477. // First, walk past every contiguous black pixel in prev, starting at a0.
  478. oppositeColor := ^z.penColor()
  479. for ; (i < len(z.prev)) && (z.prev[i] == oppositeColor); i++ {
  480. }
  481. // Then walk past every contiguous white pixel.
  482. penColor := ^oppositeColor
  483. for ; (i < len(z.prev)) && (z.prev[i] == penColor); i++ {
  484. }
  485. // We're now at a black pixel (or at the end of the row). That's b1.
  486. if whichB == findB2 {
  487. // If we're looking for b2, walk past every contiguous black pixel
  488. // again.
  489. oppositeColor := ^penColor
  490. for ; (i < len(z.prev)) && (z.prev[i] == oppositeColor); i++ {
  491. }
  492. }
  493. return i
  494. }
  495. type readerMode struct {
  496. function func(z *reader, arg int) error
  497. arg int
  498. }
  499. var readerModes = [...]readerMode{
  500. modePass: {function: readerModePass},
  501. modeH: {function: readerModeH},
  502. modeV0: {function: readerModeV, arg: +0},
  503. modeVR1: {function: readerModeV, arg: +1},
  504. modeVR2: {function: readerModeV, arg: +2},
  505. modeVR3: {function: readerModeV, arg: +3},
  506. modeVL1: {function: readerModeV, arg: -1},
  507. modeVL2: {function: readerModeV, arg: -2},
  508. modeVL3: {function: readerModeV, arg: -3},
  509. modeExt: {function: readerModeExt},
  510. }
  511. func readerModePass(z *reader, arg int) error {
  512. b2 := z.findB(findB2)
  513. if (b2 < z.wi) || (len(z.curr) < b2) {
  514. return errInvalidOffset
  515. }
  516. dst := z.curr[z.wi:b2]
  517. penColor := z.penColor()
  518. for i := range dst {
  519. dst[i] = penColor
  520. }
  521. z.wi = b2
  522. return nil
  523. }
  524. func readerModeH(z *reader, arg int) error {
  525. // The first iteration finds a1. The second finds a2.
  526. for i := 0; i < 2; i++ {
  527. if err := z.decodeRun(); err != nil {
  528. return err
  529. }
  530. }
  531. return nil
  532. }
  533. func readerModeV(z *reader, arg int) error {
  534. a1 := z.findB(findB1) + arg
  535. if (a1 < z.wi) || (len(z.curr) < a1) {
  536. return errInvalidOffset
  537. }
  538. dst := z.curr[z.wi:a1]
  539. penColor := z.penColor()
  540. for i := range dst {
  541. dst[i] = penColor
  542. }
  543. z.wi = a1
  544. z.penColorIsWhite = !z.penColorIsWhite
  545. return nil
  546. }
  547. func readerModeExt(z *reader, arg int) error {
  548. return errUnsupportedMode
  549. }
  550. // DecodeIntoGray decodes the CCITT-formatted data in r into dst.
  551. //
  552. // It returns an error if dst's width and height don't match the implied width
  553. // and height of CCITT-formatted data.
  554. func DecodeIntoGray(dst *image.Gray, r io.Reader, order Order, sf SubFormat, opts *Options) error {
  555. bounds := dst.Bounds()
  556. if (bounds.Dx() < 0) || (bounds.Dy() < 0) {
  557. return errInvalidBounds
  558. }
  559. if bounds.Dx() > maxWidth {
  560. return errUnsupportedWidth
  561. }
  562. z := reader{
  563. br: bitReader{r: r, order: order},
  564. subFormat: sf,
  565. align: (opts != nil) && opts.Align,
  566. invert: (opts != nil) && opts.Invert,
  567. width: bounds.Dx(),
  568. }
  569. if err := z.startDecode(); err != nil {
  570. return err
  571. }
  572. width := bounds.Dx()
  573. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  574. p := (y - bounds.Min.Y) * dst.Stride
  575. z.curr = dst.Pix[p : p+width]
  576. if err := z.decodeRow(); err != nil {
  577. return err
  578. }
  579. z.curr, z.prev = nil, z.curr
  580. }
  581. if err := z.finishDecode(); err != nil {
  582. return err
  583. }
  584. if z.invert {
  585. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  586. p := (y - bounds.Min.Y) * dst.Stride
  587. invertBytes(dst.Pix[p : p+width])
  588. }
  589. }
  590. return nil
  591. }
  592. // NewReader returns an io.Reader that decodes the CCITT-formatted data in r.
  593. // The resultant byte stream is one bit per pixel (MSB first), with 1 meaning
  594. // white and 0 meaning black. Each row in the result is byte-aligned.
  595. func NewReader(r io.Reader, order Order, sf SubFormat, width int, height int, opts *Options) io.Reader {
  596. readErr := error(nil)
  597. if (width < 0) || (height < 0) {
  598. readErr = errInvalidBounds
  599. } else if width > maxWidth {
  600. readErr = errUnsupportedWidth
  601. }
  602. return &reader{
  603. br: bitReader{r: r, order: order},
  604. subFormat: sf,
  605. align: (opts != nil) && opts.Align,
  606. invert: (opts != nil) && opts.Invert,
  607. width: width,
  608. rowsRemaining: height,
  609. readErr: readErr,
  610. }
  611. }