mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 17:27:08 +08:00
1 parent
151ddb0a20
commit
d97c6f4e6f
60 files changed
+570
-262
No files matched your search
@@ -52,6 +52,28 @@ type Writer struct {
|
||||
closed bool
|
||||
}
|
||||
|
||||
// NewTemp 创建私有输入缓存文件,调用方负责关闭和删除
|
||||
// 入参: directory 缓存目录,空值使用系统临时目录
|
||||
// 返回: *os.File 缓存文件, error 创建错误
|
||||
func NewTemp(directory string) (*os.File, error) {
|
||||
if directory == "" {
|
||||
directory = os.TempDir()
|
||||
}
|
||||
for range 100 {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(10000000))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := filepath.Join(directory, fmt.Sprintf("ofdgo-%07d.tmp", n.Int64()))
|
||||
file, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
continue
|
||||
}
|
||||
return file, err
|
||||
}
|
||||
return nil, os.ErrExist
|
||||
}
|
||||
|
||||
// NewTarget 记录目标当前状态,允许目标尚不存在
|
||||
// 入参: name 目标路径
|
||||
// 返回: *Target 保存目标, error 路径或文件错误
|
||||
|
||||
@@ -96,7 +96,7 @@ func pngColorProfile(ctx context.Context, data []byte) ([]byte, *pngColor, error
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
profile, err := io.ReadAll(io.LimitReader(contextInput{ReadCloser: reader, context: ctx}, profileLimit+1))
|
||||
profile, err := io.ReadAll(io.LimitReader(contextInput{ReadCloser: reader, ctx: ctx}, profileLimit+1))
|
||||
closeErr := reader.Close()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
||||
@@ -139,7 +139,7 @@ func (img *EncodedImage) decodeContext(ctx context.Context, pending chan struct{
|
||||
close(pending)
|
||||
img.mu.Unlock()
|
||||
}()
|
||||
pixels, _, err := image.Decode(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(img.data)), context: ctx})
|
||||
pixels, _, err := image.Decode(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(img.data)), ctx: ctx})
|
||||
if err == nil && (img.format == "jpeg" || img.profile != nil) {
|
||||
var profile *pdfgo.ColorSpace
|
||||
if img.profile != nil {
|
||||
@@ -184,7 +184,7 @@ func newEncodedImage(data []byte) (*EncodedImage, error) {
|
||||
// 入参: ctx 取消上下文, data 只读图片编码
|
||||
// 返回: *EncodedImage 惰性图片, error 头信息、格式或取消错误
|
||||
func newEncodedImageContext(ctx context.Context, data []byte) (*EncodedImage, error) {
|
||||
config, format, err := image.DecodeConfig(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(data)), context: ctx})
|
||||
config, format, err := image.DecodeConfig(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(data)), ctx: ctx})
|
||||
if cancelErr := ctx.Err(); cancelErr != nil {
|
||||
return nil, cancelErr
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ func DecodeContext(ctx context.Context, data []byte) (img image.Image, format st
|
||||
img, err := DecodeTIFF(ctx, data, layout)
|
||||
return img, "tiff", err
|
||||
}
|
||||
img, format, err = image.Decode(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(data)), context: ctx})
|
||||
img, format, err = image.Decode(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(data)), ctx: ctx})
|
||||
if err == nil {
|
||||
return img, NormalizeFormat(format), nil
|
||||
}
|
||||
@@ -267,7 +267,7 @@ func DecodeConfigDataContext(ctx context.Context, data []byte) (config image.Con
|
||||
cfg.Width, cfg.Height = size.X, size.Y
|
||||
return cfg, "tiff", nil
|
||||
}
|
||||
cfg, format, err := image.DecodeConfig(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(data)), context: ctx})
|
||||
cfg, format, err := image.DecodeConfig(contextInput{ReadCloser: io.NopCloser(bytes.NewReader(data)), ctx: ctx})
|
||||
if err == nil {
|
||||
return cfg, NormalizeFormat(format), nil
|
||||
}
|
||||
|
||||
@@ -22,14 +22,14 @@ import (
|
||||
// contextInput 在读取图像编码时检查调用方取消状态
|
||||
type contextInput struct {
|
||||
io.ReadCloser
|
||||
context context.Context
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// Read 检查取消状态后读取原始数据
|
||||
// 入参: data 数据缓冲区
|
||||
// 返回: int 读取字节数, error 读取或取消错误
|
||||
func (r contextInput) Read(data []byte) (int, error) {
|
||||
if err := r.context.Err(); err != nil {
|
||||
if err := r.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.ReadCloser.Read(data)
|
||||
|
||||
@@ -165,7 +165,7 @@ func readTIFFDirectory(ctx context.Context, input io.Reader, order binary.ByteOr
|
||||
if offset < position {
|
||||
return result, position, ErrTIFFRandomAccess
|
||||
}
|
||||
reader := contextInput{ReadCloser: io.NopCloser(input), context: ctx}
|
||||
reader := contextInput{ReadCloser: io.NopCloser(input), ctx: ctx}
|
||||
if _, err := io.CopyN(io.Discard, reader, offset-position); err != nil {
|
||||
return result, position, err
|
||||
}
|
||||
|
||||
@@ -211,11 +211,11 @@ func (s *u3dLineState) accumulateCorners(mesh *U3DMesh, shadingIndex uint32, cor
|
||||
}
|
||||
|
||||
// value 按指定上下文和位宽读取点集或线集字段
|
||||
// 入参: context 压缩上下文, small 是否为8位字段
|
||||
// 入参: ctx 压缩上下文, small 是否为8位字段
|
||||
// 返回: uint32 字段值
|
||||
func (r *u3dLineReader) value(context int, small bool) uint32 {
|
||||
func (r *u3dLineReader) value(ctx int, small bool) uint32 {
|
||||
if r.bits != nil {
|
||||
return r.bits.dynamicValue(&r.hist[context], small)
|
||||
return r.bits.dynamicValue(&r.hist[ctx], small)
|
||||
}
|
||||
if small {
|
||||
return uint32(r.values.u8())
|
||||
|
||||
@@ -167,15 +167,15 @@ func (r *u3dResourceReader) motionFrame(frame *U3DKeyFrame, previous U3DKeyFrame
|
||||
}
|
||||
|
||||
// motionDifference 用共享幅值上下文恢复一至三个差分分量
|
||||
// 入参: previous 前一帧分量, scale 逆量化系数, context 符号上下文,下一项为幅值上下文
|
||||
// 入参: previous 前一帧分量, scale 逆量化系数, ctx 符号上下文,下一项为幅值上下文
|
||||
// 返回: [3]float32 重建分量, error 符号、数值或读取错误
|
||||
func (r *u3dResourceReader) motionDifference(previous []float32, scale float32, context int) (values [3]float32, err error) {
|
||||
signs := r.value(context, true)
|
||||
func (r *u3dResourceReader) motionDifference(previous []float32, scale float32, ctx int) (values [3]float32, err error) {
|
||||
signs := r.value(ctx, true)
|
||||
if signs >= 1<<len(previous) {
|
||||
return values, fmt.Errorf("invalid U3D motion difference signs")
|
||||
}
|
||||
for index, prior := range previous {
|
||||
difference := float64(r.value(context+1, false)) * float64(scale)
|
||||
difference := float64(r.value(ctx+1, false)) * float64(scale)
|
||||
if signs&(1<<index) != 0 {
|
||||
difference = -difference
|
||||
}
|
||||
|
||||
@@ -30,9 +30,9 @@ type u3dResourceReader struct {
|
||||
}
|
||||
|
||||
// value 读取指定上下文的压缩字段,首次使用时初始化算术区间
|
||||
// 入参: context 压缩上下文, small 是否为8位字段
|
||||
// 入参: ctx 压缩上下文, small 是否为8位字段
|
||||
// 返回: uint32 字段值
|
||||
func (r *u3dResourceReader) value(context int, small bool) uint32 {
|
||||
func (r *u3dResourceReader) value(ctx int, small bool) uint32 {
|
||||
if r.err() != nil {
|
||||
return 0
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func (r *u3dResourceReader) value(context int, small bool) uint32 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return r.bits.dynamicValue(&r.hist[context], small)
|
||||
return r.bits.dynamicValue(&r.hist[ctx], small)
|
||||
}
|
||||
|
||||
// u32 读取普通32位字段,压缩开始后沿用当前算术区间
|
||||
|
||||
Reference in new issue
Block a user