Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(61)

Unified Diff: src/pkg/image/png/reader.go

Issue 6242056: code review 6242056: image/png: optimize the paeth filter implementation. (Closed)
Patch Set: diff -r 00a25723a7cd https://go.googlecode.com/hg/ Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/pkg/image/png/paeth_test.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pkg/image/png/reader.go
===================================================================
--- a/src/pkg/image/png/reader.go
+++ b/src/pkg/image/png/reader.go
@@ -98,13 +98,6 @@
func (e UnsupportedError) Error() string { return "png: unsupported feature: " + string(e) }
-func abs(x int) int {
- if x < 0 {
- return -x
- }
- return x
-}
-
func min(a, b int) int {
if a < b {
return a
@@ -241,12 +234,28 @@
return d.verifyChecksum()
}
-// The Paeth filter function, as per the PNG specification.
+// paeth implements the Paeth filter function, as per the PNG specification.
func paeth(a, b, c uint8) uint8 {
- p := int(a) + int(b) - int(c)
- pa := abs(p - int(a))
- pb := abs(p - int(b))
- pc := abs(p - int(c))
+ // This is an optimized version of the sample code in the PNG spec.
+ // For example, the sample code starts with:
+ // p := int(a) + int(b) - int(c)
+ // pa := abs(p - int(a))
+ // but the optimized form uses fewer arithmetic operations:
+ // pa := int(b) - int(c)
+ // pa = abs(pa)
+ pc := int(c)
+ pa := int(b) - pc
+ pb := int(a) - pc
+ pc = pa + pb
+ if pa < 0 {
+ pa = -pa
+ }
+ if pb < 0 {
+ pb = -pb
+ }
+ if pc < 0 {
+ pc = -pc
+ }
if pa <= pb && pa <= pc {
return a
} else if pb <= pc {
« no previous file with comments | « src/pkg/image/png/paeth_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b