Skip to content

Commit

Permalink
Minor linting
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Jun 15, 2022
1 parent 907702f commit b96edcb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 69 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ If a regular PNG file is read, the first Frame of the APNG returned by `DecodeAl
### APNG
The APNG type contains the frames of a decoded `.apng` file, along with any important properties. It may also be created and used for Encoding.

| Signature | Description |
|---------------------------|-------------------------------|
| Frames []Frame | The stored frames of the APNG.|
| LoopCount uint | The number of times an animation should be restarted during display. A value of 0 means to loop forever. |
| Signature | Description |
|----------------|----------------------------------------------------------------------------------------------------------|
| Frames []Frame | The stored frames of the APNG. |
| LoopCount uint | The number of times an animation should be restarted during display. A value of 0 means to loop forever. |

### Frame
The Frame type contains an individual frame of an APNG. The following table provides the important properties and methods.

| Signature | Description |
|---------------------------|------------------|
| Image image.Image | Frame image data. |
| IsDefault bool | Indicates if this frame is a default image that should not be included as part of the animation frames. May only be true for the first Frame. |
| XOffset int | Returns the x offset of the frame. |
| YOffset int | Returns the y offset of the frame. |
| DelayNumerator int | Returns the delay numerator. |
| DelayDenominator int | Returns the delay denominator. |
| DisposeOp byte | Returns the frame disposal operation. May be `apng.DISPOSE_OP_NONE`, `apng.DISPOSE_OP_BACKGROUND`, or `apng.DISPOSE_OP_PREVIOUS`. See the [APNG Specification](https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk) for more information. |
| BlendOp byte | Returns the frame blending operation. May be `apng.BLEND_OP_SOURCE` or `apng.BLEND_OP_OVER`. See the [APNG Specification](https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk) for more information. |
| Signature | Description |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|
| Image image.Image | Frame image data. |
| IsDefault bool | Indicates if this frame is a default image that should not be included as part of the animation frames. May only be true for the first Frame. |
| XOffset int | Returns the x offset of the frame. |
| YOffset int | Returns the y offset of the frame. |
| DelayNumerator int | Returns the delay numerator. |
| DelayDenominator int | Returns the delay denominator. |
| DisposeOp byte | Returns the frame disposal operation. May be `apng.DISPOSE_OP_NONE`, `apng.DISPOSE_OP_BACKGROUND`, or `apng.DISPOSE_OP_PREVIOUS`. See the [APNG Specification](https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk) for more information. |
| BlendOp byte | Returns the frame blending operation. May be `apng.BLEND_OP_SOURCE` or `apng.BLEND_OP_OVER`. See the [APNG Specification](https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk) for more information. |

## Methods
### DecodeAll(io.Reader) (APNG, error)
Expand All @@ -39,9 +39,9 @@ This method returns an APNG type containing the frames and associated data withi
package main

import (
"github.com/kettek/apng"
"os"
"log"
"os"
"github.com/kettek/apng"
)

func main() {
Expand Down Expand Up @@ -77,9 +77,9 @@ This method writes the passed APNG object to the given io.Writer as an APNG bina
package main

import (
"github.com/kettek/apng"
"image/png"
"os"
"github.com/kettek/apng"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
)

type Frame struct {
Image image.Image
Image image.Image
width, height int
XOffset, YOffset int
DelayNumerator uint16
Expand Down
53 changes: 26 additions & 27 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ const (
dsSeenIHDR
dsSeenPLTE
dsSeentRNS
dsSeenacTL
dsSeenIDAT
dsSeenfdAT
dsSeenIEND
Expand All @@ -109,9 +108,9 @@ const pngHeader = "\x89PNG\r\n\x1a\n"

type decoder struct {
r io.Reader
num_frames uint32
numFrames uint32
a APNG
frame_index int
frameIndex int
crc hash.Hash32
width, height int
depth int
Expand Down Expand Up @@ -446,8 +445,8 @@ func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image
width int
height int
)
width = d.a.Frames[d.frame_index].width
height = d.a.Frames[d.frame_index].height
width = d.a.Frames[d.frameIndex].width
height = d.a.Frames[d.frameIndex].height
if d.interlace == itAdam7 && !allocateOnly {
p := interlacing[pass]
// Add the multiplication factor and subtract one, effectively rounding up.
Expand Down Expand Up @@ -885,7 +884,7 @@ func (d *decoder) parseacTL(length uint32) (err error) {
return err
}

d.num_frames = binary.BigEndian.Uint32(d.tmp[:4])
d.numFrames = binary.BigEndian.Uint32(d.tmp[:4])
d.a.LoopCount = uint(binary.BigEndian.Uint32(d.tmp[4:8]))

d.crc.Write(d.tmp[:8])
Expand All @@ -900,19 +899,19 @@ func (d *decoder) parsefcTL(length uint32) (err error) {
return err
}

if d.frame_index >= len(d.a.Frames) {
if d.frameIndex >= len(d.a.Frames) {
d.a.Frames = append(d.a.Frames, Frame{})
}

d.a.Frames[d.frame_index].IsDefault = false
d.a.Frames[d.frame_index].width = int(int32(binary.BigEndian.Uint32(d.tmp[4:8])))
d.a.Frames[d.frame_index].height = int(int32(binary.BigEndian.Uint32(d.tmp[8:12])))
d.a.Frames[d.frame_index].XOffset = int(binary.BigEndian.Uint32(d.tmp[12:16]))
d.a.Frames[d.frame_index].YOffset = int(binary.BigEndian.Uint32(d.tmp[16:20]))
d.a.Frames[d.frame_index].DelayNumerator = binary.BigEndian.Uint16(d.tmp[20:22])
d.a.Frames[d.frame_index].DelayDenominator = binary.BigEndian.Uint16(d.tmp[22:24])
d.a.Frames[d.frame_index].DisposeOp = byte(d.tmp[24])
d.a.Frames[d.frame_index].BlendOp = byte(d.tmp[25])
d.a.Frames[d.frameIndex].IsDefault = false
d.a.Frames[d.frameIndex].width = int(int32(binary.BigEndian.Uint32(d.tmp[4:8])))
d.a.Frames[d.frameIndex].height = int(int32(binary.BigEndian.Uint32(d.tmp[8:12])))
d.a.Frames[d.frameIndex].XOffset = int(binary.BigEndian.Uint32(d.tmp[12:16]))
d.a.Frames[d.frameIndex].YOffset = int(binary.BigEndian.Uint32(d.tmp[16:20]))
d.a.Frames[d.frameIndex].DelayNumerator = binary.BigEndian.Uint16(d.tmp[20:22])
d.a.Frames[d.frameIndex].DelayDenominator = binary.BigEndian.Uint16(d.tmp[22:24])
d.a.Frames[d.frameIndex].DisposeOp = d.tmp[24]
d.a.Frames[d.frameIndex].BlendOp = d.tmp[25]

d.crc.Write(d.tmp[:26])
return d.verifyChecksum()
Expand All @@ -924,7 +923,7 @@ func (d *decoder) parsefdAT(length uint32) (err error) {
}
d.crc.Write(d.tmp[:4])
d.idatLength = length - 4
d.a.Frames[d.frame_index].Image, err = d.decode()
d.a.Frames[d.frameIndex].Image, err = d.decode()
if err != nil {
return err
}
Expand All @@ -933,7 +932,7 @@ func (d *decoder) parsefdAT(length uint32) (err error) {

func (d *decoder) parseIDAT(length uint32) (err error) {
d.idatLength = length
d.a.Frames[d.frame_index].Image, err = d.decode()
d.a.Frames[d.frameIndex].Image, err = d.decode()
if err != nil {
return err
}
Expand Down Expand Up @@ -987,7 +986,7 @@ func (d *decoder) parseChunk() error {
return d.parseacTL(length)
case "fcTL":
if d.stage >= dsSeenIDAT {
d.frame_index = d.frame_index + 1
d.frameIndex = d.frameIndex + 1
}
return d.parsefcTL(length)
case "fdAT":
Expand Down Expand Up @@ -1053,16 +1052,16 @@ func (d *decoder) checkHeader() error {
return nil
}

// Decode reads an APNG file from r and returns it as an APNG
// DecodeAll reads an APNG file from r and returns it as an APNG
// Type. If the first frame returns true for IsDefault(), that
// frame should not be part of the a.
// frame should not be part of the result.
// The type of Image returned depends on the PNG contents.
func DecodeAll(r io.Reader) (APNG, error) {
d := &decoder{
r: r,
crc: crc32.NewIEEE(),
frame_index: 0,
a: APNG{Frames: make([]Frame, 1)},
r: r,
crc: crc32.NewIEEE(),
frameIndex: 0,
a: APNG{Frames: make([]Frame, 1)},
}
d.a.Frames[0].IsDefault = true
if err := d.checkHeader(); err != nil {
Expand Down Expand Up @@ -1143,8 +1142,8 @@ func DecodeConfig(r io.Reader) (image.Config, error) {
}
return image.Config{
ColorModel: cm,
Width: int(d.a.Frames[0].width),
Height: int(d.a.Frames[0].height),
Width: d.a.Frames[0].width,
Height: d.a.Frames[0].height,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestDimensionOverflow(t *testing.T) {
}

func TestReadAPNGWithDefaultFrame(t *testing.T) {
a, err := ReadAPNG("tests/WithDefaultFrame.png")
a, err := readAPNG("tests/WithDefaultFrame.png")
if err != nil {
t.Error(err)
return
Expand All @@ -328,7 +328,7 @@ func TestReadAPNGWithDefaultFrame(t *testing.T) {
}

func TestReadAPNGWithoutDefaultFrame(t *testing.T) {
a, err := ReadAPNG("tests/WithoutDefaultFrame.png")
a, err := readAPNG("tests/WithoutDefaultFrame.png")
if err != nil {
t.Error(err)
return
Expand All @@ -346,7 +346,7 @@ func TestReadAPNGWithoutDefaultFrame(t *testing.T) {
}

func TestReadAPNGWithMultipleIDATs(t *testing.T) {
a, err := ReadAPNG("tests/MultipleIDATs.png")
a, err := readAPNG("tests/MultipleIDATs.png")
if err != nil {
t.Error(err)
return
Expand All @@ -358,7 +358,7 @@ func TestReadAPNGWithMultipleIDATs(t *testing.T) {
}
}

func ReadAPNG(path string) (APNG, error) {
func readAPNG(path string) (APNG, error) {
f, err := os.Open(path)
if err != nil {
return APNG{}, err
Expand Down
40 changes: 20 additions & 20 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ type EncoderBufferPool interface {
type EncoderBuffer encoder

type encoder struct {
enc *Encoder
w io.Writer
a APNG
write_type int // 0 = IDAT, 1 = fdAT
seq int
cb int
err error
header [8]byte
footer [4]byte
tmp [4 * 256]byte
cr [nFilter][]uint8
pr []uint8
zw *zlib.Writer
zwLevel int
bw *bufio.Writer
enc *Encoder
w io.Writer
a APNG
writeType int // 0 = IDAT, 1 = fdAT
seq int
cb int
err error
header [8]byte
footer [4]byte
tmp [4 * 256]byte
cr [nFilter][]uint8
pr []uint8
zw *zlib.Writer
zwLevel int
bw *bufio.Writer
}

// CompressionLevel indicates the compression level.
Expand Down Expand Up @@ -185,15 +185,15 @@ func (e *encoder) writefcTL(f Frame) {
binary.BigEndian.PutUint32(e.tmp[8:12], uint32(b.Dy()))
binary.BigEndian.PutUint32(e.tmp[12:16], uint32(f.XOffset))
binary.BigEndian.PutUint32(e.tmp[16:20], uint32(f.YOffset))
binary.BigEndian.PutUint16(e.tmp[20:22], uint16(f.DelayNumerator))
binary.BigEndian.PutUint16(e.tmp[22:24], uint16(f.DelayDenominator))
binary.BigEndian.PutUint16(e.tmp[20:22], f.DelayNumerator)
binary.BigEndian.PutUint16(e.tmp[22:24], f.DelayDenominator)
e.tmp[24] = f.DisposeOp
e.tmp[25] = f.BlendOp
e.writeChunk(e.tmp[:26], "fcTL")
}

func (e *encoder) writefdATs(f Frame) {
e.write_type = 1
e.writeType = 1
if e.err != nil {
return
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func (e *encoder) writePLTEAndTRNS(p color.Palette) {
// This method should only be called from writeIDATs (via writeImage).
// No other code should treat an encoder as an io.Writer.
func (e *encoder) Write(b []byte) (int, error) {
if e.write_type == 0 {
if e.writeType == 0 {
e.writeChunk(b, "IDAT")
} else {
c := make([]byte, 4)
Expand Down Expand Up @@ -572,7 +572,7 @@ func (e *encoder) writeImage(w io.Writer, m image.Image, cb int, level int) erro

// Write the actual image data to one or more IDAT chunks.
func (e *encoder) writeIDATs() {
e.write_type = 0
e.writeType = 0
if e.err != nil {
return
}
Expand Down

0 comments on commit b96edcb

Please sign in to comment.