From d8dc25293ef9d18616c89fd771ad5e0e0cc35195 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Mon, 12 Oct 2026 17:09:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=8E=8B=E7=BC=A9=E8=83=BD=E5=8A=9B):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BD=8D=E5=9B=BE=E9=99=8D=E9=87=87=E6=A0=B7?= =?UTF-8?q?=E4=B8=8E=E5=8E=8B=E7=BC=A9=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_compression_raster.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/ofdgo_compression_raster.go b/ofdgo_compression_raster.go index 555a8ae..cbbd5f0 100644 --- a/ofdgo_compression_raster.go +++ b/ofdgo_compression_raster.go @@ -123,10 +123,24 @@ func optimizeBMPResource(ctx context.Context, data []byte, options CompressionOp } // optimizeRasterResource 编码已解码的资源图片,同步降采样后的像素密度,仅采用更小结果 -// 绝对密度在降采样后量化为PNG整数,避免原始有理数密度被二次舍入 +// 绝对密度图片先降采样再编码,密度按最终尺寸换算后量化为PNG整数 // 入参: ctx 取消上下文, data 原编码, pixels 像素, options 压缩选项, size 像素需求, density 像素密度 // 返回: []byte 图片编码, error 编码或取消错误 func optimizeRasterResource(ctx context.Context, data []byte, pixels image.Image, options CompressionOptions, size image.Point, density rasterDensity) ([]byte, error) { + hasDensity := density.x != 0 || density.y != 0 + if hasDensity && density.unit == 1 { + before := pixels.Bounds() + if options.Mode == CompressionLossy { + var err error + pixels, err = pdfgo.ResizeImage(ctx, pixels, size) + if err != nil { + return nil, err + } + } + density.x *= float64(pixels.Bounds().Dx()) / float64(before.Dx()) + density.y *= float64(pixels.Bounds().Dy()) / float64(before.Dy()) + size = image.Point{} + } var encoded bytes.Buffer limit := &outputCompressionBuffer{buffer: &encoded, limit: outputOptimizationBufferLimit} if err := pdfgo.EncodePNG(ctx, limit, pixels); err != nil { @@ -136,19 +150,12 @@ func optimizeRasterResource(ctx context.Context, data []byte, pixels image.Image return nil, err } input := encoded.Bytes() - hasDensity := density.x != 0 || density.y != 0 if hasDensity && density.unit == 1 { var err error input, err = pdfgo.OptimizeImageSize(ctx, input, options, size) if err != nil { return nil, err } - config, _, err := image.DecodeConfig(bytes.NewReader(input)) - if err != nil { - return nil, err - } - density.x *= float64(config.Width) / float64(pixels.Bounds().Dx()) - density.y *= float64(config.Height) / float64(pixels.Bounds().Dy()) } if hasDensity { x, y := math.Round(density.x), math.Round(density.y)