Compare commits

...
5 Commits
5 changed files with 53 additions and 46 deletions

No files matched your search

+14 -20
View File
@@ -826,10 +826,8 @@ func encodeNumberCFF(buf *bytes.Buffer, val float64) {
// 返回: []byte 编码后的索引数据 // 返回: []byte 编码后的索引数据
func encodeCFFIndex(items []([]byte)) []byte { func encodeCFFIndex(items []([]byte)) []byte {
count := len(items) count := len(items)
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint16(count))
if count == 0 { if count == 0 {
return buf.Bytes() return []byte{0, 0}
} }
totalSize := 0 totalSize := 0
for _, item := range items { for _, item := range items {
@@ -845,25 +843,21 @@ func encodeCFFIndex(items []([]byte)) []byte {
if totalSize+1 > 16777215 { if totalSize+1 > 16777215 {
offSize = 4 offSize = 4
} }
buf.WriteByte(byte(offSize)) base := 3 + (count+1)*offSize
data := make([]byte, base+totalSize)
binary.BigEndian.PutUint16(data, uint16(count))
data[2] = byte(offSize)
offset := 1 offset := 1
putOffset(buf, offset, offSize) for i := 0; i <= count; i++ {
for _, item := range items { for j := 0; j < offSize; j++ {
offset += len(item) data[3+i*offSize+offSize-1-j] = byte(offset >> (8 * j))
putOffset(buf, offset, offSize) }
if i < count {
copy(data[base+offset-1:], items[i])
offset += len(items[i])
}
} }
for _, item := range items { return data
buf.Write(item)
}
return buf.Bytes()
}
// putOffset 写入指定大小的偏移量
// 入参: buf 缓冲区, val 偏移值, size 字节大小
func putOffset(buf *bytes.Buffer, val int, size int) {
tmp := make([]byte, 4)
binary.BigEndian.PutUint32(tmp, uint32(val))
buf.Write(tmp[4-size:])
} }
// getCFFIndexCount 读取CFF索引的计数和大小 // getCFFIndexCount 读取CFF索引的计数和大小
+15 -13
View File
@@ -91,19 +91,21 @@ func FileTables(data []byte, index int) (map[string][]byte, error) {
// 入参: data 表数据 // 入参: data 表数据
// 返回: uint32 校验和 // 返回: uint32 校验和
func calcTableChecksum(data []byte) uint32 { func calcTableChecksum(data []byte) uint32 {
var sum uint32 var a, b, c, d uint32
length := len(data) for len(data) >= 16 {
for i := 0; i < length; i += 4 { a += binary.BigEndian.Uint32(data[:4])
if i+4 <= length { b += binary.BigEndian.Uint32(data[4:8])
sum += binary.BigEndian.Uint32(data[i : i+4]) c += binary.BigEndian.Uint32(data[8:12])
} else { d += binary.BigEndian.Uint32(data[12:16])
var val uint32 data = data[16:]
rem := data[i:] }
for j, b := range rem { sum := a + b + c + d
val |= uint32(b) << (24 - 8*j) for len(data) >= 4 {
} sum += binary.BigEndian.Uint32(data[:4])
sum += val data = data[4:]
} }
for i, value := range data {
sum += uint32(value) << (24 - 8*i)
} }
return sum return sum
} }
+11 -2
View File
@@ -722,6 +722,7 @@ func WithTransparentEdge(ctx context.Context, img image.Image) (image.Image, int
hasZero, hasVisible := false, false hasZero, hasVisible := false, false
var pixels []byte var pixels []byte
var stride, step int var stride, step int
wideAlpha := false
switch src := source.(type) { switch src := source.(type) {
case *image.NRGBA: case *image.NRGBA:
pixels, stride, step = src.Pix, src.Stride, 4 pixels, stride, step = src.Pix, src.Stride, 4
@@ -729,9 +730,17 @@ func WithTransparentEdge(ctx context.Context, img image.Image) (image.Image, int
pixels, stride, step = src.Pix, src.Stride, 4 pixels, stride, step = src.Pix, src.Stride, 4
case *image.Alpha: case *image.Alpha:
pixels, stride, step = src.Pix, src.Stride, 1 pixels, stride, step = src.Pix, src.Stride, 1
case *image.NRGBA64:
pixels, stride, step, wideAlpha = src.Pix, src.Stride, 8, true
case *image.RGBA64:
pixels, stride, step, wideAlpha = src.Pix, src.Stride, 8, true
case *image.Alpha16:
pixels, stride, step, wideAlpha = src.Pix, src.Stride, 2, true
case *colorImage:
pixels, stride, step, wideAlpha = src.Pix, src.Stride, 8, true
} }
if pixels != nil { if pixels != nil {
hasZero = pixels[step-1] == 0 hasZero = pixels[step-1] == 0 && (!wideAlpha || pixels[step-2] == 0)
hasVisible = !hasZero hasVisible = !hasZero
scan: scan:
for y := 0; y < h; y++ { for y := 0; y < h; y++ {
@@ -745,7 +754,7 @@ func WithTransparentEdge(ctx context.Context, img image.Image) (image.Image, int
return nil, 0, err return nil, 0, err
} }
} }
if (pixels[offset] == 0) != hasZero { if (pixels[offset] == 0 && (!wideAlpha || pixels[offset-1] == 0)) != hasZero {
hasZero, hasVisible = true, true hasZero, hasVisible = true, true
break scan break scan
} }
+7 -9
View File
@@ -189,11 +189,8 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
clear(row) clear(row)
first, last := samples[0], samples[len(samples)-1] first, last := samples[0], samples[len(samples)-1]
reversed := first.first > last.first || first.first == last.first && len(first.weights) > len(last.weights) reversed := first.first > last.first || first.first == last.first && len(first.weights) > len(last.weights)
start, end := samples[0].first, samples[0].first+len(samples[0].weights) start := min(first.first, last.first)
for _, sample := range samples[1:] { end := max(first.first+len(first.weights), last.first+len(last.weights))
start = min(start, sample.first)
end = max(end, sample.first+len(sample.weights))
}
active, next := 0, 0 active, next := 0, 0
for x := start; x < end; x++ { for x := start; x < end; x++ {
for next < len(samples) { for next < len(samples) {
@@ -217,6 +214,7 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
active++ active++
} }
p := src.RGBA64At(x, y) p := src.RGBA64At(x, y)
r, g, b, a := float64(p.R), float64(p.G), float64(p.B), float64(p.A)
for column := active; column < next; column++ { for column := active; column < next; column++ {
index := column index := column
if reversed { if reversed {
@@ -226,10 +224,10 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
weight := sample.weights[x-sample.first] weight := sample.weights[x-sample.first]
if weight != 0 { if weight != 0 {
value := &row[index] value := &row[index]
value.r += float64(p.R) * weight value.r += r * weight
value.g += float64(p.G) * weight value.g += g * weight
value.b += float64(p.B) * weight value.b += b * weight
value.a += float64(p.A) * weight value.a += a * weight
} }
} }
} }
+6 -2
View File
@@ -42,10 +42,14 @@ func StorageCost(img image.Image, limit int) int {
} }
cost += cap(encoded.profile) cost += cap(encoded.profile)
if encoded.profile != nil || encoded.pngColor != nil || encoded.config.ColorModel == color.CMYKModel { if encoded.profile != nil || encoded.pngColor != nil || encoded.config.ColorModel == color.CMYKModel {
if w > (limit-cost)/12/h { pixelSize := 12
if encoded.config.ColorModel == color.RGBA64Model || encoded.config.ColorModel == color.NRGBA64Model {
pixelSize = 16
}
if w > (limit-cost)/pixelSize/h {
return limit + 1 return limit + 1
} }
pixels = w * h * 12 pixels = w * h * pixelSize
} }
if result := encoded.result.Load(); result != nil && result.image != nil { if result := encoded.result.Load(); result != nil && result.image != nil {
pixels = max(pixels, StorageCost(result.image, limit-cost)) pixels = max(pixels, StorageCost(result.image, limit-cost))