Skip to content

yaroslavesev/image-transform

Repository files navigation

📸 Image Transform Utility

A simple, yet powerful C-based utility for applying geometric transformations to BMP images. This project was built with modular design in mind, making it easy to extend, maintain, and experiment with new image formats and transformations.


Features

  • Efficient handling of BMP files (24-bit format).
  • Various transformation options:
    • 🔄 Rotate 90° Clockwise (cw90)
    • 🔄 Rotate 90° Counterclockwise (ccw90)
    • 🔁 Horizontal Flip (fliph)
    • 🔃 Vertical Flip (flipv)
    • ➡️ No Transformation (none)

🔧 How to Use

Run the utility using the following syntax in the terminal:

./image-transform <source-image> <transformed-image> <transformation>

Example:

./image-transform input.bmp output.bmp cw90

Available Transformations

Transformation Description
none No transformation; output = input
cw90 Rotate the image 90° clockwise
ccw90 Rotate the image 90° counterclockwise
fliph Flip the image horizontally
flipv Flip the image vertically

📁 Project Structure

The project adheres to modular architecture, dividing the responsibilities into different modules to ensure maintainability and extendability.

/solution
    ├── /src          # Source files (C files)
    ├── /include      # Header files (definitions and declarations)
    ├── /build        # Compiled binaries
    ├── /tester       # Test cases and example BMP images

🏗️ Building the Project

The project uses CMake for its build system. Follow the instructions below to get started:

Prerequisites

  • CMake version 3.12 or higher
  • A C compiler (GCC or Clang)
  • Optional tools:
    • AddressSanitizer (ASan), LeakSanitizer (LSan), and UndefinedBehaviorSanitizer (UBSan) for debugging.
    • clang-tidy for static code analysis.

Build Steps:

  1. Clone the repository:

    git clone https://github.com/yourusername/image-transform.git
    cd image-transform
  2. Generate the build files:

    cmake -S . -B build
  3. Compile the project:

    cmake --build build
  4. Run the tests:

    ctest --test-dir build

🖼️ BMP File Format

The project handles BMP images by interpreting their headers and pixel data using the following structures:

struct bmp_header {
    uint16_t bfType;
    uint32_t bfileSize;
    uint32_t bfReserved;
    uint32_t bOffBits;
    uint32_t biSize;
    uint32_t biWidth;
    uint32_t biHeight;
    uint16_t biPlanes;
    uint16_t biBitCount;
    uint32_t biCompression;
    uint32_t biSizeImage;
    uint32_t biXPelsPerMeter;
    uint32_t biYPelsPerMeter;
    uint32_t biClrUsed;
    uint32_t biClrImportant;
} __attribute__((packed));

Pixel Structure:
Each pixel is represented using 3 bytes for the blue, green, and red channels:

struct pixel {
    uint8_t b, g, r;
};

⚙️ Transformations Explained

Transformations are applied to the image in memory using the following internal image representation:

struct image {
    uint64_t width, height;
    struct pixel* data;
};

Examples of transformations include:

  • Rotating the image by creating a new image with swapped dimensions.
  • Flipping by rearranging pixel rows or columns.

🛠️ Extending the Project

Want to add more transformations or image formats? The project’s modular structure makes it simple:

  1. New Image Formats:
    Implement additional formats by adding new modules for deserialization and serialization, similar to from_bmp and to_bmp.

  2. New Transformations:
    Add transformations by implementing functions that manipulate the struct image.


🛡️ Error Handling and Testing

Robust error handling ensures that:

  • Memory allocation issues return appropriate error codes (e.g., ENOMEM).
  • File I/O errors are properly handled with warnings or termination.

To aid development, sanitizers are used to detect issues like memory leaks, buffer overflows, or uninitialized memory reads.

Sanitizers Available:

  • ASan: Detects invalid memory access.
  • LSan: Detects memory leaks.
  • UBSan: Detects undefined behavior.

🧪 Testing

Test cases are provided in the tester/ directory. Use CTest to verify your implementation.


🤝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests. To contribute, please:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature").
  4. Push to the branch (git push origin feature-name).
  5. Open a pull request.

📚 Further Reading


🏅 Why This Project?

This pet project is not just about manipulating BMP images. It's an excellent opportunity to:

  • Practice low-level programming in C.
  • Learn modular design and code maintainability.
  • Get hands-on experience with error handling, memory management, and testing tools.

Feel free to experiment, extend, and share your ideas! 😊

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5