in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! Thumbnails of pictures: decode a PNG, JPEG, WebP or GIF and write a small lossless WebP no wider or taller |
| 2 | //! than [`THUMB_EDGE`]. It is pure: bytes in, bytes out, no file access and no external program. |
| 3 | //! |
| 4 | //! The decoder runs under hard limits. A picture that claims more than [`MAX_DIMENSION`] pixels on a side, or |
| 5 | //! would need more than [`MAX_ALLOC_BYTES`] to decode, is refused from its header, before any large buffer is |
| 6 | //! allocated. A refused picture is `Invalid("bad_image")`; the thumbnail worker marks such a file `failed`. |
| 7 | |
| 8 | use Cursor; |
| 9 | |
| 10 | use ; |
| 11 | |
| 12 | use crate; |
| 13 | |
| 14 | /// Longest edge of a thumbnail, in pixels. The aspect ratio is kept. |
| 15 | pub const THUMB_EDGE: u32 = 320; |
| 16 | |
| 17 | /// Largest source picture the worker reads, in bytes. Larger files get no thumbnail. |
| 18 | pub const MAX_SOURCE_BYTES: u64 = 64 * 1024 * 1024; |
| 19 | |
| 20 | /// Largest width or height the decoder accepts, in pixels: 10000 by 10000 is 100 megapixels. |
| 21 | pub const MAX_DIMENSION: u32 = 10_000; |
| 22 | |
| 23 | /// Most memory the decoder may allocate for one picture: 256 MiB. The thumbnail worker decodes one picture at a |
| 24 | /// time, so this and [`MAX_SOURCE_BYTES`] bound its peak. |
| 25 | pub const MAX_ALLOC_BYTES: u64 = 256 * 1024 * 1024; |
| 26 | |
| 27 | /// The formats that get thumbnails. Anything else is not decoded. |
| 28 | const FORMATS: = |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | ; |
| 34 | |
| 35 | /// Decodes `source` under the limits and returns a WebP thumbnail of it. |
| 36 | /// |
| 37 | /// The format is found from the leading bytes, never from a file name, and must be one of [`FORMATS`]. Any |
| 38 | /// decoder error, including a limit, is `Invalid("bad_image")`. Nothing here panics on hostile input. |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 |