diff --git a/nimPNG/filters.nim b/nimPNG/filters.nim index ade7c2f..ab8af8e 100644 --- a/nimPNG/filters.nim +++ b/nimPNG/filters.nim @@ -29,129 +29,129 @@ proc paethPredictor(a, b, c: int): uint = elif pb < pa: return b.uint result = a.uint -proc filterScanline*(output: var openArray[byte], input: openArray[byte], byteWidth, len: int, filterType: PNGFilter) = +proc filterScanline*[T](output: var openArray[T], input: openArray[T], TWidth, len: int, filterType: PNGFilter) = template currPix: untyped = input[i].uint - template prevPix: untyped = input[i - byteWidth].uint + template prevPix: untyped = input[i - TWidth].uint case filterType of FLT_NONE: for i in 0.. 0: - output[0] = byte(FLT_NONE) # filterType byte + output[0] = T(FLT_NONE) # filterType T filterScanline(output.toOpenArray(1, output.len-1), # skip filterType - input, byteWidth, lineBytes, FLT_NONE) + input, TWidth, lineTs, FLT_NONE) # next line start from 1 var prevIndex = 0 for y in 1.. 0: - output[0] = byte(predefinedFilters[0]) # filterType byte + output[0] = T(predefinedFilters[0]) # filterType T filterScanline(output.toOpenArray(1, output.len-1), # skip filterType - input, byteWidth, lineBytes, predefinedFilters[0]) + input, TWidth, lineTs, predefinedFilters[0]) # next line start from 1 var prevIndex = 0 for y in 1.. 0: unfilterScanLine(output, input.toOpenArray(1, input.len-1), # skip the filterType - byteWidth, lineBytes, + TWidth, lineTs, PNGFilter(input[0])) # next line start from 1 var prevIndex = 0 for y in 1.. -1: result += readBitFromReversedStream(bitptr, bitstream) shl i dec i -proc `&=`(a: var byte, b: byte) = - a = byte(int(a) and int(b)) +proc `&=`[T](a: var T, b: T) = + a = T(int(a) and int(b)) -proc `|=`(a: var byte, b: byte) = - a = byte(int(a) or int(b)) +proc `|=`[T](a: var T, b: T) = + a = T(int(a) or int(b)) -proc setBitOfReversedStream0(bitptr: var int, bitstream: var openArray[byte], bit: int) = +proc setBitOfReversedStream0[T](bitptr: var int, bitstream: var openArray[T], bit: int) = # the current bit in bitstream must be 0 for this to work if bit != 0: - # earlier bit of huffman code is in a lesser significant bit of an earlier byte - bitstream[bitptr shr 3] |= byte(bit shl (7 - (bitptr and 0x7))) + # earlier bit of huffman code is in a lesser significant bit of an earlier T + bitstream[bitptr shr 3] |= T(bit shl (7 - (bitptr and 0x7))) inc bitptr -proc setBitOfReversedStream(bitptr: var int, bitstream: var openArray[byte], bit: int) = +proc setBitOfReversedStream[T](bitptr: var int, bitstream: var openArray[T], bit: int) = # the current bit in bitstream may be 0 or 1 for this to work - if bit == 0: bitstream[bitptr shr 3] &= byte(not (1 shl (7 - (bitptr and 0x7)))) - else: bitstream[bitptr shr 3] |= byte(1 shl (7 - (bitptr and 0x7))) + if bit == 0: bitstream[bitptr shr 3] &= T(not (1 shl (7 - (bitptr and 0x7)))) + else: bitstream[bitptr shr 3] |= T(1 shl (7 - (bitptr and 0x7))) inc bitptr -proc removePaddingBits(output: var openArray[byte], input: openArray[byte], olinebits, ilinebits, h: int) = +proc removePaddingBits[T](output: var openArray[T], input: openArray[T], olinebits, ilinebits, h: int) = # After filtering there are still padding bits if scanLines have non multiple of 8 bit amounts. They need # to be removed (except at last scanLine of (Adam7-reduced) image) before working with pure image buffers # for the Adam7 code, the color convert code and the output to the user. @@ -453,18 +453,18 @@ proc removePaddingBits(output: var openArray[byte], input: openArray[byte], olin # passw: output containing the width of the 7 passes # passh: output containing the height of the 7 passes # filter_passstart: output containing the index of the start and end of each -# reduced image with filter bytes +# reduced image with filter Ts # padded_passstart output containing the index of the start and end of each -# reduced image when without filter bytes but with padded scanLines +# reduced image when without filter Ts but with padded scanLines # passstart: output containing the index of the start and end of each reduced # image without padding between scanLines, but still padding between the images # w, h: width and height of non-interlaced image # bpp: bits per pixel # "padded" is only relevant if bpp is less than 8 and a scanLine or image does not -# end at a full byte -proc adam7PassValues*(pass: var PNGPass, w, h, bpp: int) = +# end at a full T +proc adam7PassValues(pass: var PNGPass, w, h, bpp: int) = # the passstart values have 8 values: - # the 8th one indicates the byte after the end of the 7th (= last) pass + # the 8th one indicates the T after the end of the 7th (= last) pass # calculate width and height in pixels of each pass for i in 0..6: @@ -477,17 +477,17 @@ proc adam7PassValues*(pass: var PNGPass, w, h, bpp: int) = pass.paddedStart[0] = 0 pass.start[0] = 0 for i in 0..6: - # if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte) + # if passw[i] is 0, it's 0 Ts, not 1 (no filtertype-T) pass.filterStart[i + 1] = pass.filterStart[i] if (pass.w[i] != 0) and (pass.h[i] != 0): pass.filterStart[i + 1] += pass.h[i] * (1 + (pass.w[i] * bpp + 7) div 8) - # bits padded if needed to fill full byte at end of each scanLine + # bits padded if needed to fill full T at end of each scanLine pass.paddedStart[i + 1] = pass.paddedStart[i] + pass.h[i] * ((pass.w[i] * bpp + 7) div 8) # only padded at end of reduced image pass.start[i + 1] = pass.start[i] + (pass.h[i] * pass.w[i] * bpp + 7) div 8 # input: Adam7 interlaced image, with no padding bits between scanLines, but between -# reduced images so that each reduced image starts at a byte. +# reduced images so that each reduced image starts at a T. # output: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h # bpp: bits per pixel # output has the following size in bits: w * h * bpp. @@ -495,18 +495,18 @@ proc adam7PassValues*(pass: var PNGPass, w, h, bpp: int) = # output must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation # (because that's likely a little bit faster) # NOTE: comments about padding bits are only relevant if bpp < 8 -proc adam7Deinterlace*(output: var openArray[byte], input: openArray[byte], w, h, bpp: int) = +proc adam7Deinterlace*[T](output: var openArray[T], input: openArray[T], w, h, bpp: int) = var pass: PNGPass adam7PassValues(pass, w, h, bpp) if bpp >= 8: for i in 0..6: - let byteWidth = bpp div 8 + let TWidth = bpp div 8 for y in 0..= 8: for i in 0..6: - let byteWidth = bpp div 8 + let TWidth = bpp div 8 for y in 0..