Compare commits
72
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54a2d44a53 | ||
|
|
81c228beb8 | ||
|
|
c140c5b776 | ||
|
|
241ddd7f6d | ||
|
|
8ed07bf8db | ||
|
|
328648f57f | ||
|
|
ee2180a905 | ||
|
|
33bc2f29e6 | ||
|
|
39911cd662 | ||
|
|
d2557b84cf | ||
|
|
c51545ebef | ||
|
|
eeffb8766b | ||
|
|
e50c8d4c2f | ||
|
|
2a168f8947 | ||
|
|
4a181348b5 | ||
|
|
7745967382 | ||
|
|
56d26fe723 | ||
|
|
ff25fad5ae | ||
|
|
9310b9c1f2 | ||
|
|
3d00cfd2a7 | ||
|
|
854f5c2ba6 | ||
|
|
5367c71c34 | ||
|
|
592045db60 | ||
|
|
5fef946de3 | ||
|
|
55db25040a | ||
|
|
1b7eb6f076 | ||
|
|
f76b034a5b | ||
|
|
19492f71a3 | ||
|
|
25f786043a | ||
|
|
9f9c4784b6 | ||
|
|
e2436b57ac | ||
|
|
babb721f15 | ||
|
|
9595f0ec1b | ||
|
|
e2782a3a44 | ||
|
|
0a1d1c4607 | ||
|
|
9b8a883e68 | ||
|
|
18061cb7b8 | ||
|
|
048ef5c8d8 | ||
|
|
b725a006bf | ||
|
|
5fb7893518 | ||
|
|
65241c1d06 | ||
|
|
bf591f0c1b | ||
|
|
03512290d0 | ||
|
|
5d2fae460f | ||
|
|
df6665336e | ||
|
|
b03e5ba882 | ||
|
|
0ad76b8ad5 | ||
|
|
c0bfdb484d | ||
|
|
b986e7394e | ||
|
|
6ade1b7a5e | ||
|
|
adcb295a5e | ||
|
|
3858e463ee | ||
|
|
53090ecd78 | ||
|
|
1f9ff8fe3a | ||
|
|
5d8804fdeb | ||
|
|
49748c8795 | ||
|
|
f20499439c | ||
|
|
50d17af073 | ||
|
|
64cf77bbe3 | ||
|
|
05a15d38af | ||
|
|
319a6e3b2c | ||
|
|
36fb0ebf32 | ||
|
|
1bcf91e9a2 | ||
|
|
e3b2e7dd2f | ||
|
|
4782c3e876 | ||
|
|
f6bc756839 | ||
|
|
8f66878367 | ||
|
|
59b89a61b5 | ||
|
|
ce216b1879 | ||
|
|
cd3bf2ddb9 | ||
|
|
f129a0d50f | ||
|
|
7315d5005d |
No files matched your search
@@ -17,3 +17,4 @@ AllowShortIfStatementsOnASingleLine: AllIfsAndElse
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
IndentAccessModifiers: false # Indent access specifiers by one level
|
||||
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
|
||||
# SpacesInBlockCommentsStyle: Always
|
||||
@@ -1,3 +1,5 @@
|
||||
build/
|
||||
.cache/
|
||||
assets/
|
||||
.DS_Store
|
||||
assets/
|
||||
+25
-13
@@ -1,22 +1,34 @@
|
||||
cmake_minimum_required(VERSION 4.2.1)
|
||||
cmake_minimum_required(VERSION 4.4.3)
|
||||
project(libimg LANGUAGES C)
|
||||
|
||||
set(CMAKE_C_STANDARD 23)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
include_directories(include)
|
||||
project(libgl LANGUAGES C)
|
||||
|
||||
set(SOURCES
|
||||
src/main.c
|
||||
set(LIB_SOURCES
|
||||
src/transformations.c
|
||||
src/utils.c
|
||||
src/filters.c
|
||||
src/imgio.c
|
||||
)
|
||||
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(PROCESSOR
|
||||
GIT_REPOSITORY https://git.moomancer.xyz/DarthMooMancer/libimg.git
|
||||
GIT_TAG main
|
||||
GIT_SHALLOW TRUE
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(TURBOJPEG REQUIRED IMPORTED_TARGET libturbojpeg)
|
||||
|
||||
add_library(imgproc STATIC ${LIB_SOURCES})
|
||||
|
||||
target_include_directories(imgproc
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
PRIVATE
|
||||
${TURBOJPEG_INCLUDE_DIRS}
|
||||
)
|
||||
FetchContent_MakeAvailable(PROCESSOR)
|
||||
|
||||
add_executable(libgl ${SOURCES})
|
||||
target_link_libraries(libgl PRIVATE PROCESSOR)
|
||||
target_link_libraries(imgproc PUBLIC PkgConfig::TURBOJPEG)
|
||||
set_target_properties(imgproc PROPERTIES EXPORT_NAME imgproc)
|
||||
|
||||
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
|
||||
add_executable(libimg src/main.c)
|
||||
target_link_libraries(libimg PRIVATE imgproc)
|
||||
endif()
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2011-2026 The Bootstrap Authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,128 @@
|
||||
# LibIMG
|
||||
|
||||
_A lightweight, fast, and minimal library designed to be powerful for my current capabilities. This is a hobby project to learn bit operations and expanded to a full processor between two types --- PPM and JPEG. It's powerful for what it is and it is weak for what it could be._
|
||||
|
||||
> [Note]
|
||||
> All documentation is in the code and files itself. So to save myself time I would recommend going through the code itself.
|
||||
|
||||
> [Warning]
|
||||
> There are a lot of changes you can make to an image but this is not an extensive library to use mainstream. I would hope you understood that already. This is for a self made project learning bitwise for c originally and now has become bigger and cooler than expected.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. This library does require _```libturbojpeg```_ as manually writing the functions for JPEG would take a long time to figure out and I may come back to do later.
|
||||
2. ```C23 and CMake 4.4.3```, as there are some new features from it. Tested only on my M1 Macbook for compatibility.
|
||||
|
||||
## Types
|
||||
|
||||
Available
|
||||
|
||||
1. PPM (Originally just to learn with)
|
||||
2. JPEG (Added as having only PPM felt wrong)
|
||||
|
||||
Planned
|
||||
|
||||
3. PNG (Planned)
|
||||
4. SVG (We Shall See)
|
||||
|
||||
## Installation
|
||||
|
||||
You can build manually or pull it into a project
|
||||
|
||||
_To Build_:
|
||||
|
||||
```bash
|
||||
git clone https://git.moomancer.xyz/DarthMooMancer/libimg.git
|
||||
cd libimg
|
||||
cmake -S . -B build && cmake --build build
|
||||
./build/libimg
|
||||
```
|
||||
|
||||
_To pull, use FetchContent in your ```CMakeLists.txt```:_
|
||||
|
||||
|
||||
```Make
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(imgproc
|
||||
GIT_REPOSITORY https://git.moomancer.xyz/DarthMooMancer/libimg.git
|
||||
GIT_TAG main
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(imgproc)
|
||||
target_link_libraries(<project> PRIVATE imgproc)
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
_While it is not an extensive example, it shows many of the patterns for how to use all functions throughout the codebase._
|
||||
|
||||
```c
|
||||
/*
|
||||
* Keep in mind any function that creates a new buffer and returns it can
|
||||
* overwrite the old one, however it is lost. SO create a new variable and free
|
||||
* the old one after use.
|
||||
*
|
||||
* Functions (Specifically filters) that return unsigned int (not pointers) need
|
||||
* to be used as an input to the same point in a buffer.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* buf[i] = u32_grayscale(buf[i]);
|
||||
*
|
||||
* The buf[i] is replaced by the output of grayscale at buf[i]
|
||||
*/
|
||||
int
|
||||
main()
|
||||
{
|
||||
// Init base image containers
|
||||
PPM img;
|
||||
JPEG jimg;
|
||||
|
||||
// Add a file location of image to modify
|
||||
img.name = "../assets/test_2.ppm";
|
||||
|
||||
// Load the PPM image
|
||||
unsigned int* buf = ppm_load(img.name, &img);
|
||||
|
||||
// Check for null image load. In case image doesn't exist or fails to
|
||||
// load
|
||||
if (!buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
|
||||
// Blur image and save to new pointer
|
||||
unsigned int* blur = box_blur(buf, &img, 20);
|
||||
|
||||
// Free old buffer. MUST DO THIS. Maybe not immediately but before
|
||||
// ending or possibly leaking. Also it stays in heap and a large image
|
||||
// can take a lot of memory so better to clear anyways.
|
||||
free(buf);
|
||||
|
||||
// Check for null image load. In case image doesn't exist or fails to
|
||||
// load
|
||||
if (!blur)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
|
||||
// Loop image and apply filter to each pixel.
|
||||
for (int i = 0; i < img.len; i++)
|
||||
{
|
||||
blur[i] = u32_rotl(blur[i], 4);
|
||||
}
|
||||
|
||||
// Take the attributes from ppm image so JPEG file can be saved
|
||||
jimg = inherit_from_ppm(&img);
|
||||
|
||||
// Saves JPEG to location specified
|
||||
jpeg_save(blur, &jimg, "../assets/test_2_new.jpg");
|
||||
|
||||
// Make sure to free any buffer still active
|
||||
free(blur);
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,151 @@
|
||||
#ifndef FILTERS_H
|
||||
#define FILTERS_H
|
||||
#include "utils.h"
|
||||
|
||||
/**
|
||||
* @brief Packs new alpha channel
|
||||
* @details Takes a value from 0 - 255 for levels of opacity in image and packs
|
||||
* it into the first byte. 0 is transparent and 255 is fully opaque.
|
||||
* @param[in] clr Takes in a packed pixel
|
||||
* @param[in] alpha One byte value to be packed for image
|
||||
* @return unsigned int Packed color with new alpha channel.
|
||||
*/
|
||||
unsigned int
|
||||
u32_put_alpha(const unsigned int clr, const unsigned char alpha);
|
||||
|
||||
/**
|
||||
* @brief Inverts bits for color channels
|
||||
* @details Takes a packed color and inverts the RGB channel bits from 0 to 1
|
||||
* and vice versa. Then saves the alpha channel back to new packed image.
|
||||
* @param[in] clr Takes in a packed pixel
|
||||
* @return unsigned int Packed color with inverted RGB channels.
|
||||
*/
|
||||
unsigned int
|
||||
u32_invert(const unsigned int clr);
|
||||
|
||||
/**
|
||||
* @brief Inverts RED and BLUE channels
|
||||
* @details Takes a packed color and isolates the A and G channels, then shifts
|
||||
* the clr Red channel 16 Bits right and B channel 16 Bits left.
|
||||
* @param[in] clr Takes in a packed pixel
|
||||
* @return unsigned int Packed color with inverted RB channels.
|
||||
*/
|
||||
unsigned int
|
||||
u32_swap_rb(const unsigned int clr);
|
||||
|
||||
/**
|
||||
* @brief Grays pixels from RGB channels
|
||||
* @details Takes a packed color and merges RGB channels and sets all RGB
|
||||
* channels to new gray.
|
||||
* @param[in] clr Takes in a packed pixel
|
||||
* @return unsigned int Packed color with gray RGB channels.
|
||||
*/
|
||||
unsigned int
|
||||
u32_grayscale(const unsigned int clr);
|
||||
|
||||
/**
|
||||
* @brief Grays pixels from RGB channels
|
||||
* @details If greyscale value is less than Max then set color to transparent
|
||||
* and black. If greater, set color to opaque white. channels to new gray.
|
||||
* @param[in] clr Takes in a packed pixel
|
||||
* @param[in] max Max gray value for split
|
||||
* @return unsigned int Packed color with white or black RGB channels.
|
||||
*/
|
||||
unsigned int
|
||||
u32_step(const unsigned int clr, const unsigned char max);
|
||||
|
||||
/**
|
||||
* @brief Brightens Color
|
||||
* @details Uses a factor to multiply RGB channels to increase values closer to
|
||||
* 255 to raise brightness. Use x > 1 for a brighter pixel and x < 1 for a
|
||||
* darker pixel
|
||||
* @param[in] clr Takes in a packed pixel
|
||||
* @param[in] factor Used to multiply against RGB channels. Can be 0 < x > 1 but
|
||||
* NOT negative.
|
||||
* @return unsigned int Packed color with brighter or darker pixel
|
||||
*/
|
||||
unsigned int
|
||||
u32_brighten(const unsigned int clr, const float factor);
|
||||
|
||||
/**
|
||||
* @brief Combines RGB values between 2 packed colors
|
||||
* @details Takes and adds the respective RGB values between 2 colors and
|
||||
* divides by 2 to make sure each channel is below 255. So, even if 2 pixels are
|
||||
* white, the max values will stay 255.
|
||||
* @param[in] clr_a Packed Pixel A
|
||||
* @param[in] clr_b Packed Pixel B
|
||||
* @return unsigned int Packed ARGB pixel.
|
||||
*/
|
||||
unsigned int
|
||||
u32_mix(const unsigned int clr_a, const unsigned int clr_b);
|
||||
|
||||
/**
|
||||
* @brief Combines RGB values between 2 packed colors
|
||||
* @details Takes and adds the respective RGB values between 2 colors and
|
||||
* divides by 2 to make sure each channel is below 255. So, even if 2 pixels are
|
||||
* white, the max values will stay 255.
|
||||
* @param[in] clr_a Packed Pixel A
|
||||
* @param[in] clr_b Packed Pixel B
|
||||
* @return unsigned int Packed ARGB pixel.
|
||||
*/
|
||||
unsigned int
|
||||
u32_lerp(const unsigned clr_a, const unsigned int clr_b, const float ratio);
|
||||
|
||||
/**
|
||||
* @brief Returns a Packed Pixel of one Channel
|
||||
* @param[in] clr Packed Pixel
|
||||
* @param[in] keep Character of 'r', 'g', 'b', keeping the channel respectively.
|
||||
* @return unsigned int Packed Pixel with one channel kept, includes alpha auto
|
||||
*/
|
||||
unsigned int
|
||||
u32_mask(const unsigned int clr, const char keep);
|
||||
|
||||
/**
|
||||
* @brief Rotates the bits of each channel by n
|
||||
* @details Moves bits of channels left by n with the max being 8 and
|
||||
* recommended to use 0 < x < 8 instead of 8; 8 just returns the same pixel as
|
||||
* it is just rotated back to original position. Cannot be greater than 8
|
||||
* because the calculations would result in a negative.
|
||||
* @param[in] clr Packed Pixel
|
||||
* @param[in] dist Distance from original position to move each bit to the left.
|
||||
* @return unsigned int Packed Pixel with newly moved bits
|
||||
*/
|
||||
unsigned int
|
||||
u32_rotl(const unsigned int clr, const int dist);
|
||||
|
||||
/**
|
||||
* @brief Raises Color Vibrance
|
||||
* @details Raises the vibrance by essentially raising specific channels higher
|
||||
* and lowering others.
|
||||
* @param[in] clr Packed Pixel
|
||||
* @param[in] factor Floating point to raise or lower vibrance by.
|
||||
* @return unsigned int Packed Pixel with one channel kept, includes alpha auto
|
||||
*/
|
||||
unsigned int
|
||||
u32_contrast(const unsigned int clr, const float factor);
|
||||
|
||||
/**
|
||||
* @brief Averages Red and Blue Channels for Green Channel
|
||||
* @param[in] clr Packed Pixel
|
||||
* @return unsigned int Packed Pixel with green pixel averaged
|
||||
*/
|
||||
unsigned int
|
||||
u32_avg_gb(const unsigned int clr);
|
||||
|
||||
/**
|
||||
* @brief Blurs image in a radiance with a box
|
||||
* @details Uses 2 loops to scan from left to right; selecting a pixel and
|
||||
* mixing all of the pixels from "radius" left to "radius" right and averaging
|
||||
* them. Then it moves to the next pixel doing the same. Then it does the second
|
||||
* loop from top to bottom doing the same principle. This can take some time
|
||||
* depending on hardware, image size, radius, etc.
|
||||
* @param[in] buf The image buffer to be blurred
|
||||
* @param[in] metadata the PPM image container
|
||||
* @param[in] radius The amount of pixels around the left and right, then top
|
||||
* and bottom to mix with the currently selected pixel
|
||||
* @return unsigned int Pointer to new created image buffer.
|
||||
*/
|
||||
unsigned int*
|
||||
u32_box_blur(const unsigned int* buf, const PPM* metadata, const int radius);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef IMGIO_H
|
||||
#define IMGIO_H
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
/**
|
||||
* @brief Open and Fill buffer from .ppm file
|
||||
* @param [in] filename Location of the image to be modified
|
||||
* @param [in,out] metadata PPM pointer to store image information
|
||||
*/
|
||||
unsigned int*
|
||||
read_ppm(const char* filename, PPM* metadata);
|
||||
|
||||
/**
|
||||
* @brief Open and write buffer to file
|
||||
* @param [in] buffer Information of pixels
|
||||
* @param [in] metadata PPM pointer to store image information
|
||||
* @param [in] filename Name/Location for file
|
||||
*/
|
||||
int
|
||||
write_ppm(const unsigned int* buffer, const PPM metadata, const char* filename);
|
||||
|
||||
/**
|
||||
* @brief Open and Fill buffer from .jpg file
|
||||
* @details Opens a .jpg file, creates a new buffer and extracts the pixels into
|
||||
* said buffer as integers.
|
||||
* @param [in,out] metadata JPEG pointer to store image information
|
||||
*/
|
||||
unsigned int*
|
||||
read_jpeg(JPEG* metadata);
|
||||
|
||||
/**
|
||||
* @brief Open and write buffer to file
|
||||
* @param [in] buf Information of pixels
|
||||
* @param [in] metadata JPEG pointer to store image information
|
||||
* @param [in] filename Name/Location for file
|
||||
*/
|
||||
int
|
||||
write_jpeg(const unsigned int* buf, const JPEG* metadata, const char* filename);
|
||||
|
||||
/**
|
||||
* @brief Converts PPM to JPEG
|
||||
* @param [in] location points to file to use
|
||||
*/
|
||||
void
|
||||
transcode_to_jpeg(const char* location);
|
||||
|
||||
/**
|
||||
* @brief Converts JPEG to PPM
|
||||
* @param [in] location Pointer to JPEG container
|
||||
*/
|
||||
void
|
||||
transcode_to_ppm(const char* location);
|
||||
|
||||
/**
|
||||
* @brief Inherits the attributes from JPEG container
|
||||
* @param [in] metadata Pointer to JPEG container
|
||||
* @param [in] fmt Character for type of PPM image, binary or raw
|
||||
*/
|
||||
PPM
|
||||
inherit_from_jpeg(const JPEG* metadata, const char fmt);
|
||||
|
||||
/**
|
||||
* @brief Inherits the attributes from PPM container
|
||||
* @param [in] metadata Pointer to PPM container
|
||||
*/
|
||||
JPEG
|
||||
inherit_from_ppm(const PPM* metadata);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef TRANSFORMATIONS_H
|
||||
#define TRANSFORMATIONS_H
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
/**
|
||||
* @brief Flips image on X-Axis
|
||||
* @details Creates a new buffer to move pixels to and flips pixels on X-Axis,
|
||||
* freeing the old buffer at the end.
|
||||
* @param[in] buf Pointer to original pixel buffer, later freed.
|
||||
* @param[in] img Loaded PPM image for calculations
|
||||
* @return unsigned int Pointer to new pixel buffer
|
||||
* @retval NULL If original buffer is invalid or if new buffer fails to create
|
||||
* */
|
||||
unsigned int*
|
||||
u32_flop(const unsigned int* buf, const int width, const int height);
|
||||
|
||||
/**
|
||||
* @brief Flips image on Y-Axis
|
||||
* @details Creates a new buffer to move pixels to and flips pixels on Y-Axis,
|
||||
* freeing the old buffer at the end.
|
||||
* @param[in] buf Pointer to original pixel buffer, later freed.
|
||||
* @param[in] img Loaded PPM image for calculations
|
||||
* @return unsigned int Pointer to new pixel buffer
|
||||
* @retval NULL If original buffer is invalid or if new buffer fails to create
|
||||
* */
|
||||
unsigned int*
|
||||
u32_flip(const unsigned int* buf, const int width, const int height);
|
||||
|
||||
/**
|
||||
* @brief Rotates image on 90 Clockwise
|
||||
* @details Creates a new buffer to move pixels to and rotates pixels 90 degrees
|
||||
* clockwise around center of origin, freeing the old buffer at the end.
|
||||
* @param[in] buf Pointer to original pixel buffer, later freed.
|
||||
* @param[in] img Pointer to loaded PPM image for calculations
|
||||
* @return unsigned int Pointer to new pixel buffer
|
||||
* @retval NULL If original buffer is invalid, if image pointer is invalid or if
|
||||
* new buffer fails to create and is invalid
|
||||
* */
|
||||
unsigned int*
|
||||
u32_rotate(const unsigned int* buf, PPM* metadata);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,184 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
/*
|
||||
* 0 0 0 0 0 0 0 0
|
||||
* ^ - Starting Bit for all calculations
|
||||
* ^ - Starting bit for when I talk about sets of bits
|
||||
*/
|
||||
|
||||
// The u in the BYTE Macros are for unsigned calls so colors are not changed to
|
||||
// signed and the values cut in half
|
||||
#include <stddef.h>
|
||||
#define BYTE_A 0xFF000000u /**< Isolates the First Byte for alpha */
|
||||
#define BYTE_R 0x00FF0000u /**< Isolates the Second Byte for red */
|
||||
#define BYTE_G 0x0000FF00u /**< Isolates the Third Byte for green */
|
||||
#define BYTE_B 0x000000FFu /**< Isolates the Fourth Byte for blue */
|
||||
#define BYTE_RGB 0x00FFFFFFu /**< Isolates 24-Bits from end (RGB) */
|
||||
#define BYTE 0xFFu /**< Pulls out 8 shifted Bits */
|
||||
#define LONG_BYTE 0xFFFFu /**< Pulls out 16 shifted Bits */
|
||||
|
||||
#define SHIFT_8 8 /**< Constant shift of 8-Bits */
|
||||
#define SHIFT_16 16 /**< Constant shift of 16-Bits */
|
||||
#define SHIFT_24 24 /**< Constant shift of 24-Bits */
|
||||
|
||||
// These are success and error codes
|
||||
#define SUCCESS 0
|
||||
|
||||
#define FAILURE 1
|
||||
#define ERR_BUF_NULL 20
|
||||
#define ERR_FILE_NULL 21
|
||||
#define ERR_INVALID_SIZE 22
|
||||
#define ERR_CREATION_FAIL 23
|
||||
#define ERR_IMG_NULL 24
|
||||
#define ERR_IMG_NAME_INVALID 25
|
||||
#define ERR_JHEADER 26
|
||||
#define ERR_MALLOC_NULL 27
|
||||
|
||||
extern const char* error_messages[];
|
||||
|
||||
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
||||
#define LOGERROR(error_code) \
|
||||
printf("ERROR: %s\n", \
|
||||
error_messages[error_code]) /**< Print log error message*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Max value for any channel
|
||||
* @details For alpha channel this is the max opacity setting and 0 is fully
|
||||
* transparent, 255 is white for any color and 0 is black.
|
||||
*/
|
||||
#define MAX 255
|
||||
#define HALF_MAX \
|
||||
((int)(MAX / 2) + 1) /**< Half Max Color Value for any channel */
|
||||
|
||||
/**< Func to isolate alpha channel into variable */
|
||||
#define DECODE_A(clr) (((clr) >> SHIFT_24) & BYTE)
|
||||
/**< Func to isolate red channel into variable */
|
||||
#define DECODE_R(clr) (((clr) >> SHIFT_16) & BYTE)
|
||||
/**< Func to isolate green channel into variable */
|
||||
#define DECODE_G(clr) (((clr) >> SHIFT_8) & BYTE)
|
||||
/**< Func to isolate blue channel into variable */
|
||||
#define DECODE_B(clr) ((clr) & BYTE)
|
||||
|
||||
/**
|
||||
* @brief Packs RGB channels into int
|
||||
* @details Takes R, G, B char channels and packs them into a singular integer
|
||||
* with the first 8 Bits reserved for alpha channel (set to MAX by default). See
|
||||
* diagram at top for my thinking of starting bits
|
||||
* @param[in] r The red channel stored as char and packed into int
|
||||
* @param[in] g The green channel stored as char and packed into int
|
||||
* @param[in] b The blue channel stored as char and packed into int
|
||||
*
|
||||
*/
|
||||
#define ENCODE_RGB(r, g, b) \
|
||||
(((unsigned int)(MAX) << SHIFT_24) | ((unsigned int)(r) << SHIFT_16) | \
|
||||
((unsigned int)(g) << SHIFT_8) | (unsigned int)(b))
|
||||
|
||||
/**
|
||||
* @brief Packs ARGB channels into int
|
||||
* @details Takes A, R, G, B char channels and packs them into a singular
|
||||
* integer with the first 8 Bits reserved for alpha channel. See diagram at top
|
||||
* for my thinking of starting bits
|
||||
* @param[in] a The alpha channel stored as char and packed into int
|
||||
* @param[in] r The red channel stored as char and packed into int
|
||||
* @param[in] g The green channel stored as char and packed into int
|
||||
* @param[in] b The blue channel stored as char and packed into int
|
||||
*
|
||||
*/
|
||||
#define ENCODE_ARGB(a, r, g, b) \
|
||||
(((unsigned int)(a) << SHIFT_24) | ((unsigned int)(r) << SHIFT_16) | \
|
||||
((unsigned int)(g) << SHIFT_8) | (unsigned int)(b))
|
||||
|
||||
/**
|
||||
* @brief Stops values exiting 0 - 255 range
|
||||
* @details Takes in a value of X and checks if it is Above the Max of 255. If
|
||||
* so, then set to Max. If not, then simply set back to whatever X was before.
|
||||
* @param[in] x Color channel: char
|
||||
*/
|
||||
#define CLAMP_MAX(x) (((x) > MAX) ? MAX : (x))
|
||||
|
||||
/**
|
||||
* @brief Stops values exiting 0 - 255 range
|
||||
* @details Takes in a value of X and checks if it is Below 0. If so, then set
|
||||
* to 0. If not, then simply set back to whatever X was before.
|
||||
* @param[in] x Color channel: char
|
||||
*/
|
||||
#define CLAMP_MIN(x) (((x) < 0) ? 0 : (x))
|
||||
|
||||
/**
|
||||
* @brief Contains values for PPM Image
|
||||
* @details Can be defined with just a name and filled later with ppm_load
|
||||
* as it stores information for header of file not the pixels of the image
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int width; /**< Image width in pixels */
|
||||
int height; /**< Image height in pixels */
|
||||
int len; /**< Image length in pixels, width * height */
|
||||
char* name; /**< File name and location, relative */
|
||||
char fmt; /**< Raw or Binary values, P3 or P6 respectively*/
|
||||
} PPM;
|
||||
|
||||
/**
|
||||
* @brief Contains values for JPEG Image
|
||||
* @details Can be defined with just a name and filled later with jpeg_load as
|
||||
* it stores information for header of file not the pixels of the image
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int width; /**< Image width in pixels */
|
||||
int height; /**< Image height in pixels */
|
||||
int len; /**< Image length in pixels, width * height */
|
||||
char* name; /**< File name and location, relative */
|
||||
} JPEG;
|
||||
|
||||
/**
|
||||
* @brief Converts an int to long
|
||||
* @details Takes in 32-Bit packed color and separates the 4 color values into a
|
||||
* 64-Bit packed color. Used for bigger image calculations with one channel and
|
||||
* then dividing later.
|
||||
* @param[in] clr Takes a packed color, unsigned int
|
||||
* @return unsigned long Packed color in 4 ints instead of one
|
||||
*/
|
||||
unsigned long
|
||||
u32to_u64(const unsigned int clr);
|
||||
|
||||
/**
|
||||
* @brief Converts a long to int
|
||||
* @details Takes in a full packed color and compresses it to 1 Byte
|
||||
* @param[in] clr Takes a packed color, unsigned long
|
||||
* @param[in] u64_div Takes a divisor to break down to raw color
|
||||
* @return unsigned int Packed color in 1 int instead of 4
|
||||
*/
|
||||
unsigned int
|
||||
u64to_u32(const unsigned long clr, const unsigned long u64_div);
|
||||
|
||||
/**
|
||||
* @brief Prints quick debug info of container
|
||||
* @details Prints metadata like Name, Format, Width, Height and Length
|
||||
* important for debugging errors
|
||||
* @param[in] metadata Pointer to information of a PPM image
|
||||
*/
|
||||
void __attribute__((overloadable))
|
||||
print_attr(const PPM* metadata);
|
||||
|
||||
/**
|
||||
* @brief Prints quick debug info of container
|
||||
* @details Prints metadata like Name, Width, Height and Length
|
||||
* important for debugging errors
|
||||
* @param[in] metadata Pointer to information of a JPEG image
|
||||
*/
|
||||
void __attribute__((overloadable))
|
||||
print_attr(const JPEG* metadata);
|
||||
|
||||
/**
|
||||
* @brief Finds start of extension in image name
|
||||
* @details Loops through the character array starting from the end to find the
|
||||
* start of the extension.
|
||||
* @param[in] str Char array to loop through
|
||||
*/
|
||||
size_t
|
||||
find_extension(const char* str);
|
||||
|
||||
#endif
|
||||
Vendored
BIN
Binary file not shown.
+294
@@ -0,0 +1,294 @@
|
||||
#include "image/filters.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "image/utils.h"
|
||||
|
||||
unsigned int
|
||||
u32_put_alpha(const unsigned int clr, const unsigned char alpha)
|
||||
{
|
||||
return (clr & BYTE_RGB) | alpha << SHIFT_24;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_invert(const unsigned int clr)
|
||||
{
|
||||
return ((~clr) & BYTE_RGB) | (clr & BYTE_A);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_swap_rb(const unsigned int clr)
|
||||
{
|
||||
return (clr & 0xFF00FF00) | ((clr & BYTE_R) >> SHIFT_16) |
|
||||
((clr & BYTE_B) << SHIFT_16);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_grayscale(const unsigned int clr)
|
||||
{
|
||||
/*
|
||||
* WHY: The colors use the REC 601 Standard: In layman's terms they are
|
||||
* weighed in how the human eye perceives color. So green is the largest
|
||||
* because we are more sensitive to green and least to blue.
|
||||
* The Shift_8 is to divide 256 to bring the color value back into range
|
||||
of 0 - 255
|
||||
*/
|
||||
unsigned int gray = ((DECODE_R(clr) * 77) + (DECODE_G(clr) * 150) +
|
||||
(DECODE_B(clr) * 29)) >>
|
||||
SHIFT_8;
|
||||
|
||||
return (clr & BYTE_A) | (gray << SHIFT_16) | (gray << SHIFT_8) | gray;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_step(const unsigned int clr, const unsigned char max)
|
||||
{
|
||||
if (DECODE_B(u32_grayscale(clr)) < max)
|
||||
{
|
||||
return ENCODE_ARGB(0, 0, 0, 0);
|
||||
}
|
||||
return ENCODE_ARGB(MAX, MAX, MAX, MAX);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_brighten(const unsigned int clr, const float factor)
|
||||
{
|
||||
// NOTE: keep definition for readability
|
||||
unsigned int red = CLAMP_MAX(DECODE_R(clr) * factor);
|
||||
unsigned int green = CLAMP_MAX(DECODE_G(clr) * factor);
|
||||
unsigned int blue = CLAMP_MAX(DECODE_B(clr) * factor);
|
||||
return (clr & BYTE_A) | (red << SHIFT_16) | (green << SHIFT_8) | blue;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_mix(const unsigned int clr_a, const unsigned int clr_b)
|
||||
{
|
||||
// NOTE: keep definition for readability
|
||||
unsigned char blue = (DECODE_B(clr_a) + DECODE_B(clr_b)) / 2;
|
||||
unsigned char green = (DECODE_G(clr_a) + DECODE_G(clr_b)) / 2;
|
||||
unsigned char red = (DECODE_R(clr_a) + DECODE_R(clr_b)) / 2;
|
||||
|
||||
return ENCODE_ARGB(DECODE_A(clr_a), red, green, blue);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_lerp(const unsigned int clr_a, const unsigned int clr_b, const float ratio)
|
||||
{
|
||||
/*
|
||||
* A more powerful mix using floating points to more precisely and
|
||||
* smoothly mix colors from 2 pixels. Doesn't really work on PPM images
|
||||
* because they are whole number pixels. So JPEG are better because the
|
||||
* numbers result in floating point colors.
|
||||
* */
|
||||
// NOTE: keep definition for readability
|
||||
unsigned char blue =
|
||||
((DECODE_B(clr_a) * (1 - ratio)) + ((DECODE_B(clr_b) * ratio)));
|
||||
unsigned char green =
|
||||
((DECODE_G(clr_a) * (1 - ratio)) + ((DECODE_G(clr_b) * ratio)));
|
||||
unsigned char red =
|
||||
((DECODE_R(clr_a) * (1 - ratio)) + ((DECODE_R(clr_b) * ratio)));
|
||||
return ENCODE_ARGB(DECODE_A(clr_a), red, green, blue);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_mask(const unsigned int clr, const char keep)
|
||||
{
|
||||
switch (keep)
|
||||
{
|
||||
case 'r':
|
||||
return clr & (BYTE_A | BYTE_R);
|
||||
break;
|
||||
case 'g':
|
||||
return clr & (BYTE_A | BYTE_G);
|
||||
break;
|
||||
case 'b':
|
||||
return clr & (BYTE_A | BYTE_B);
|
||||
break;
|
||||
default:
|
||||
return FAILURE;
|
||||
break;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_rotl(const unsigned int clr, const int dist)
|
||||
{
|
||||
if (dist > 8)
|
||||
{
|
||||
LOGERROR(ERR_INVALID_SIZE);
|
||||
return ERR_INVALID_SIZE;
|
||||
}
|
||||
unsigned int red = DECODE_R(clr);
|
||||
unsigned int green = DECODE_G(clr);
|
||||
unsigned int blue = DECODE_B(clr);
|
||||
/*
|
||||
* Rotates the color channels. Essentially it moves bits left a certain
|
||||
* n and then wraps if it leaves the left to the right.
|
||||
*
|
||||
* If you input 1 to n and have:
|
||||
* 0 0 0 0 0 0 0 1 # 1
|
||||
*
|
||||
* You will get:
|
||||
* 0 0 0 0 0 0 1 0 # 2
|
||||
*
|
||||
* But this gets complex with larger numbers:
|
||||
* 0 1 0 1 1 1 0 0 # 4 + 8 + 16 + 64 = 92
|
||||
*
|
||||
* Rotating by 2 will wrap and give:
|
||||
* 0 1 1 1 0 0 0 1 # 1 + 16 + 32 + 64 = 113
|
||||
*
|
||||
* This creates a pixilated looking image of random colors while still
|
||||
* holding the original image's structure under. Almost a distorted,
|
||||
* broken TV look.
|
||||
*
|
||||
* N has a max of 8 because any number after 8 results in the bits
|
||||
* shifting negatively due to the wrapping math, which cant happen and
|
||||
* just makes the pixels black. If the number is 8 it just results in
|
||||
* the pixels being the same as they are just rotated back to their
|
||||
* original spot.
|
||||
* */
|
||||
red = ((red << dist) | (red >> (SHIFT_8 - dist))) & BYTE;
|
||||
green = ((green << dist) | (green >> (SHIFT_8 - dist))) & BYTE;
|
||||
blue = ((blue << dist) | (blue >> (SHIFT_8 - dist))) & BYTE;
|
||||
return ENCODE_RGB(red, green, blue);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_contrast(const unsigned int clr, const float factor)
|
||||
{
|
||||
// NOTE: keep definition for readability
|
||||
int red = CLAMP_MIN(CLAMP_MAX(
|
||||
(int)(factor * (float)(int)(DECODE_R(clr) - HALF_MAX)) + HALF_MAX));
|
||||
int green = CLAMP_MIN(CLAMP_MAX(
|
||||
(int)(factor * (float)(int)(DECODE_G(clr) - HALF_MAX)) + HALF_MAX));
|
||||
int blue = CLAMP_MIN(CLAMP_MAX(
|
||||
(int)(factor * (float)(int)(DECODE_B(clr) - HALF_MAX)) + HALF_MAX));
|
||||
return BYTE_A | (red << SHIFT_16) | (green << SHIFT_8) | blue;
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u32_avg_gb(const unsigned int clr)
|
||||
{
|
||||
unsigned char red = DECODE_R(clr);
|
||||
unsigned char blue = DECODE_B(clr);
|
||||
unsigned char green = (red + blue) / 2;
|
||||
return ENCODE_RGB(red, green, blue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Blurs column at (x,y) using -radius to radius
|
||||
* @details Starts at the top extreme and moves to the bottom extreme,
|
||||
* taking the pixel and adding each channel. Because each channel is separated
|
||||
* into their own int, it adds each int together and at the end divides each
|
||||
* channel back below 256 and converts back to one int.
|
||||
* @param[in] src Pointer to buffer image.
|
||||
* @param[in] x Location in the image buffer for column of original pixel
|
||||
* @param[in] y Location in the image buffer for row of original pixel
|
||||
* @param[in] img Pointer to PPM container
|
||||
* @param[in] radius The value to find -extreme and +extreme to add to pixel
|
||||
* @return unsigned int Packed color with new alpha channel.
|
||||
*/
|
||||
unsigned int
|
||||
blur_col(const unsigned int* src, const int x, const int y, const PPM* img,
|
||||
const int radius)
|
||||
{
|
||||
unsigned long pt = 0, cnt = 0;
|
||||
|
||||
for (int dx = -radius; dx <= radius; dx++)
|
||||
{
|
||||
int px = x + dx;
|
||||
if (px >= 0 && px < img->width)
|
||||
{
|
||||
pt += u32to_u64(src[y * img->width + px]);
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
|
||||
// WHY: takes the count (cnt) of pixels from column and divides the
|
||||
// point (pt) by (cnt) to get back below 256 and then converted back
|
||||
// into an int from a long
|
||||
if (cnt > 0)
|
||||
{
|
||||
return u64to_u32(pt, cnt);
|
||||
}
|
||||
|
||||
return src[y * img->width + x];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Blurs column at (x,y) using -radius to radius
|
||||
* @details Starts at the left extreme and moves to the right extreme taking the
|
||||
* pixel and adding each channel. Because each channel is separated into their
|
||||
* own int, it adds each int together and at the end divides each channel back
|
||||
* below 256 and converts back to one int.
|
||||
* @param[in] src Pointer to buffer image.
|
||||
* @param[in] x Location in the image buffer for column of original pixel
|
||||
* @param[in] y Location in the image buffer for row of original pixel
|
||||
* @param[in] img Pointer to PPM container
|
||||
* @param[in] radius The value to find -extreme and +extreme to add to pixel
|
||||
* @return unsigned int Packed color with new alpha channel.
|
||||
*/
|
||||
unsigned int
|
||||
blur_row(const unsigned int* src, const int x, const int y, const PPM* img,
|
||||
const int radius)
|
||||
{
|
||||
unsigned long pt = 0, cnt = 0;
|
||||
|
||||
for (int dy = -radius; dy <= radius; dy++)
|
||||
{
|
||||
int py = y + dy;
|
||||
if (py >= 0 && py < img->height)
|
||||
{
|
||||
pt += u32to_u64(src[py * img->width + x]);
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
|
||||
// WHY: takes the count (cnt) of pixels from row and divides the
|
||||
// point (pt) by (cnt) to get back below 256 and then converted back
|
||||
// into an int from a long
|
||||
if (cnt > 0)
|
||||
{
|
||||
return u64to_u32(pt, cnt);
|
||||
}
|
||||
return src[y * img->width + x];
|
||||
}
|
||||
|
||||
unsigned int*
|
||||
u32_box_blur(const unsigned int* buf, const PPM* metadata, const int radius)
|
||||
{
|
||||
unsigned int* col_buf = malloc(metadata->len * sizeof(unsigned int));
|
||||
if (!col_buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
unsigned int* dst_buf = malloc(metadata->len * sizeof(unsigned int));
|
||||
if (!dst_buf)
|
||||
{
|
||||
free(col_buf);
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
for (int y = 0; y < metadata->height; y++)
|
||||
{
|
||||
for (int x = 0; x < metadata->width; x++)
|
||||
{
|
||||
col_buf[y * metadata->width + x] =
|
||||
blur_col(buf, x, y, metadata, radius);
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < metadata->height; y++)
|
||||
{
|
||||
for (int x = 0; x < metadata->width; x++)
|
||||
{
|
||||
dst_buf[y * metadata->width + x] =
|
||||
blur_row(col_buf, x, y, metadata, radius);
|
||||
}
|
||||
}
|
||||
free(col_buf);
|
||||
free((void*)buf);
|
||||
return dst_buf;
|
||||
}
|
||||
+328
@@ -0,0 +1,328 @@
|
||||
#include "image/imgio.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <turbojpeg.h>
|
||||
|
||||
#include "image/utils.h"
|
||||
|
||||
unsigned int*
|
||||
read_ppm(const char* filename, PPM* metadata)
|
||||
{
|
||||
LOG("Decoding");
|
||||
unsigned char red, green, blue;
|
||||
char fmt[3];
|
||||
int max_val;
|
||||
FILE* fptr;
|
||||
fptr = fopen(filename, "r");
|
||||
if (!fptr)
|
||||
{
|
||||
LOGERROR(ERR_IMG_NAME_INVALID);
|
||||
LOG(filename);
|
||||
return NULL;
|
||||
}
|
||||
fscanf(fptr, "%s", fmt);
|
||||
metadata->fmt = fmt[1];
|
||||
fscanf(fptr, "%d %d", &metadata->width, &metadata->height);
|
||||
metadata->len = metadata->width * metadata->height;
|
||||
fscanf(fptr, "%d", &max_val);
|
||||
fgetc(fptr);
|
||||
unsigned char* img_buf = malloc(metadata->len * 3);
|
||||
if (!img_buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
unsigned int* clr_buf = malloc(metadata->len * sizeof(unsigned int));
|
||||
if (!clr_buf)
|
||||
{
|
||||
free(img_buf);
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
if (metadata->fmt == '3')
|
||||
{
|
||||
for (int i = 0; i < metadata->len; i++)
|
||||
{
|
||||
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
|
||||
clr_buf[i] = ENCODE_RGB(red, green, blue);
|
||||
}
|
||||
}
|
||||
else if (metadata->fmt == '6')
|
||||
{
|
||||
fread(img_buf, 3, metadata->len, fptr);
|
||||
|
||||
for (int i = 0; i < metadata->len; i++)
|
||||
{
|
||||
red = img_buf[i * 3];
|
||||
green = img_buf[i * 3 + 1];
|
||||
blue = img_buf[i * 3 + 2];
|
||||
clr_buf[i] = ENCODE_RGB(red, green, blue);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fptr);
|
||||
free(img_buf);
|
||||
LOG("Decoding Finished");
|
||||
return clr_buf;
|
||||
}
|
||||
|
||||
int
|
||||
write_ppm(const unsigned int* buf, const PPM metadata, const char* filename)
|
||||
{
|
||||
if (!buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
unsigned char* dst_buf = malloc(metadata.len * 3);
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
LOG("Encoding");
|
||||
FILE* fptr;
|
||||
|
||||
if (metadata.fmt == '3')
|
||||
{
|
||||
fptr = fopen(filename, "w");
|
||||
}
|
||||
|
||||
if (metadata.fmt == '6')
|
||||
{
|
||||
fptr = fopen(filename, "wb");
|
||||
}
|
||||
|
||||
if (!fptr)
|
||||
{
|
||||
free(dst_buf);
|
||||
LOGERROR(ERR_FILE_NULL);
|
||||
return ERR_FILE_NULL;
|
||||
}
|
||||
|
||||
fprintf(fptr, "P%c\n%d %d\n255\n", metadata.fmt, metadata.width,
|
||||
metadata.height);
|
||||
if (metadata.fmt == '3')
|
||||
{
|
||||
for (int i = 0; i < metadata.len; i++)
|
||||
{
|
||||
fprintf(fptr, "%d %d %d ", DECODE_R(buf[i]),
|
||||
DECODE_G(buf[i]), DECODE_B(buf[i]));
|
||||
if (i % 7 == 0)
|
||||
{
|
||||
fprintf(fptr, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata.fmt == '6')
|
||||
{
|
||||
for (int i = 0; i < metadata.len; i++)
|
||||
{
|
||||
dst_buf[i * 3] = DECODE_R(buf[i]);
|
||||
dst_buf[i * 3 + 1] = DECODE_G(buf[i]);
|
||||
dst_buf[i * 3 + 2] = DECODE_B(buf[i]);
|
||||
}
|
||||
fwrite(dst_buf, 1, metadata.len * 3, fptr);
|
||||
}
|
||||
fclose(fptr);
|
||||
free(dst_buf);
|
||||
LOG("Encoding Finished");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned int*
|
||||
read_jpeg(JPEG* metadata)
|
||||
{
|
||||
LOG("Decoding");
|
||||
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
|
||||
unsigned char* jpeg_buf = NULL;
|
||||
FILE* infile = fopen(metadata->name, "rb");
|
||||
if (!infile)
|
||||
{
|
||||
LOGERROR(ERR_FILE_NULL);
|
||||
return NULL;
|
||||
}
|
||||
fseek(infile, 0, SEEK_END);
|
||||
metadata->len = ftell(infile);
|
||||
fseek(infile, 0, SEEK_SET);
|
||||
jpeg_buf = (unsigned char*)malloc(metadata->len);
|
||||
fread(jpeg_buf, 1, metadata->len, infile);
|
||||
fclose(infile);
|
||||
if (tj3DecompressHeader(tjdecompress, jpeg_buf, metadata->len) < 0)
|
||||
{
|
||||
LOGERROR(ERR_JHEADER);
|
||||
LOG(tj3GetErrorStr(tjdecompress));
|
||||
free(jpeg_buf);
|
||||
return NULL;
|
||||
}
|
||||
metadata->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
|
||||
metadata->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
|
||||
metadata->len = metadata->width * metadata->height;
|
||||
unsigned int* pixels =
|
||||
malloc(metadata->width * metadata->height * sizeof(unsigned int));
|
||||
if (!pixels)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
tj3Decompress8(tjdecompress, jpeg_buf, metadata->len,
|
||||
(unsigned char*)pixels, 0, TJPF_BGRA);
|
||||
tj3Destroy(tjdecompress);
|
||||
free(jpeg_buf);
|
||||
LOG("Decoding Finished");
|
||||
return pixels;
|
||||
}
|
||||
|
||||
int
|
||||
write_jpeg(const unsigned int* buf, const JPEG* metadata, const char* filename)
|
||||
{
|
||||
LOG("Encoding");
|
||||
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
|
||||
unsigned char* dst_buf = NULL;
|
||||
size_t dst_size = 0;
|
||||
tj3Set(tjcompress, TJPARAM_QUALITY, 80);
|
||||
tj3Set(tjcompress, TJPARAM_SUBSAMP, TJSAMP_420);
|
||||
if (tj3Compress8(tjcompress, (unsigned char*)buf, metadata->width, 0,
|
||||
metadata->height, TJPF_BGRA, &dst_buf, &dst_size) < 0)
|
||||
{
|
||||
LOGERROR(ERR_JHEADER);
|
||||
LOG(tj3GetErrorStr(tjcompress));
|
||||
return ERR_JHEADER;
|
||||
}
|
||||
FILE* fptr = fopen(filename, "wb");
|
||||
if (!fptr)
|
||||
{
|
||||
tj3Free(dst_buf);
|
||||
LOGERROR(ERR_FILE_NULL);
|
||||
return ERR_FILE_NULL;
|
||||
}
|
||||
if (!dst_buf)
|
||||
{
|
||||
tj3Free(dst_buf);
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
if (dst_size <= 0)
|
||||
{
|
||||
tj3Free(dst_buf);
|
||||
LOGERROR(ERR_INVALID_SIZE);
|
||||
return ERR_INVALID_SIZE;
|
||||
}
|
||||
fwrite(dst_buf, 1, dst_size, fptr);
|
||||
fclose(fptr);
|
||||
|
||||
tj3Destroy(tjcompress);
|
||||
tj3Free(dst_buf);
|
||||
LOG("Encoding Finished");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void
|
||||
transcode_to_jpeg(const char* location)
|
||||
{
|
||||
JPEG dst;
|
||||
PPM src;
|
||||
if (!location)
|
||||
{
|
||||
LOGERROR(ERR_IMG_NAME_INVALID);
|
||||
return;
|
||||
}
|
||||
size_t length = find_extension(location);
|
||||
char stem[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
stem[i] = location[i];
|
||||
}
|
||||
char* ext = ".jpg";
|
||||
char* result = malloc(strlen(stem) + strlen(ext) + 1);
|
||||
if (!result)
|
||||
{
|
||||
LOGERROR(ERR_MALLOC_NULL);
|
||||
return;
|
||||
}
|
||||
strcat(result, stem);
|
||||
strcat(result, ext);
|
||||
unsigned int* dst_buf = read_ppm(location, &src);
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return;
|
||||
}
|
||||
dst = inherit_from_ppm(&src);
|
||||
write_jpeg(dst_buf, &dst, result);
|
||||
free(dst_buf);
|
||||
free(result);
|
||||
}
|
||||
|
||||
void
|
||||
transcode_to_ppm(const char* location)
|
||||
{
|
||||
JPEG src;
|
||||
PPM dst;
|
||||
if (!location)
|
||||
{
|
||||
LOGERROR(ERR_IMG_NAME_INVALID);
|
||||
return;
|
||||
}
|
||||
src.name = (void*)location;
|
||||
size_t length = find_extension(location);
|
||||
char str2[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
str2[i] = location[i];
|
||||
}
|
||||
char* str3 = ".ppm";
|
||||
char* result = malloc(strlen(str2) + strlen(str3) + 1);
|
||||
if (!result)
|
||||
{
|
||||
LOGERROR(ERR_MALLOC_NULL);
|
||||
return;
|
||||
}
|
||||
strcat(result, str2);
|
||||
strcat(result, str3);
|
||||
unsigned int* dst_buf = read_jpeg(&src);
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
dst = inherit_from_jpeg(&src, '6');
|
||||
write_ppm(dst_buf, dst, result);
|
||||
free(result);
|
||||
free(dst_buf);
|
||||
}
|
||||
|
||||
PPM
|
||||
inherit_from_jpeg(const JPEG* metadata, const char fmt)
|
||||
{
|
||||
PPM image;
|
||||
if (!metadata)
|
||||
{
|
||||
LOGERROR(ERR_IMG_NULL);
|
||||
return image;
|
||||
}
|
||||
image.width = metadata->width;
|
||||
image.height = metadata->height;
|
||||
image.len = metadata->len;
|
||||
image.fmt = fmt;
|
||||
return image;
|
||||
}
|
||||
|
||||
JPEG
|
||||
inherit_from_ppm(const PPM* metadata)
|
||||
{
|
||||
JPEG image;
|
||||
if (!metadata)
|
||||
{
|
||||
LOGERROR(ERR_IMG_NULL);
|
||||
return image;
|
||||
}
|
||||
image.width = metadata->width;
|
||||
image.height = metadata->height;
|
||||
image.len = metadata->len;
|
||||
return image;
|
||||
}
|
||||
+12
-13
@@ -1,26 +1,25 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "image/filters.h"
|
||||
#include "image/imgio.h"
|
||||
#include "image/transformations.h"
|
||||
#include "image/utils.h"
|
||||
// #include "image/filters.h"
|
||||
// #include "image/transformations.h"
|
||||
#include "image/imgio.h"
|
||||
|
||||
/*
|
||||
* Keep in mind any function that creates a new buffer and returns it can
|
||||
* overwrite the old one, however it is lost. SO create a new variable and free
|
||||
* the old one after use.
|
||||
*/
|
||||
int
|
||||
main()
|
||||
{
|
||||
JPEG img;
|
||||
PPM out;
|
||||
img.name = "../assets/test.jpeg";
|
||||
char img[] = "../assets/IMG_1649_new.ppm";
|
||||
transcode_to_jpeg(img);
|
||||
|
||||
unsigned int* buf = jpeg_load(&img);
|
||||
for (int i = 0; i < img.len; i++)
|
||||
{
|
||||
buf[i] = u32_grayscale(buf[i]);
|
||||
}
|
||||
out = inherit_from_jpeg(&img, '3');
|
||||
out.name = "../assets/test.ppm";
|
||||
ppm_save(buf, out, out.name);
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "image/transformations.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "image/utils.h"
|
||||
|
||||
unsigned int*
|
||||
u32_flop(const unsigned int* buf, const int width, const int height)
|
||||
{
|
||||
int len, idx;
|
||||
if (!buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
len = width * height;
|
||||
unsigned int* flip = malloc(len * sizeof(unsigned int));
|
||||
if (!flip)
|
||||
{
|
||||
LOGERROR(ERR_CREATION_FAIL);
|
||||
return NULL;
|
||||
}
|
||||
idx = 0;
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int k = width - 1; k >= 0; k--)
|
||||
{
|
||||
flip[idx] = buf[(i * width) + k];
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
free((void*)buf);
|
||||
|
||||
return flip;
|
||||
}
|
||||
|
||||
unsigned int*
|
||||
u32_flip(const unsigned int* buf, const int width, const int height)
|
||||
{
|
||||
int len, idx;
|
||||
if (!buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
len = width * height;
|
||||
unsigned int* flip = malloc(len * sizeof(unsigned int));
|
||||
if (!flip)
|
||||
{
|
||||
LOGERROR(ERR_FILE_NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
idx = 0;
|
||||
for (int i = height - 1; i >= 0; i--)
|
||||
{
|
||||
for (int k = 0; k < width; k++)
|
||||
{
|
||||
flip[idx] = buf[(i * width) + k];
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
free((void*)buf);
|
||||
|
||||
return flip;
|
||||
}
|
||||
|
||||
unsigned int*
|
||||
u32_rotate(const unsigned int* buf, PPM* metadata)
|
||||
{
|
||||
if (!buf)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
if (!metadata)
|
||||
{
|
||||
LOGERROR(ERR_IMG_NULL);
|
||||
return NULL;
|
||||
}
|
||||
unsigned int* mask = malloc(metadata->len * sizeof(unsigned int));
|
||||
if (!mask)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return NULL;
|
||||
}
|
||||
int old_width = metadata->width;
|
||||
int old_height = metadata->height;
|
||||
for (int row = 0; row < old_height; row++)
|
||||
{
|
||||
for (int col = 0; col < old_width; col++)
|
||||
{
|
||||
int new_height = col;
|
||||
int new_width = (old_height - 1 - row);
|
||||
|
||||
mask[(new_height * old_height) + new_width] =
|
||||
buf[(row * old_width) + col];
|
||||
}
|
||||
}
|
||||
metadata->width = old_height;
|
||||
metadata->height = old_width;
|
||||
free((void*)buf);
|
||||
return mask;
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
#include "image/utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* error_messages[] = {
|
||||
[ERR_BUF_NULL] = "Buffer NULL",
|
||||
[ERR_FILE_NULL] = "File NULL",
|
||||
[ERR_CREATION_FAIL] = "Buffer creation failed",
|
||||
[ERR_INVALID_SIZE] = "Given size is invalid",
|
||||
[ERR_IMG_NULL] = "Image NULL",
|
||||
[ERR_IMG_NAME_INVALID] = "Invalid File Name",
|
||||
[ERR_JHEADER] = "Header Error:",
|
||||
[ERR_MALLOC_NULL] = "Malloc Failed to allocate",
|
||||
};
|
||||
|
||||
unsigned long
|
||||
u32to_u64(const unsigned int clr)
|
||||
{
|
||||
unsigned long mask = 0;
|
||||
unsigned long alpha = DECODE_A(clr);
|
||||
unsigned long red = DECODE_R(clr);
|
||||
unsigned long green = DECODE_G(clr);
|
||||
unsigned long blue = DECODE_B(clr);
|
||||
return (alpha << 48) | (red << 32) | (green << 16) | (blue);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
u64to_u32(const unsigned long clr, const unsigned long u64_div)
|
||||
{
|
||||
unsigned int a = ((clr >> 48) & LONG_BYTE);
|
||||
unsigned int r = ((clr >> 32) & LONG_BYTE);
|
||||
unsigned int g = ((clr >> 16) & LONG_BYTE);
|
||||
unsigned int b = (clr & LONG_BYTE);
|
||||
return ENCODE_ARGB((a / u64_div), (r / u64_div), (g / u64_div),
|
||||
(b / u64_div));
|
||||
}
|
||||
|
||||
void __attribute__((overloadable))
|
||||
print_attr(const PPM* metadata)
|
||||
{
|
||||
printf("Name: %s\nFMT: %c\nWidth: %i, Height: %i\nLength: %i\n",
|
||||
metadata->name, metadata->fmt, metadata->width, metadata->height,
|
||||
metadata->len);
|
||||
}
|
||||
|
||||
void __attribute__((overloadable))
|
||||
print_attr(const JPEG* metadata)
|
||||
{
|
||||
printf("Name: %s\nWidth: %i, Height: %i\nLength: %i\n", metadata->name,
|
||||
metadata->width, metadata->height, metadata->len);
|
||||
}
|
||||
|
||||
size_t
|
||||
find_extension(const char* str)
|
||||
{
|
||||
size_t length = strlen(str);
|
||||
for (int i = length; i >= 0; i--)
|
||||
{
|
||||
if (str[i] == '.')
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
Reference in new issue
Block a user