Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Image file formats

Practical notes on common raster image file formats, what they store, how they compress pixels, and how to compute storage sizes and compression ratios for exam problems.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Images are everywhere in multimedia systems. An engineer chooses a file format when saving, transmitting or processing pictures to meet constraints of quality, file size, editing features and compatibility. Knowing formats lets you predict storage, choose lossless vs lossy, and read or write headers and metadata for applications such as web pages, cameras, and image processing pipelines.

Key ideas

  • Indexed colour (palette): A representation where pixels store an index into a colour lookup table (CLUT) or palette. Definition: indexed colour image stores pixel values as indices (usually 1, 2, 4, or 8 bits) which reference palette entries that hold full colour values (typically 24-bit RGB). Draw: three blocks labelled Palette (256 entries) -> Colour values; Pixel array stores indices.

  • Uncompressed formats: Store raw pixel samples. Example: BMP (Windows Bitmap) and some TIFF variants. Exact description: an uncompressed raster file layout contains a file header, optionally a palette, and a contiguous array of pixel samples row by row (sometimes padded). Assume row alignment rules (e.g. BMP rows padded to 4 bytes).

  • Lossless compression: Compression that allows exact reconstruction of original pixels. Common methods in images: Run-Length Encoding (RLE), Lempel–Ziv–Welch (LZW), and DEFLATE (combination of LZ77 + Huffman). Examples: GIF uses LZW, PNG uses DEFLATE.

  • Lossy compression: Compression that discards information and does not allow exact reconstruction. Common example: JPEG uses block-based Discrete Cosine Transform (DCT), quantisation and entropy coding. Exact definition: lossy means decompressed_image != original_image in general; goal is reduced perceptual difference.

  • File header and metadata: Most formats have a header with format identifier, width, height, bit depth, colour type, and may contain metadata fields such as EXIF (camera model, orientation), IPTC, or XMP. Draw: box labelled Header -> fields: format id, W, H, bpp, compression, metadata; followed by Image Data blocks.

  • Common image formats (short precise definitions):

    • BMP: Simple raster with header, optional palette; often uncompressed. Use for raw storage or Windows interoperability. Default pixel format is BGR, and scanlines are stored bottom-up (unless height is negative); rows are padded to 4-byte boundaries; some variants support simple RLE for 4/8-bit images.
    • GIF: Indexed colour (max 256 colours), uses LZW compression, supports simple animation via multiple frames, supports binary transparency (one colour index transparent). Limited to 8-bit indices.
    • PNG: Portable Network Graphics. Lossless compressed (DEFLATE). Supports truecolour (24/32-bit), indexed colour, alpha transparency, and gamma/chromatic metadata. Uses filtering before compression. Supports 8- or 16-bit per channel.
    • JPEG: Joint Photographic Experts Group standard for lossy compression of continuous-tone images. Uses 8x8 DCT blocks, quantisation tables, and entropy coding (Huffman or arithmetic). Typically encodes in YCbCr with chroma subsampling (e.g., 4:2:0); baseline and progressive modes exist. Not suited for images with sharp edges or limited colours (banding, artefacts possible). Baseline JPEG does not support an alpha channel.
    • JPEG 2000: Uses wavelet transform (discrete wavelet transform) and offers better scalability and region-of-interest coding; supports lossy and lossless modes.
    • TIFF: Tagged Image File Format. A container that can hold uncompressed or compressed data (LZW, DEFLATE, JPEG-in-TIFF), multiple pages/frames and rich metadata. Used in imaging and printing workflows.
    • RAW: Raw sensor data from cameras (manufacturer-specific). Stores minimally processed sensor samples and metadata; needs demosaicing to get RGB. Typical sensor bit depth is 10–14 bits per photosite with a Bayer colour filter array; not directly viewable without demosaicing and colour space conversion.
    • WebP / HEIF: Modern formats combining advanced compression (predictive + transform) to give smaller sizes than JPEG for similar quality. HEIF (container) often uses HEVC-based intra-image coding (HEIC). WebP supports lossy and lossless modes, alpha, and animation; HEIF can also wrap AV1-coded still images (AVIF).
  • Compression ratio and trade-offs: compression ratio = original_size / compressed_size. Higher ratio gives smaller file but usually lower fidelity for lossy methods. For lossless, ratio depends on image redundancy and algorithm efficiency.

  • Compatibility and use cases: Use PNG for lossless images and transparency, GIF for simple animations or very small palette images, JPEG for photographs, TIFF/RAW where editing/archival with metadata is required. Where supported, prefer modern formats such as WebP/AVIF (HEIF/AV1) or HEIC (HEIF/HEVC) for smaller files at similar quality, with options for transparency/animation.

Formulas

FileSize_uncompressed = W · H · bpp / 8 where W = width (pixels), H = height (pixels), bpp = bits per pixel; units: bytes. Applies to raw raster with no padding or headers.

FileSize_indexed ≈ PaletteSize + (W · H · bits_per_index / 8) where PaletteSize = number_of_palette_entries · bytes_per_palette_entry (often 3 bytes for 24-bit RGB). Units: bytes. Applies when using indexed colour. Some formats store per-palette-entry transparency separately (e.g., PNG tRNS adds up to 1 byte per entry), so adjust PaletteSize if alpha is present.

CompressionRatio = OriginalSize / CompressedSize dimensionless. Use for any compression method.

Bytes_per_row_padded = ceil((W · bpp) / 32) · 4 where BMP rows often padded to 4-byte boundaries. Units: bytes. Applies to BMP row-alignment.

If you need syntax to remember for programming file I/O (brief):

  • Read header fields with fixed offsets, then read image data as arrays of bytes. Example: open file in binary mode, parse header, allocate W×H×channels buffer.

Worked examples

  1. Uncompressed 24-bit RGB image (BMP-like) — exam numerical Given: W = 1920 px, H = 1080 px, bit depth = 24 bpp. Calculate raw image data size and approximate BMP file size assuming 54-byte header and rows padded to 4 bytes. Steps:
  1. Formula: FileSize_uncompressed = W · H · bpp / 8.
  2. Compute: 1920 · 1080 · 24 / 8 = 1920 · 1080 · 3 bytes = 6,220,800 bytes.
  3. Row padding: bytes_per_row = 1920 · 3 = 5760; 5760 is divisible by 4 so no extra padding. Total image data = 6,220,800 B.
  4. Add header: 6,220,800 + 54 = 6,220,854 bytes. Answer: 6,220,854 bytes (≈ 5.93 MiB).
  1. Indexed 8-bit GIF with 256-colour palette — exam numerical Given: W = 800 px, H = 600 px, palette entries = 256, palette entry size = 3 bytes (RGB). Pixel indices stored as 8 bits per pixel. Ignore LZW overhead; compute baseline storage: palette + pixel indices + small header (assume 100 bytes). Steps:
  1. PaletteSize = 256 · 3 = 768 bytes.
  2. Pixel index array = 800 · 600 · 1 byte = 480,000 bytes.
  3. Total ≈ 480,000 + 768 + 100 = 480,868 bytes. Answer: 480,868 bytes (≈ 469.6 KiB). Note: real GIF with LZW will be smaller; this is the uncompressed baseline.
  1. JPEG compression ratio example — algorithmic exam format Given: Original uncompressed image from example 1 (6,220,800 bytes). After JPEG compression the file size is 450 KB. Compute compression ratio and percent reduction. Steps:
  1. Convert JPEG size to bytes: 450 KB = 450,000 bytes (use decimal kilo as common for file sizes in many contexts — check university convention).
  2. CompressionRatio = 6,220,800 / 450,000 ≈ 13.824.
  3. Percent reduction = (1 - 1/CompressionRatio) · 100% = (1 - 450,000 / 6,220,800) · 100% ≈ 92.77%. Answer: Compression ratio ≈ 13.8:1; size reduced by ≈ 92.8%.

Common mistakes

  • Assuming JPEG supports transparency: baseline JPEG has no alpha channel.
  • Forgetting row padding in BMP when computing file size — leads to small but wrong answers.
  • Confusing container (TIFF, HEIF) with a single compression algorithm — TIFF can carry many types of compression.
  • Using KiB vs KB without stating convention; exams may expect binary (KiB = 1024 bytes) or decimal (KB = 1000 bytes). State and use one consistently.

For your exam

Typical question types:

  • Definitions: define raster, bit depth, indexed colour, lossless/lossy.
  • Short notes: compare PNG, JPEG, GIF and TIFF, or describe EXIF fields.
  • Derivations / calculations: compute file sizes for uncompressed and indexed images, include header and padding; compute compression ratio.
  • Algorithms / traces: outline LZW steps on a short string or explain DCT quantisation for a simple 8×8 block (universities may ask a stepwise DCT explanation rather than full numeric transform).
  • Diagrams: draw file layout for BMP/PNG/TIFF showing header, palette, data blocks and metadata. What a full-marks answer includes:
  • Precise definitions with units where applicable (pixels, bits), clear diagrams labelled (W, H, pixel grid), exact formula used, step-by-step numeric substitution with units and final answer with unit. For compression questions, state whether ratio is decimal or binary and include header/metadata sizes or padding assumptions.

Quick check

  1. What file format is best for a photograph with small file size but some loss of detail?
  2. How many bytes are needed to store a 100×100 image at 8 bpp (no palette)?
  3. Name a lossless and a lossy image compression method. Answers: 1) JPEG. 2) 100·100·8/8 = 10,000 bytes. 3) Lossless: PNG (DEFLATE), LZW; Lossy: JPEG (DCT-based).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?