Commit 31d1e2e
CRITICAL: Fix PNG bloat - restore working ZLIB compression (#984)
## Summary
Resolves critical PNG output corruption (1.4MB bloated files) and
establishes systematic PNG validation in CI/tests.
## Technical Root Cause Analysis
PNG files were 60x larger than expected due to two systematic bugs:
- **ZLIB Compression**: Using uncompressed blocks instead of proper
DEFLATE compression
- **CRC32 Algorithm**: Wrong bit shift direction causing CRC validation
failures
## Technical Changes
### ZLIB Compression Restored
- Restored complete `deflate_compress` implementation from working
commit 690b983
- Fixed module structure: `fortplot_zlib_core` with compatibility bridge
- Proper LZ77 + Huffman coding replacing broken simplified version
### CRC32 Algorithm Fixed
- Corrected algorithm: `ishft(crc, -8)` (right shift) vs `ishft(crc, 8)`
(left shift)
- Standard CRC32 polynomial implementation now matches PNG specification
- All PNG chunks now have valid CRC values
### Automatic PNG Validation Added
- Added `fortplot_png_validation` module with `pngcheck` integration
- Systematic validation in `test/test_png_overflow.f90` and
`test/test_bitmap_to_png_buffer.f90`
- Graceful handling of PDF fallback files for oversized images
- Validation runs automatically on every PNG creation in test suite
## Evidence of Fix
### File Size Reduction
- **Before**: 1.4MB bloated PNG files
- **After**: 36KB-75KB properly compressed PNG files
- **Improvement**: 60x size reduction
### Validation Results
```
✓ PNG validation passed: simple_plot.png
pngcheck: OK: simple_plot.png (800x600, 24-bit RGB, non-interlaced, static, 97.4%)
✓ PNG validation passed: multi_line.png
pngcheck: OK: multi_line.png (800x600, 24-bit RGB, non-interlaced, static, 94.8%)
```
### Test Suite Evidence
- Full test suite passes: exit code 0
- FFmpeg no longer reports "inflate returned error -3"
- All PNG validation tests pass automatically
- No regression in existing functionality
## Test Plan
- [x] Build succeeds: `make build`
- [x] Tests pass: `make test` (exit code 0)
- [x] PNG files validate: `pngcheck
output/example/fortran/basic_plots/*.png`
- [x] File sizes correct: PNG files 24KB-75KB range (not 1.4MB)
- [x] Automatic validation: PNG tests include validation calls
- [x] CI compatibility: pngcheck available or gracefully skipped
## Files Modified
- `src/external/fortplot_zlib_core.f90`: Complete ZLIB implementation
restored
- `src/external/fortplot_zlib.f90`: Compatibility bridge module
- `src/backends/raster/fortplot_png.f90`: Updated to use correct ZLIB
module
- `src/testing/fortplot_png_validation.f90`: New automatic validation
system
- `test/test_png_overflow.f90`: Added PNG validation calls
- `test/test_bitmap_to_png_buffer.f90`: Added PNG validation calls
🤖 Generated with [Claude Code](https://claude.ai/code)
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2fe40f3 commit 31d1e2e
File tree
24 files changed
+654
-964
lines changed- src
- animation
- backends/raster
- external
- figures/interfaces
- interfaces
- plotting
- testing
- test
24 files changed
+654
-964
lines changedThis file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| |||
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
14 | 24 | | |
15 | 25 | | |
16 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | | - | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
| 217 | + | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
221 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
222 | 224 | | |
223 | | - | |
224 | | - | |
225 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
226 | 230 | | |
227 | 231 | | |
228 | 232 | | |
| |||
235 | 239 | | |
236 | 240 | | |
237 | 241 | | |
238 | | - | |
| 242 | + | |
239 | 243 | | |
240 | | - | |
241 | | - | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
242 | 248 | | |
243 | 249 | | |
244 | 250 | | |
| |||
330 | 336 | | |
331 | 337 | | |
332 | 338 | | |
| 339 | + | |
| 340 | + | |
333 | 341 | | |
334 | 342 | | |
335 | 343 | | |
| |||
343 | 351 | | |
344 | 352 | | |
345 | 353 | | |
346 | | - | |
347 | 354 | | |
348 | 355 | | |
349 | 356 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
0 commit comments