Two different contracts
The key question is what a decoder must recover. A lossless method promises the same original sequence within its defined input domain. A lossy method permits some change, ideally selected to suit the purpose of the data. The difference is a correctness contract, not a simple judgment about whether one file looks good.
Google’s WebP documentation describes both lossless and lossy image modes, illustrating that a container or format family can support more than one contract. A file extension alone therefore may not tell you everything about how the information was encoded. WebP compression techniques.
An exact-data example
Imagine a file containing a list of invoice identifiers. The values must survive storage and transmission exactly. Turning identifier 80142 into 80141 is not an acceptable visual approximation. A suitable lossless decoder should reproduce the original bytes, subject to the stated format and valid-input rules.
Testing this case can be straightforward: encode a known input, decode it, and compare the complete output with the original. Include empty input, repeated data, unusual byte values, and malformed compressed streams. A successful comparison on one example establishes only that example; a reusable implementation needs a much broader correctness test set.
Some compressed containers include checksums to help detect accidental corruption. RFC 1950 documents the zlib format and its Adler-32 check value. That is useful error detection, but it should not be confused with cryptographic authentication or proof that arbitrary content is trustworthy. Zlib format.
A deliberately lossy example
Suppose an illustrative sensor system records temperatures to the nearest whole degree because the downstream task only needs coarse categories. Values 21.1 and 21.4 may both become 21. The mapping has intentionally lost information: after decoding, the original decimal value cannot be uniquely reconstructed.
Whether that is acceptable depends on the task. A rough room-comfort display and a laboratory calibration system can have very different error requirements. Define the permitted error before selecting the representation. An attractive storage reduction cannot justify silently changing the output contract.
The same principle applies to images. An altered pixel value might be visually negligible in one use and unacceptable in another, such as exact image comparison. “Looks the same to me” is one observation, not a universal correctness criterion.
Include the surrounding information
A format may carry metadata, color information, transparency, or multiple encoded components. A complete test must state what counts as the original: just decoded sample values, or the complete file including metadata? WebP’s container specification provides a concrete example of a format that carries more than one kind of information. WebP container.
Re-encoding an image can preserve its visible pixels while changing metadata or file organization. That is different from byte-for-byte preservation of the entire source file. State the comparison level explicitly so the test matches the intended guarantee.
Make the tradeoff measurable
Record original bytes, encoded bytes, encoding and decoding cost, supported input types, and the exact recovery requirement. For a lossy method, add a declared error or quality assessment tied to the application. Avoid treating a compression ratio from one dataset as a guarantee for unrelated inputs.
Start with run-length encoding for an exact and inspectable example, then read entropy to understand why data distributions place limits on lossless compression.