Skip to content

内嵌Jpg图像高度错误,导致文档结构查看器无法查看图像 #303

@Charltsing

Description

@Charltsing

这个文件是康泰克斯扫描仪生成的PDF,里面的jpg图像高度写成了65000,PDF字典的高度数据是4848。

PDF 规范说“如果尺寸或色彩信息与图像数据本身不一致,结果未定义(undefined)。”

Acrobat的做法是当两者不一致的时候,选择相信PDF字典。

所以,可以考虑加个补丁,读取jpg数据的SOF0段(PDF图像应该没有渐进的)中的宽度和高度数据,如果与PDF字典中的不一致,则把字典宽高数据写入Jpg数据中,这样导出的图像就可以看了。

内嵌DCT高度错误.zip

for (int i = 0; i < bytes.Length - 1; i++)
{
    if (bytes[i] == 0xff && bytes[i + 1] == 0xc0)  //找到SOF0段。2标记+2长度+1精度+2高度+2宽度+1分量个数
    {                            
        int jpgheight = (bytes[i + 5] << 8) | bytes[i + 6];
        int jpgwidth = (bytes[i + 7] << 8) | bytes[i + 8];
        
        if (info.Width > 0 && info.Width <= 65535 && info.Width != jpgwidth)
        {
            //优先相信 PDF 字典            
            bytes[i + 7] = (byte)(info.Width >> 8);
            bytes[i + 8] = (byte)(info.Width & 0xFF);            
        }

        if (info.Height > 0 && info.Height <= 65535 && info.Height != jpgheight)
        {
            //优先相信 PDF 字典            
            bytes[i + 5] = (byte)(info.Height >> 8);
            bytes[i + 6] = (byte)(info.Height & 0xFF);            
        }
        break;
    }
}

Metadata

Metadata

Assignees

Labels

改进New feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions