mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 17:27:08 +08:00
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
// Copyright 2025-2026 肖其顿 (XIAO QI DUN)
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package ofdgo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"math"
|
|
|
|
"github.com/xiaoqidun/pdfgo"
|
|
)
|
|
|
|
// rasterMaskAllowed 仅对不可矢量表达的蒙版启用显式局部合成,格式错误仍返回
|
|
// 入参: err 蒙版转换错误
|
|
// 返回: bool 是否允许局部合成
|
|
func (p *pdfImporter) rasterMaskAllowed(err error) bool {
|
|
var unsupported *pdfgo.UnsupportedError
|
|
return p.warning != nil && errors.As(err, &unsupported)
|
|
}
|
|
|
|
// applySoftMask 按原始颜色分量计算蒙版,逐块应用透明度及亮度传递
|
|
// 入参: source 源图像, box 页面区域, mask PDF软蒙版
|
|
// 返回: image.Image 合成图像, error 蒙版解释或渲染错误
|
|
func (p *pdfImporter) applySoftMask(source image.Image, box Box, mask *pdfgo.SoftMask) (image.Image, error) {
|
|
if err := p.ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
inverse, ok := p.matrix.Inverse()
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid PDF mask transform")
|
|
}
|
|
out := image.NewNRGBA(source.Bounds())
|
|
w, h := out.Rect.Dx(), out.Rect.Dy()
|
|
step := 25.4 / p.rasterDPI
|
|
cache := p.compositingCache()
|
|
scratch := &pdfCompositeScratch{}
|
|
space := &pdfgo.ColorSpace{Model: "DeviceRGB"}
|
|
for y := 0; y < h; y += pdfCompositeTileSize {
|
|
for x := 0; x < w; x += pdfCompositeTileSize {
|
|
if err := p.ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
width, height := min(pdfCompositeTileSize, w-x), min(pdfCompositeTileSize, h-y)
|
|
c := pdfCompositor{importer: p, box: Box{X: box.X + float64(x)*step, Y: box.Y + float64(y)*step, W: float64(width) * step, H: float64(height) * step}, width: width, height: height, inverse: inverse, cache: cache, scratch: scratch, masks: map[*pdfgo.SoftMask][]float64{}}
|
|
alpha, err := c.mask(mask, space)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i, opacity := range alpha {
|
|
if i%width == 0 {
|
|
if err := p.ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
px, py := out.Rect.Min.X+x+i%width, out.Rect.Min.Y+y+i/width
|
|
pixel := imageNRGBAAt(source, px, py)
|
|
pixel.A = uint8(math.Round(float64(pixel.A) * opacity))
|
|
out.SetNRGBA(px, py, pixel)
|
|
}
|
|
c.releaseMasks()
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|