REFACTOR: Expanded many parameter names for functions.
DOCS: Corrected comments to match new parameters. CHORE: Moved some variable definition to top of function call.
This commit is contained in:
@@ -107,11 +107,11 @@ u32_mask(const unsigned int clr, const char keep);
|
|||||||
* it is just rotated back to original position. Cannot be greater than 8
|
* it is just rotated back to original position. Cannot be greater than 8
|
||||||
* because the calculations would result in a negative.
|
* because the calculations would result in a negative.
|
||||||
* @param[in] clr Packed Pixel
|
* @param[in] clr Packed Pixel
|
||||||
* @param[in] n Amount of times to move bits to left.
|
* @param[in] dist Distance from original position to move each bit to the left.
|
||||||
* @return unsigned int Packed Pixel with newly moved bits
|
* @return unsigned int Packed Pixel with newly moved bits
|
||||||
*/
|
*/
|
||||||
unsigned int
|
unsigned int
|
||||||
u32_rotl(const unsigned int clr, const int n);
|
u32_rotl(const unsigned int clr, const int dist);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Raises Color Vibrance
|
* @brief Raises Color Vibrance
|
||||||
@@ -139,13 +139,13 @@ u32_avg_gb(const unsigned int clr);
|
|||||||
* them. Then it moves to the next pixel doing the same. Then it does the second
|
* 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
|
* loop from top to bottom doing the same principle. This can take some time
|
||||||
* depending on hardware, image size, radius, etc.
|
* depending on hardware, image size, radius, etc.
|
||||||
* @param[in] src_buf The image buffer to be blurred
|
* @param[in] buf The image buffer to be blurred
|
||||||
* @param[in] img the PPM image container
|
* @param[in] metadata the PPM image container
|
||||||
* @param[in] radius The amount of pixels around the left and right, then top
|
* @param[in] radius The amount of pixels around the left and right, then top
|
||||||
* and bottom to mix with the currently selected pixel
|
* and bottom to mix with the currently selected pixel
|
||||||
* @return unsigned int Pointer to new created image buffer.
|
* @return unsigned int Pointer to new created image buffer.
|
||||||
*/
|
*/
|
||||||
unsigned int*
|
unsigned int*
|
||||||
u32_box_blur(const unsigned int* src_buf, const PPM* img, const int radius);
|
u32_box_blur(const unsigned int* buf, const PPM* metadata, const int radius);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+20
-21
@@ -5,67 +5,66 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Open and Fill buffer from .ppm file
|
* @brief Open and Fill buffer from .ppm file
|
||||||
* @param [in] file Char pointer use to locate the image to be modified lays.
|
* @param [in] filename Location of the image to be modified
|
||||||
* @param [in,out] m_attr PPM pointer to store image information
|
* @param [in,out] metadata PPM pointer to store image information
|
||||||
*/
|
*/
|
||||||
unsigned int*
|
unsigned int*
|
||||||
read_ppm(const char* file, PPM* m_attr);
|
read_ppm(const char* filename, PPM* metadata);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Open and write buffer to file
|
* @brief Open and write buffer to file
|
||||||
* @param [in] buf Information of pixels
|
* @param [in] buffer Information of pixels
|
||||||
* @param [in] img PPM pointer to store image information
|
* @param [in] metadata PPM pointer to store image information
|
||||||
* @param [in] char Name/Location for file
|
* @param [in] filename Name/Location for file
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
write_ppm(const unsigned int* buf, const PPM img, const char* name);
|
write_ppm(const unsigned int* buffer, const PPM metadata, const char* filename);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Open and Fill buffer from .jpg file
|
* @brief Open and Fill buffer from .jpg file
|
||||||
* @details Opens a .jpg file, creates a new buffer and extracts the pixels into
|
* @details Opens a .jpg file, creates a new buffer and extracts the pixels into
|
||||||
* said buffer as integers.
|
* said buffer as integers.
|
||||||
* @param [in] file Char pointer use to locate the image to be modified lays.
|
* @param [in,out] metadata JPEG pointer to store image information
|
||||||
* @param [in,out] m_attr JPEG pointer to store image information
|
|
||||||
*/
|
*/
|
||||||
unsigned int*
|
unsigned int*
|
||||||
read_jpeg(JPEG* m_attr);
|
read_jpeg(JPEG* metadata);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Open and write buffer to file
|
* @brief Open and write buffer to file
|
||||||
* @param [in] buf Information of pixels
|
* @param [in] buf Information of pixels
|
||||||
* @param [in] attr JPEG pointer to store image information
|
* @param [in] metadata JPEG pointer to store image information
|
||||||
* @param [in] char Name/Location for file
|
* @param [in] filename Name/Location for file
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
write_jpeg(const unsigned int* src_buf, const JPEG* attr, const char* name);
|
write_jpeg(const unsigned int* buf, const JPEG* metadata, const char* filename);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts PPM to JPEG
|
* @brief Converts PPM to JPEG
|
||||||
* @param [in] in Pointer to PPM container
|
* @param [in] location points to file to use
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
transcode_to_jpeg(PPM* in);
|
transcode_to_jpeg(const char* location);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts JPEG to PPM
|
* @brief Converts JPEG to PPM
|
||||||
* @param [in] in Pointer to JPEG container
|
* @param [in] location Pointer to JPEG container
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
transcode_to_ppm(JPEG* in);
|
transcode_to_ppm(const char* location);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Inherits the attributes from JPEG container
|
* @brief Inherits the attributes from JPEG container
|
||||||
* @param [in] in Pointer to JPEG container
|
* @param [in] metadata Pointer to JPEG container
|
||||||
* @param [in] fmt Character for type of PPM image, binary or raw
|
* @param [in] fmt Character for type of PPM image, binary or raw
|
||||||
*/
|
*/
|
||||||
PPM
|
PPM
|
||||||
inherit_from_jpeg(const JPEG* in, const char fmt);
|
inherit_from_jpeg(const JPEG* metadata, const char fmt);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Inherits the attributes from PPM container
|
* @brief Inherits the attributes from PPM container
|
||||||
* @param [in] in Pointer to PPM container
|
* @param [in] metadata Pointer to PPM container
|
||||||
*/
|
*/
|
||||||
JPEG
|
JPEG
|
||||||
inherit_from_ppm(const PPM* in);
|
inherit_from_ppm(const PPM* metadata);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -38,6 +38,6 @@ u32_flip(const unsigned int* buf, const int width, const int height);
|
|||||||
* new buffer fails to create and is invalid
|
* new buffer fails to create and is invalid
|
||||||
* */
|
* */
|
||||||
unsigned int*
|
unsigned int*
|
||||||
u32_rotate(const unsigned int* buf, PPM* m_img);
|
u32_rotate(const unsigned int* buf, PPM* metadata);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -158,27 +158,27 @@ u64to_u32(const unsigned long clr, const unsigned long u64_div);
|
|||||||
* @brief Prints quick debug info of container
|
* @brief Prints quick debug info of container
|
||||||
* @details Prints metadata like Name, Format, Width, Height and Length
|
* @details Prints metadata like Name, Format, Width, Height and Length
|
||||||
* important for debugging errors
|
* important for debugging errors
|
||||||
* @param[in] src Pointer to src metadata of a PPM image
|
* @param[in] metadata Pointer to information of a PPM image
|
||||||
*/
|
*/
|
||||||
void __attribute__((overloadable))
|
void __attribute__((overloadable))
|
||||||
print_attr(const PPM* src);
|
print_attr(const PPM* metadata);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Prints quick debug info of container
|
* @brief Prints quick debug info of container
|
||||||
* @details Prints metadata like Name, Width, Height and Length
|
* @details Prints metadata like Name, Width, Height and Length
|
||||||
* important for debugging errors
|
* important for debugging errors
|
||||||
* @param[in] src Pointer to src metadata of a JPEG image
|
* @param[in] metadata Pointer to information of a JPEG image
|
||||||
*/
|
*/
|
||||||
void __attribute__((overloadable))
|
void __attribute__((overloadable))
|
||||||
print_attr(const JPEG* src);
|
print_attr(const JPEG* metadata);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Finds start of extension in image name
|
* @brief Finds start of extension in image name
|
||||||
* @details Loops through the character array starting from the end to find the
|
* @details Loops through the character array starting from the end to find the
|
||||||
* start of the extension.
|
* start of the extension.
|
||||||
* @param[in] n Char array (str)
|
* @param[in] str Char array to loop through
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
find_extension(const char* n);
|
find_extension(const char* str);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+27
-25
@@ -27,9 +27,6 @@ u32_swap_rb(const unsigned int clr)
|
|||||||
unsigned int
|
unsigned int
|
||||||
u32_grayscale(const unsigned int clr)
|
u32_grayscale(const unsigned int clr)
|
||||||
{
|
{
|
||||||
unsigned int red = DECODE_R(clr);
|
|
||||||
unsigned int green = DECODE_G(clr);
|
|
||||||
unsigned int blue = DECODE_B(clr);
|
|
||||||
/*
|
/*
|
||||||
* WHY: The colors use the REC 601 Standard: In layman's terms they are
|
* 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
|
* weighed in how the human eye perceives color. So green is the largest
|
||||||
@@ -37,8 +34,9 @@ u32_grayscale(const unsigned int clr)
|
|||||||
* The Shift_8 is to divide 256 to bring the color value back into range
|
* The Shift_8 is to divide 256 to bring the color value back into range
|
||||||
of 0 - 255
|
of 0 - 255
|
||||||
*/
|
*/
|
||||||
unsigned int gray =
|
unsigned int gray = ((DECODE_R(clr) * 77) + (DECODE_G(clr) * 150) +
|
||||||
((red * 77) + (green * 150) + (blue * 29)) >> SHIFT_8;
|
(DECODE_B(clr) * 29)) >>
|
||||||
|
SHIFT_8;
|
||||||
|
|
||||||
return (clr & BYTE_A) | (gray << SHIFT_16) | (gray << SHIFT_8) | gray;
|
return (clr & BYTE_A) | (gray << SHIFT_16) | (gray << SHIFT_8) | gray;
|
||||||
}
|
}
|
||||||
@@ -56,6 +54,7 @@ u32_step(const unsigned int clr, const unsigned char max)
|
|||||||
unsigned int
|
unsigned int
|
||||||
u32_brighten(const unsigned int clr, const float factor)
|
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 red = CLAMP_MAX(DECODE_R(clr) * factor);
|
||||||
unsigned int green = CLAMP_MAX(DECODE_G(clr) * factor);
|
unsigned int green = CLAMP_MAX(DECODE_G(clr) * factor);
|
||||||
unsigned int blue = CLAMP_MAX(DECODE_B(clr) * factor);
|
unsigned int blue = CLAMP_MAX(DECODE_B(clr) * factor);
|
||||||
@@ -65,6 +64,7 @@ u32_brighten(const unsigned int clr, const float factor)
|
|||||||
unsigned int
|
unsigned int
|
||||||
u32_mix(const unsigned int clr_a, const unsigned int clr_b)
|
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 blue = (DECODE_B(clr_a) + DECODE_B(clr_b)) / 2;
|
||||||
unsigned char green = (DECODE_G(clr_a) + DECODE_G(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;
|
unsigned char red = (DECODE_R(clr_a) + DECODE_R(clr_b)) / 2;
|
||||||
@@ -81,6 +81,7 @@ u32_lerp(const unsigned int clr_a, const unsigned int clr_b, const float ratio)
|
|||||||
* because they are whole number pixels. So JPEG are better because the
|
* because they are whole number pixels. So JPEG are better because the
|
||||||
* numbers result in floating point colors.
|
* numbers result in floating point colors.
|
||||||
* */
|
* */
|
||||||
|
// NOTE: keep definition for readability
|
||||||
unsigned char blue =
|
unsigned char blue =
|
||||||
((DECODE_B(clr_a) * (1 - ratio)) + ((DECODE_B(clr_b) * ratio)));
|
((DECODE_B(clr_a) * (1 - ratio)) + ((DECODE_B(clr_b) * ratio)));
|
||||||
unsigned char green =
|
unsigned char green =
|
||||||
@@ -112,9 +113,9 @@ u32_mask(const unsigned int clr, const char keep)
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
u32_rotl(const unsigned int clr, const int n)
|
u32_rotl(const unsigned int clr, const int dist)
|
||||||
{
|
{
|
||||||
if (n > 8)
|
if (dist > 8)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_INVALID_SIZE);
|
LOGERROR(ERR_INVALID_SIZE);
|
||||||
return ERR_INVALID_SIZE;
|
return ERR_INVALID_SIZE;
|
||||||
@@ -148,15 +149,16 @@ u32_rotl(const unsigned int clr, const int n)
|
|||||||
* the pixels being the same as they are just rotated back to their
|
* the pixels being the same as they are just rotated back to their
|
||||||
* original spot.
|
* original spot.
|
||||||
* */
|
* */
|
||||||
red = ((red << n) | (red >> (SHIFT_8 - n))) & BYTE;
|
red = ((red << dist) | (red >> (SHIFT_8 - dist))) & BYTE;
|
||||||
green = ((green << n) | (green >> (SHIFT_8 - n))) & BYTE;
|
green = ((green << dist) | (green >> (SHIFT_8 - dist))) & BYTE;
|
||||||
blue = ((blue << n) | (blue >> (SHIFT_8 - n))) & BYTE;
|
blue = ((blue << dist) | (blue >> (SHIFT_8 - dist))) & BYTE;
|
||||||
return ENCODE_RGB(red, green, blue);
|
return ENCODE_RGB(red, green, blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
u32_contrast(const unsigned int clr, const float factor)
|
u32_contrast(const unsigned int clr, const float factor)
|
||||||
{
|
{
|
||||||
|
// NOTE: keep definition for readability
|
||||||
int red = CLAMP_MIN(CLAMP_MAX(
|
int red = CLAMP_MIN(CLAMP_MAX(
|
||||||
(int)(factor * (float)(int)(DECODE_R(clr) - HALF_MAX)) + HALF_MAX));
|
(int)(factor * (float)(int)(DECODE_R(clr) - HALF_MAX)) + HALF_MAX));
|
||||||
int green = CLAMP_MIN(CLAMP_MAX(
|
int green = CLAMP_MIN(CLAMP_MAX(
|
||||||
@@ -255,38 +257,38 @@ blur_row(const unsigned int* src, const int x, const int y, const PPM* img,
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int*
|
unsigned int*
|
||||||
u32_box_blur(const unsigned int* src_buf, const PPM* img, const int radius)
|
u32_box_blur(const unsigned int* buf, const PPM* metadata, const int radius)
|
||||||
{
|
{
|
||||||
unsigned int* first_pass = malloc(img->len * sizeof(unsigned int));
|
unsigned int* col_buf = malloc(metadata->len * sizeof(unsigned int));
|
||||||
if (!first_pass)
|
if (!col_buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
unsigned int* dst_buf = malloc(img->len * sizeof(unsigned int));
|
unsigned int* dst_buf = malloc(metadata->len * sizeof(unsigned int));
|
||||||
if (!dst_buf)
|
if (!dst_buf)
|
||||||
{
|
{
|
||||||
free(first_pass);
|
free(col_buf);
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
for (int y = 0; y < img->height; y++)
|
for (int y = 0; y < metadata->height; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < img->width; x++)
|
for (int x = 0; x < metadata->width; x++)
|
||||||
{
|
{
|
||||||
first_pass[y * img->width + x] =
|
col_buf[y * metadata->width + x] =
|
||||||
blur_col(src_buf, x, y, img, radius);
|
blur_col(buf, x, y, metadata, radius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int y = 0; y < img->height; y++)
|
for (int y = 0; y < metadata->height; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < img->width; x++)
|
for (int x = 0; x < metadata->width; x++)
|
||||||
{
|
{
|
||||||
dst_buf[y * img->width + x] =
|
dst_buf[y * metadata->width + x] =
|
||||||
blur_row(first_pass, x, y, img, radius);
|
blur_row(col_buf, x, y, metadata, radius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(first_pass);
|
free(col_buf);
|
||||||
free((void*)src_buf);
|
free((void*)buf);
|
||||||
return dst_buf;
|
return dst_buf;
|
||||||
}
|
}
|
||||||
|
|||||||
+98
-104
@@ -8,52 +8,52 @@
|
|||||||
#include "image/utils.h"
|
#include "image/utils.h"
|
||||||
|
|
||||||
unsigned int*
|
unsigned int*
|
||||||
read_ppm(const char* file, PPM* m_attr)
|
read_ppm(const char* filename, PPM* metadata)
|
||||||
{
|
{
|
||||||
LOG("Decoding");
|
LOG("Decoding");
|
||||||
unsigned char red, green, blue;
|
unsigned char red, green, blue;
|
||||||
char fmt[3];
|
char fmt[3];
|
||||||
int max_val;
|
int max_val;
|
||||||
FILE* fptr;
|
FILE* fptr;
|
||||||
fptr = fopen(file, "r");
|
fptr = fopen(filename, "r");
|
||||||
if (!fptr)
|
if (!fptr)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_IMG_NAME_INVALID);
|
LOGERROR(ERR_IMG_NAME_INVALID);
|
||||||
LOG(file);
|
LOG(filename);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fscanf(fptr, "%s", fmt);
|
fscanf(fptr, "%s", fmt);
|
||||||
m_attr->fmt = fmt[1];
|
metadata->fmt = fmt[1];
|
||||||
fscanf(fptr, "%d %d", &m_attr->width, &m_attr->height);
|
fscanf(fptr, "%d %d", &metadata->width, &metadata->height);
|
||||||
m_attr->len = m_attr->width * m_attr->height;
|
metadata->len = metadata->width * metadata->height;
|
||||||
fscanf(fptr, "%d", &max_val);
|
fscanf(fptr, "%d", &max_val);
|
||||||
fgetc(fptr);
|
fgetc(fptr);
|
||||||
unsigned char* img_buf = malloc(m_attr->len * 3);
|
unsigned char* img_buf = malloc(metadata->len * 3);
|
||||||
if (!img_buf)
|
if (!img_buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
unsigned int* clr_buf = malloc(m_attr->len * sizeof(unsigned int));
|
unsigned int* clr_buf = malloc(metadata->len * sizeof(unsigned int));
|
||||||
if (!clr_buf)
|
if (!clr_buf)
|
||||||
{
|
{
|
||||||
free(img_buf);
|
free(img_buf);
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (m_attr->fmt == '3')
|
if (metadata->fmt == '3')
|
||||||
{
|
{
|
||||||
for (int i = 0; i < m_attr->len; i++)
|
for (int i = 0; i < metadata->len; i++)
|
||||||
{
|
{
|
||||||
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
|
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
|
||||||
clr_buf[i] = ENCODE_RGB(red, green, blue);
|
clr_buf[i] = ENCODE_RGB(red, green, blue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (m_attr->fmt == '6')
|
else if (metadata->fmt == '6')
|
||||||
{
|
{
|
||||||
fread(img_buf, 3, m_attr->len, fptr);
|
fread(img_buf, 3, metadata->len, fptr);
|
||||||
|
|
||||||
for (int i = 0; i < m_attr->len; i++)
|
for (int i = 0; i < metadata->len; i++)
|
||||||
{
|
{
|
||||||
red = img_buf[i * 3];
|
red = img_buf[i * 3];
|
||||||
green = img_buf[i * 3 + 1];
|
green = img_buf[i * 3 + 1];
|
||||||
@@ -69,14 +69,14 @@ read_ppm(const char* file, PPM* m_attr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
write_ppm(const unsigned int* buf, const PPM img, const char* name)
|
write_ppm(const unsigned int* buf, const PPM metadata, const char* filename)
|
||||||
{
|
{
|
||||||
if (!buf)
|
if (!buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return ERR_BUF_NULL;
|
return ERR_BUF_NULL;
|
||||||
}
|
}
|
||||||
unsigned char* dst_buf = malloc(img.len * 3);
|
unsigned char* dst_buf = malloc(metadata.len * 3);
|
||||||
if (!dst_buf)
|
if (!dst_buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
@@ -85,14 +85,14 @@ write_ppm(const unsigned int* buf, const PPM img, const char* name)
|
|||||||
LOG("Encoding");
|
LOG("Encoding");
|
||||||
FILE* fptr;
|
FILE* fptr;
|
||||||
|
|
||||||
if (img.fmt == '3')
|
if (metadata.fmt == '3')
|
||||||
{
|
{
|
||||||
fptr = fopen(name, "w");
|
fptr = fopen(filename, "w");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (img.fmt == '6')
|
if (metadata.fmt == '6')
|
||||||
{
|
{
|
||||||
fptr = fopen(name, "wb");
|
fptr = fopen(filename, "wb");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fptr)
|
if (!fptr)
|
||||||
@@ -102,10 +102,11 @@ write_ppm(const unsigned int* buf, const PPM img, const char* name)
|
|||||||
return ERR_FILE_NULL;
|
return ERR_FILE_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fptr, "P%c\n%d %d\n255\n", img.fmt, img.width, img.height);
|
fprintf(fptr, "P%c\n%d %d\n255\n", metadata.fmt, metadata.width,
|
||||||
if (img.fmt == '3')
|
metadata.height);
|
||||||
|
if (metadata.fmt == '3')
|
||||||
{
|
{
|
||||||
for (int i = 0; i < img.len; i++)
|
for (int i = 0; i < metadata.len; i++)
|
||||||
{
|
{
|
||||||
fprintf(fptr, "%d %d %d ", DECODE_R(buf[i]),
|
fprintf(fptr, "%d %d %d ", DECODE_R(buf[i]),
|
||||||
DECODE_G(buf[i]), DECODE_B(buf[i]));
|
DECODE_G(buf[i]), DECODE_B(buf[i]));
|
||||||
@@ -116,15 +117,15 @@ write_ppm(const unsigned int* buf, const PPM img, const char* name)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (img.fmt == '6')
|
if (metadata.fmt == '6')
|
||||||
{
|
{
|
||||||
for (int i = 0; i < img.len; i++)
|
for (int i = 0; i < metadata.len; i++)
|
||||||
{
|
{
|
||||||
dst_buf[i * 3] = DECODE_R(buf[i]);
|
dst_buf[i * 3] = DECODE_R(buf[i]);
|
||||||
dst_buf[i * 3 + 1] = DECODE_G(buf[i]);
|
dst_buf[i * 3 + 1] = DECODE_G(buf[i]);
|
||||||
dst_buf[i * 3 + 2] = DECODE_B(buf[i]);
|
dst_buf[i * 3 + 2] = DECODE_B(buf[i]);
|
||||||
}
|
}
|
||||||
fwrite(dst_buf, 1, img.len * 3, fptr);
|
fwrite(dst_buf, 1, metadata.len * 3, fptr);
|
||||||
}
|
}
|
||||||
fclose(fptr);
|
fclose(fptr);
|
||||||
free(dst_buf);
|
free(dst_buf);
|
||||||
@@ -133,41 +134,41 @@ write_ppm(const unsigned int* buf, const PPM img, const char* name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int*
|
unsigned int*
|
||||||
read_jpeg(JPEG* m_attr)
|
read_jpeg(JPEG* metadata)
|
||||||
{
|
{
|
||||||
LOG("Decoding");
|
LOG("Decoding");
|
||||||
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
|
tjhandle tjdecompress = tj3Init(TJINIT_DECOMPRESS);
|
||||||
unsigned char* jpeg_buf = NULL;
|
unsigned char* jpeg_buf = NULL;
|
||||||
FILE* infile = fopen(m_attr->name, "rb");
|
FILE* infile = fopen(metadata->name, "rb");
|
||||||
if (!infile)
|
if (!infile)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_FILE_NULL);
|
LOGERROR(ERR_FILE_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fseek(infile, 0, SEEK_END);
|
fseek(infile, 0, SEEK_END);
|
||||||
m_attr->len = ftell(infile);
|
metadata->len = ftell(infile);
|
||||||
fseek(infile, 0, SEEK_SET);
|
fseek(infile, 0, SEEK_SET);
|
||||||
jpeg_buf = (unsigned char*)malloc(m_attr->len);
|
jpeg_buf = (unsigned char*)malloc(metadata->len);
|
||||||
fread(jpeg_buf, 1, m_attr->len, infile);
|
fread(jpeg_buf, 1, metadata->len, infile);
|
||||||
fclose(infile);
|
fclose(infile);
|
||||||
if (tj3DecompressHeader(tjdecompress, jpeg_buf, m_attr->len) < 0)
|
if (tj3DecompressHeader(tjdecompress, jpeg_buf, metadata->len) < 0)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_JHEADER);
|
LOGERROR(ERR_JHEADER);
|
||||||
LOG(tj3GetErrorStr(tjdecompress));
|
LOG(tj3GetErrorStr(tjdecompress));
|
||||||
free(jpeg_buf);
|
free(jpeg_buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
m_attr->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
|
metadata->width = tj3Get(tjdecompress, TJPARAM_JPEGWIDTH);
|
||||||
m_attr->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
|
metadata->height = tj3Get(tjdecompress, TJPARAM_JPEGHEIGHT);
|
||||||
m_attr->len = m_attr->width * m_attr->height;
|
metadata->len = metadata->width * metadata->height;
|
||||||
unsigned int* pixels =
|
unsigned int* pixels =
|
||||||
malloc(m_attr->width * m_attr->height * sizeof(unsigned int));
|
malloc(metadata->width * metadata->height * sizeof(unsigned int));
|
||||||
if (!pixels)
|
if (!pixels)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
tj3Decompress8(tjdecompress, jpeg_buf, m_attr->len,
|
tj3Decompress8(tjdecompress, jpeg_buf, metadata->len,
|
||||||
(unsigned char*)pixels, 0, TJPF_BGRA);
|
(unsigned char*)pixels, 0, TJPF_BGRA);
|
||||||
tj3Destroy(tjdecompress);
|
tj3Destroy(tjdecompress);
|
||||||
free(jpeg_buf);
|
free(jpeg_buf);
|
||||||
@@ -176,7 +177,7 @@ read_jpeg(JPEG* m_attr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
write_jpeg(const unsigned int* src_buf, const JPEG* attr, const char* name)
|
write_jpeg(const unsigned int* buf, const JPEG* metadata, const char* filename)
|
||||||
{
|
{
|
||||||
LOG("Encoding");
|
LOG("Encoding");
|
||||||
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
|
tjhandle tjcompress = tj3Init(TJINIT_COMPRESS);
|
||||||
@@ -184,21 +185,21 @@ write_jpeg(const unsigned int* src_buf, const JPEG* attr, const char* name)
|
|||||||
size_t dst_size = 0;
|
size_t dst_size = 0;
|
||||||
tj3Set(tjcompress, TJPARAM_QUALITY, 80);
|
tj3Set(tjcompress, TJPARAM_QUALITY, 80);
|
||||||
tj3Set(tjcompress, TJPARAM_SUBSAMP, TJSAMP_420);
|
tj3Set(tjcompress, TJPARAM_SUBSAMP, TJSAMP_420);
|
||||||
if (tj3Compress8(tjcompress, (unsigned char*)src_buf, attr->width, 0,
|
if (tj3Compress8(tjcompress, (unsigned char*)buf, metadata->width, 0,
|
||||||
attr->height, TJPF_BGRA, &dst_buf, &dst_size) < 0)
|
metadata->height, TJPF_BGRA, &dst_buf, &dst_size) < 0)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_JHEADER);
|
LOGERROR(ERR_JHEADER);
|
||||||
LOG(tj3GetErrorStr(tjcompress));
|
LOG(tj3GetErrorStr(tjcompress));
|
||||||
return ERR_JHEADER;
|
return ERR_JHEADER;
|
||||||
}
|
}
|
||||||
FILE* fptr = fopen(name, "wb");
|
FILE* fptr = fopen(filename, "wb");
|
||||||
if (!fptr)
|
if (!fptr)
|
||||||
{
|
{
|
||||||
tj3Free(dst_buf);
|
tj3Free(dst_buf);
|
||||||
LOGERROR(ERR_FILE_NULL);
|
LOGERROR(ERR_FILE_NULL);
|
||||||
return ERR_FILE_NULL;
|
return ERR_FILE_NULL;
|
||||||
}
|
}
|
||||||
if (dst_buf == NULL)
|
if (!dst_buf)
|
||||||
{
|
{
|
||||||
tj3Free(dst_buf);
|
tj3Free(dst_buf);
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
@@ -220,115 +221,108 @@ write_jpeg(const unsigned int* src_buf, const JPEG* attr, const char* name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
transcode_to_jpeg(PPM* in)
|
transcode_to_jpeg(const char* location)
|
||||||
{
|
{
|
||||||
size_t length = find_extension(in->name);
|
JPEG dst;
|
||||||
char str2[length];
|
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++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
str2[i] = in->name[i];
|
stem[i] = location[i];
|
||||||
}
|
}
|
||||||
char* str3 = ".jpg";
|
char* ext = ".jpg";
|
||||||
char* result = malloc(strlen(str2) + strlen(str3) + 1);
|
char* result = malloc(strlen(stem) + strlen(ext) + 1);
|
||||||
if (result != NULL)
|
if (!result)
|
||||||
{
|
|
||||||
strcat(result, str2);
|
|
||||||
strcat(result, str3);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_MALLOC_NULL);
|
LOGERROR(ERR_MALLOC_NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JPEG img;
|
strcat(result, stem);
|
||||||
if (!in)
|
strcat(result, ext);
|
||||||
{
|
unsigned int* dst_buf = read_ppm(location, &src);
|
||||||
LOGERROR(ERR_IMG_NULL);
|
if (!dst_buf)
|
||||||
return;
|
|
||||||
}
|
|
||||||
unsigned int* dst = read_ppm(in->name, in);
|
|
||||||
if (!dst)
|
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
img.width = in->width;
|
dst = inherit_from_ppm(&src);
|
||||||
img.height = in->height;
|
write_jpeg(dst_buf, &dst, result);
|
||||||
write_jpeg(dst, &img, result);
|
free(dst_buf);
|
||||||
free(dst);
|
|
||||||
free(result);
|
free(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
transcode_to_ppm(JPEG* in)
|
transcode_to_ppm(const char* location)
|
||||||
{
|
{
|
||||||
size_t length = find_extension(in->name);
|
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];
|
char str2[length];
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
str2[i] = in->name[i];
|
str2[i] = location[i];
|
||||||
}
|
}
|
||||||
char* str3 = ".ppm";
|
char* str3 = ".ppm";
|
||||||
char* result = malloc(strlen(str2) + strlen(str3) + 1);
|
char* result = malloc(strlen(str2) + strlen(str3) + 1);
|
||||||
if (result != NULL)
|
if (!result)
|
||||||
{
|
|
||||||
strcat(result, str2);
|
|
||||||
strcat(result, str3);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_MALLOC_NULL);
|
LOGERROR(ERR_MALLOC_NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PPM img;
|
strcat(result, str2);
|
||||||
if (!in)
|
strcat(result, str3);
|
||||||
{
|
unsigned int* dst_buf = read_jpeg(&src);
|
||||||
LOGERROR(ERR_IMG_NULL);
|
if (!dst_buf)
|
||||||
return;
|
|
||||||
}
|
|
||||||
unsigned int* dst = read_jpeg(in);
|
|
||||||
if (!dst)
|
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
img.width = in->width;
|
|
||||||
img.height = in->height;
|
dst = inherit_from_jpeg(&src, '6');
|
||||||
img.len = img.width * img.height;
|
write_ppm(dst_buf, dst, result);
|
||||||
write_ppm(dst, img, result);
|
|
||||||
free(dst);
|
|
||||||
free(result);
|
free(result);
|
||||||
|
free(dst_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
PPM
|
PPM
|
||||||
inherit_from_jpeg(const JPEG* in, const char fmt)
|
inherit_from_jpeg(const JPEG* metadata, const char fmt)
|
||||||
{
|
{
|
||||||
PPM img;
|
PPM image;
|
||||||
if (!in)
|
if (!metadata)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_IMG_NULL);
|
LOGERROR(ERR_IMG_NULL);
|
||||||
return img;
|
return image;
|
||||||
}
|
}
|
||||||
PPM image = { .width = in->width,
|
image.width = metadata->width;
|
||||||
.height = in->height,
|
image.height = metadata->height;
|
||||||
.len = in->len,
|
image.len = metadata->len;
|
||||||
.fmt = fmt };
|
image.fmt = fmt;
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
JPEG
|
JPEG
|
||||||
inherit_from_ppm(const PPM* in)
|
inherit_from_ppm(const PPM* metadata)
|
||||||
{
|
{
|
||||||
JPEG blank;
|
JPEG image;
|
||||||
if (!in)
|
if (!metadata)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_IMG_NULL);
|
LOGERROR(ERR_IMG_NULL);
|
||||||
return blank;
|
return image;
|
||||||
}
|
}
|
||||||
JPEG image = {
|
image.width = metadata->width;
|
||||||
.width = in->width,
|
image.height = metadata->height;
|
||||||
.height = in->height,
|
image.len = metadata->len;
|
||||||
.len = in->len,
|
|
||||||
};
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -18,9 +18,8 @@
|
|||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
PPM img;
|
char img[] = "../assets/IMG_1649_new.ppm";
|
||||||
img.name = "../assets/IMG_1649_new.ppm";
|
transcode_to_jpeg(img);
|
||||||
transcode_to_jpeg(&img);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-11
@@ -8,19 +8,20 @@
|
|||||||
unsigned int*
|
unsigned int*
|
||||||
u32_flop(const unsigned int* buf, const int width, const int height)
|
u32_flop(const unsigned int* buf, const int width, const int height)
|
||||||
{
|
{
|
||||||
|
int len, idx;
|
||||||
if (!buf)
|
if (!buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int len = width * height;
|
len = width * height;
|
||||||
unsigned int* flip = malloc(len * sizeof(unsigned int));
|
unsigned int* flip = malloc(len * sizeof(unsigned int));
|
||||||
if (!flip)
|
if (!flip)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_CREATION_FAIL);
|
LOGERROR(ERR_CREATION_FAIL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int idx = 0;
|
idx = 0;
|
||||||
for (int i = 0; i < height; i++)
|
for (int i = 0; i < height; i++)
|
||||||
{
|
{
|
||||||
for (int k = width - 1; k >= 0; k--)
|
for (int k = width - 1; k >= 0; k--)
|
||||||
@@ -37,12 +38,13 @@ u32_flop(const unsigned int* buf, const int width, const int height)
|
|||||||
unsigned int*
|
unsigned int*
|
||||||
u32_flip(const unsigned int* buf, const int width, const int height)
|
u32_flip(const unsigned int* buf, const int width, const int height)
|
||||||
{
|
{
|
||||||
|
int len, idx;
|
||||||
if (!buf)
|
if (!buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int len = width * height;
|
len = width * height;
|
||||||
unsigned int* flip = malloc(len * sizeof(unsigned int));
|
unsigned int* flip = malloc(len * sizeof(unsigned int));
|
||||||
if (!flip)
|
if (!flip)
|
||||||
{
|
{
|
||||||
@@ -50,7 +52,7 @@ u32_flip(const unsigned int* buf, const int width, const int height)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int idx = 0;
|
idx = 0;
|
||||||
for (int i = height - 1; i >= 0; i--)
|
for (int i = height - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
for (int k = 0; k < width; k++)
|
for (int k = 0; k < width; k++)
|
||||||
@@ -65,26 +67,26 @@ u32_flip(const unsigned int* buf, const int width, const int height)
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int*
|
unsigned int*
|
||||||
u32_rotate(const unsigned int* buf, PPM* m_img)
|
u32_rotate(const unsigned int* buf, PPM* metadata)
|
||||||
{
|
{
|
||||||
if (!buf)
|
if (!buf)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (!m_img)
|
if (!metadata)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_IMG_NULL);
|
LOGERROR(ERR_IMG_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
unsigned int* mask = malloc(m_img->len * sizeof(unsigned int));
|
unsigned int* mask = malloc(metadata->len * sizeof(unsigned int));
|
||||||
if (!mask)
|
if (!mask)
|
||||||
{
|
{
|
||||||
LOGERROR(ERR_BUF_NULL);
|
LOGERROR(ERR_BUF_NULL);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int old_width = m_img->width;
|
int old_width = metadata->width;
|
||||||
int old_height = m_img->height;
|
int old_height = metadata->height;
|
||||||
for (int row = 0; row < old_height; row++)
|
for (int row = 0; row < old_height; row++)
|
||||||
{
|
{
|
||||||
for (int col = 0; col < old_width; col++)
|
for (int col = 0; col < old_width; col++)
|
||||||
@@ -96,8 +98,8 @@ u32_rotate(const unsigned int* buf, PPM* m_img)
|
|||||||
buf[(row * old_width) + col];
|
buf[(row * old_width) + col];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_img->width = old_height;
|
metadata->width = old_height;
|
||||||
m_img->height = old_width;
|
metadata->height = old_width;
|
||||||
free((void*)buf);
|
free((void*)buf);
|
||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-8
@@ -37,26 +37,27 @@ u64to_u32(const unsigned long clr, const unsigned long u64_div)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void __attribute__((overloadable))
|
void __attribute__((overloadable))
|
||||||
print_attr(const PPM* src)
|
print_attr(const PPM* metadata)
|
||||||
{
|
{
|
||||||
printf("Name: %s\nFMT: %c\nWidth: %i, Height: %i\nLength: %i\n",
|
printf("Name: %s\nFMT: %c\nWidth: %i, Height: %i\nLength: %i\n",
|
||||||
src->name, src->fmt, src->width, src->height, src->len);
|
metadata->name, metadata->fmt, metadata->width, metadata->height,
|
||||||
|
metadata->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __attribute__((overloadable))
|
void __attribute__((overloadable))
|
||||||
print_attr(const JPEG* src)
|
print_attr(const JPEG* metadata)
|
||||||
{
|
{
|
||||||
printf("Name: %s\nWidth: %i, Height: %i\nLength: %i\n", src->name,
|
printf("Name: %s\nWidth: %i, Height: %i\nLength: %i\n", metadata->name,
|
||||||
src->width, src->height, src->len);
|
metadata->width, metadata->height, metadata->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
find_extension(const char* n)
|
find_extension(const char* str)
|
||||||
{
|
{
|
||||||
size_t length = strlen(n);
|
size_t length = strlen(str);
|
||||||
for (int i = length; i >= 0; i--)
|
for (int i = length; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (n[i] == '.')
|
if (str[i] == '.')
|
||||||
{
|
{
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user