mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 14:31:12 +08:00
This commit is contained in:
1 parent
5e0bca66e3
commit
b5a8c9af9c
24 files changed
+935
-223
No files matched your search
@@ -76,11 +76,7 @@ func (c *TransferCache) Resolve(reader *pdfgo.Reader, style pdfgo.Style, model p
|
||||
// 入参: space 合成空间, values 非预乘分量
|
||||
// 返回: [3]float64 显示颜色, error 校准颜色转换错误
|
||||
func OutputColor(space *pdfgo.ColorSpace, values [4]float64) ([3]float64, error) {
|
||||
if space.Model == "DeviceCMYK" && !space.Calibrated() {
|
||||
black := 1 - values[3]
|
||||
return [3]float64{(1 - values[0]) * black, (1 - values[1]) * black, (1 - values[2]) * black}, nil
|
||||
}
|
||||
return space.RGB(values[:space.Components()], "RelativeColorimetric")
|
||||
return space.SRGB(values[:space.Components()], "RelativeColorimetric")
|
||||
}
|
||||
|
||||
// NormalBlend 判断混合模式是否为普通覆盖
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
package pdf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
@@ -23,6 +28,121 @@ import (
|
||||
"github.com/xiaoqidun/pdfgo"
|
||||
)
|
||||
|
||||
// EncodeCMYKTIFF 将直接四色图像保存为十六位无损TIFF,不经过显示颜色转换
|
||||
// 入参: ctx 取消上下文, source PDF源图像
|
||||
// 返回: []byte 含ICC和非预乘透明度的TIFF,非四色图像为nil, error 解码或编码错误
|
||||
func EncodeCMYKTIFF(ctx context.Context, source *pdfgo.Image) ([]byte, error) {
|
||||
if ctx == nil || source == nil {
|
||||
return nil, fmt.Errorf("missing CMYK image context or source")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if source.ImageMask {
|
||||
return nil, nil
|
||||
}
|
||||
family, err := source.ColorSpaceFamily()
|
||||
if err != nil || family != "DeviceCMYK" && family != "ICCBased" {
|
||||
return nil, err
|
||||
}
|
||||
if family == "ICCBased" {
|
||||
count, err := source.ColorSpaceComponents()
|
||||
if err != nil || count != 4 {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
components, err := source.DecodeComponentsViewContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if components.Space == nil || components.Space.Model != "DeviceCMYK" || components.Space.Components() != 4 || components.ColorantSpace != nil {
|
||||
return nil, nil
|
||||
}
|
||||
bounds := components.Rect
|
||||
width, height := bounds.Dx(), bounds.Dy()
|
||||
if width <= 0 || height <= 0 || width > int(^uint(0)>>1)/10 || uint64(width) > uint64(^uint32(0))/10 || uint64(height) > uint64(^uint32(0)) {
|
||||
return nil, fmt.Errorf("invalid CMYK TIFF dimensions")
|
||||
}
|
||||
profile := components.Space.ICCProfile(source.Intent)
|
||||
count := 12
|
||||
if len(profile) != 0 {
|
||||
count++
|
||||
}
|
||||
bitsOffset := uint64(8 + 2 + count*12 + 4)
|
||||
profileOffset := bitsOffset + 10
|
||||
stripOffset := (profileOffset + uint64(len(profile)) + 1) &^ uint64(1)
|
||||
if stripOffset > uint64(^uint32(0)) {
|
||||
return nil, fmt.Errorf("CMYK TIFF profile exceeds size limit")
|
||||
}
|
||||
var compressed bytes.Buffer
|
||||
writer := zlib.NewWriter(&compressed)
|
||||
row := make([]byte, width*10)
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
if (x-bounds.Min.X)%1024 == 0 {
|
||||
if err := ctx.Err(); err != nil {
|
||||
writer.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
values, alpha := components.ValuesAt(x, y)
|
||||
pixel := row[(x-bounds.Min.X)*10:]
|
||||
for channel, value := range [5]float64{values[0], values[1], values[2], values[3], alpha} {
|
||||
if math.IsNaN(value) || math.IsInf(value, 0) || value < 0 || value > 1 {
|
||||
writer.Close()
|
||||
return nil, fmt.Errorf("invalid CMYK TIFF component")
|
||||
}
|
||||
binary.LittleEndian.PutUint16(pixel[channel*2:], uint16(math.Round(value*65535)))
|
||||
}
|
||||
}
|
||||
if _, err := writer.Write(row); err != nil {
|
||||
writer.Close()
|
||||
return nil, err
|
||||
}
|
||||
if stripOffset+uint64(compressed.Len()) > uint64(^uint32(0)) {
|
||||
writer.Close()
|
||||
return nil, fmt.Errorf("CMYK TIFF exceeds size limit")
|
||||
}
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
total := stripOffset + uint64(compressed.Len())
|
||||
if total > uint64(^uint32(0)) || total > uint64(int(^uint(0)>>1)) {
|
||||
return nil, fmt.Errorf("CMYK TIFF exceeds size limit")
|
||||
}
|
||||
data := make([]byte, int(total))
|
||||
copy(data, "II")
|
||||
binary.LittleEndian.PutUint16(data[2:], 42)
|
||||
binary.LittleEndian.PutUint32(data[4:], 8)
|
||||
binary.LittleEndian.PutUint16(data[8:], uint16(count))
|
||||
entries := [][4]uint32{
|
||||
{256, 4, 1, uint32(width)}, {257, 4, 1, uint32(height)},
|
||||
{258, 3, 5, uint32(bitsOffset)}, {259, 3, 1, 8}, {262, 3, 1, 5},
|
||||
{273, 4, 1, uint32(stripOffset)}, {277, 3, 1, 5}, {278, 4, 1, uint32(height)},
|
||||
{279, 4, 1, uint32(compressed.Len())}, {284, 3, 1, 1}, {332, 3, 1, 1}, {338, 3, 1, 2},
|
||||
}
|
||||
if len(profile) != 0 {
|
||||
entries = append(entries, [4]uint32{34675, 7, uint32(len(profile)), uint32(profileOffset)})
|
||||
}
|
||||
for i, entry := range entries {
|
||||
p := data[10+i*12:]
|
||||
binary.LittleEndian.PutUint16(p, uint16(entry[0]))
|
||||
binary.LittleEndian.PutUint16(p[2:], uint16(entry[1]))
|
||||
binary.LittleEndian.PutUint32(p[4:], entry[2])
|
||||
binary.LittleEndian.PutUint32(p[8:], entry[3])
|
||||
}
|
||||
for i := range 5 {
|
||||
binary.LittleEndian.PutUint16(data[int(bitsOffset)+2*i:], 16)
|
||||
}
|
||||
copy(data[int(profileOffset):], profile)
|
||||
copy(data[int(stripOffset):], compressed.Bytes())
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// StencilImage 按当前填充色读取模板图像,不分配整幅彩色样本
|
||||
type StencilImage struct {
|
||||
Source image.Image
|
||||
|
||||
Reference in new issue
Block a user