Intro

I’m familiar with how text files (of different types) are stored in a computer, it’s just bytes representing UNICODE characters.

But when it comes to media, I’m not too sure. I know it’s all bytes as well but the how is not known to me. This document will explore the ways these different media formats are stored. The difference lies in how they’re interpreted by programs and systems. // It is indeed just arrays of bytes.

Storage

Physical

On Physical storage devices such as hard-drives (disks), SSDs, flash drives, etc, all files are stored as a bunch of bits (0s and 1s), grouped in blocks or sectors that’s managed by the OS’ FileSystem.

HDDs and Other Spinning Disks

The bytes are stored as magnetic patterns on the disk/platter, these patterns are structures representing sequences of 0s and 1s.

SSDs and Flash Memory

The bits are stored as electric charges in the memory cells, some charge is 1, none is 0. // This is rather simplified...

File Systems

An OS uses a file-system (NTFS, FAT32, ext4, etc) to determine where a file’s data starts and where it ends, it reads (interprets) the blocks belonging to that file.

Every OS has an index of sorts that stores that contains information about all the different file types that your computer may encounter, hundreds if not thousands of file types (extensions), that points at a file’s name and metadata.

Formats

Standard formats such as MP3, MP4, PNG, JPEG, etc, are agreed upon and have well-defined ways of interpreting the bytes to reproduce the media. These files also have “headers” to identify the file type, size, and other metadata.

Memory

When media is read and written into memory, the data’s also a sequence of bytes. Then programs/applications decode these sequences and interpret them according to the format standard they belong to.

The hardware doesn’t care about the meaning, it’s all just bytes in memory blocks.

Image Representation in Memory

A digital image is made of Pixels, a pixel is the smallest unit comprising an image.

Each image has pixel-composition and color-representation.

  • Pixel Composition: Pixels are the smallest visible elements in an image, often represented as tiny squares on a screen.
  • Color Representation: In the RGB color model, each pixel’s color is determined by a blend of red, green, and blue. The intensity of each color can vary, leading to the creation of millions of different shades and tones.

Color Models and Depths

These values define how colors are represented in a digital image.

  • RGB: mixing red, green, and blue light in various intensities to produce a broad array of colors.
  • Color Depth: It refers to the number of bits used to represent the color of each pixel. Higher color depth allows for more colors and finer shades. For instance, 24-bit color depth offers about 16.7 () million colors.

Image Formats

There are many formats and compression algorithms for images, each plays a role in the image ecosystem.

Common formats include:

  • JPEG (Joint Photographic Experts Group): Widely used for digital photography, JPEG is known for its efficient lossy compression, which reduces file size significantly but can lead to a loss in image quality.
  • PNG (Portable Network Graphics): The PNG format is preferred for storing graphics, text, and images with transparent backgrounds. It uses lossless compression, preserving image quality at the cost of larger file sizes.
  • GIF (Graphics Interchange Format): the GIF is suitable for simple images with fewer colors. It’s mainly used for short, looping video clips on the internet.

Compression

If images where stored as-is, their files would be massive and the ability to store them diminishes greatly. This is where CompressionAlgorithms come into play. These algos encode the data using fewer bits than the raw original.

Lossless Compression

Compression with perfect preservation, i.e. no quality loss. The original data can be restored at any time. This is crucial for use cases such as medical-imaging and professional photography. Techniques and Formats: Common lossless compression techniques include run-length encoding, Huffman coding, and Lempel-Ziv-Welch (LZW) compression. Examples include PNG and GIF Use Cases: Ideal for images requiring high-quality graphics, text, or images with transparency, like logos, illustrations, or scanned documents.

Lossy Compression

Some data is lost during the image’s compression, this is usually data that’s not noticeable most of the time. The more the data is compressed, the smaller the file size, but the lower the image quality… Techniques and Formats: Techniques include reducing color depth and using Discrete Cosine Transform (DCT), as seen in the JPEG format, which is widely used for digital photography due to its efficient compression. Use Cases: Suited for web images, photographs, and any application where file size is more critical than perfect image fidelity.


References