Compare commits

...
5 Commits
5 changed files with 49 additions and 42 deletions

No files matched your search

+12 -18
View File
@@ -826,10 +826,8 @@ func encodeNumberCFF(buf *bytes.Buffer, val float64) {
// 返回: []byte 编码后的索引数据
func encodeCFFIndex(items []([]byte)) []byte {
count := len(items)
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint16(count))
if count == 0 {
return buf.Bytes()
return []byte{0, 0}
}
totalSize := 0
for _, item := range items {
@@ -845,25 +843,21 @@ func encodeCFFIndex(items []([]byte)) []byte {
if totalSize+1 > 16777215 {
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
putOffset(buf, offset, offSize)
for _, item := range items {
offset += len(item)
putOffset(buf, offset, offSize)
for i := 0; i <= count; i++ {
for j := 0; j < offSize; j++ {
data[3+i*offSize+offSize-1-j] = byte(offset >> (8 * j))
}
for _, item := range items {
buf.Write(item)
if i < count {
copy(data[base+offset-1:], items[i])
offset += len(items[i])
}
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:])
return data
}
// getCFFIndexCount 读取CFF索引的计数和大小
+13 -11
View File
@@ -91,19 +91,21 @@ func FileTables(data []byte, index int) (map[string][]byte, error) {
// 入参: data 表数据
// 返回: uint32 校验和
func calcTableChecksum(data []byte) uint32 {
var sum uint32
length := len(data)
for i := 0; i < length; i += 4 {
if i+4 <= length {
sum += binary.BigEndian.Uint32(data[i : i+4])
} else {
var val uint32
rem := data[i:]
for j, b := range rem {
val |= uint32(b) << (24 - 8*j)
var a, b, c, d uint32
for len(data) >= 16 {
a += binary.BigEndian.Uint32(data[:4])
b += binary.BigEndian.Uint32(data[4:8])
c += binary.BigEndian.Uint32(data[8:12])
d += binary.BigEndian.Uint32(data[12:16])
data = data[16:]
}
sum += val
sum := a + b + c + d
for len(data) >= 4 {
sum += binary.BigEndian.Uint32(data[:4])
data = data[4:]
}
for i, value := range data {
sum += uint32(value) << (24 - 8*i)
}
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
var pixels []byte
var stride, step int
wideAlpha := false
switch src := source.(type) {
case *image.NRGBA:
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
case *image.Alpha:
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 {
hasZero = pixels[step-1] == 0
hasZero = pixels[step-1] == 0 && (!wideAlpha || pixels[step-2] == 0)
hasVisible = !hasZero
scan:
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
}
}
if (pixels[offset] == 0) != hasZero {
if (pixels[offset] == 0 && (!wideAlpha || pixels[offset-1] == 0)) != hasZero {
hasZero, hasVisible = true, true
break scan
}
+7 -9
View File
@@ -189,11 +189,8 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
clear(row)
first, last := samples[0], samples[len(samples)-1]
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)
for _, sample := range samples[1:] {
start = min(start, sample.first)
end = max(end, sample.first+len(sample.weights))
}
start := min(first.first, last.first)
end := max(first.first+len(first.weights), last.first+len(last.weights))
active, next := 0, 0
for x := start; x < end; x++ {
for next < len(samples) {
@@ -217,6 +214,7 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
active++
}
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++ {
index := column
if reversed {
@@ -226,10 +224,10 @@ func rasterImageHorizontal(row []rasterImagePixel, src image.RGBA64Image, sample
weight := sample.weights[x-sample.first]
if weight != 0 {
value := &row[index]
value.r += float64(p.R) * weight
value.g += float64(p.G) * weight
value.b += float64(p.B) * weight
value.a += float64(p.A) * weight
value.r += r * weight
value.g += g * weight
value.b += b * 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)
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
}
pixels = w * h * 12
pixels = w * h * pixelSize
}
if result := encoded.result.Load(); result != nil && result.image != nil {
pixels = max(pixels, StorageCost(result.image, limit-cost))