i/o changes and starting box_blur
This commit is contained in:
@@ -1,8 +1,3 @@
|
|||||||
# ---
|
|
||||||
# Checks: 'bugprone-*,modernize-*,performance-*,readability-*'
|
|
||||||
# WarningsAsErrors: ''
|
|
||||||
# ---
|
|
||||||
|
|
||||||
---
|
---
|
||||||
# Enable a broad range of useful C checks
|
# Enable a broad range of useful C checks
|
||||||
Checks: >
|
Checks: >
|
||||||
+91
-32
@@ -1,5 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -36,6 +37,11 @@ typedef struct
|
|||||||
char fmt[3];
|
char fmt[3];
|
||||||
} Image;
|
} Image;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
unsigned int red, green, blue;
|
||||||
|
} Pixel;
|
||||||
|
|
||||||
static inline unsigned int
|
static inline unsigned int
|
||||||
set_alpha(const unsigned int clr, const unsigned char alpha_value)
|
set_alpha(const unsigned int clr, const unsigned char alpha_value)
|
||||||
{
|
{
|
||||||
@@ -128,30 +134,39 @@ ppm_encode(const char* file, Image* img)
|
|||||||
img->len = img->width * img->height;
|
img->len = img->width * img->height;
|
||||||
fscanf(fptr, "%d", &max_val);
|
fscanf(fptr, "%d", &max_val);
|
||||||
fgetc(fptr);
|
fgetc(fptr);
|
||||||
unsigned int* buf = malloc(img->len * sizeof(unsigned int));
|
unsigned char* img_buf = malloc(img->len * 3);
|
||||||
if (!buf) return nullptr;
|
if (!img_buf) return nullptr;
|
||||||
|
unsigned int* clr_buf = malloc(img->len * sizeof(unsigned int));
|
||||||
|
if (!clr_buf)
|
||||||
|
{
|
||||||
|
free(img_buf);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
if (fmt[1] == '3')
|
if (fmt[1] == '3')
|
||||||
{
|
{
|
||||||
for (int i = 0; i < img->len; i++)
|
for (int i = 0; i < img->len; i++)
|
||||||
{
|
{
|
||||||
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
|
fscanf(fptr, "%hhu %hhu %hhu", &red, &green, &blue);
|
||||||
buf[i] = encode(red, green, blue);
|
clr_buf[i] = encode(red, green, blue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (fmt[1] == '6')
|
else if (fmt[1] == '6')
|
||||||
{
|
{
|
||||||
|
fread(img_buf, 3, img->len, fptr);
|
||||||
|
|
||||||
for (int i = 0; i < img->len; i++)
|
for (int i = 0; i < img->len; i++)
|
||||||
{
|
{
|
||||||
fread(&red, sizeof(unsigned char), 1, fptr);
|
red = img_buf[i * 3];
|
||||||
fread(&green, sizeof(unsigned char), 1, fptr);
|
green = img_buf[i * 3 + 1];
|
||||||
fread(&blue, sizeof(unsigned char), 1, fptr);
|
blue = img_buf[i * 3 + 2];
|
||||||
buf[i] = encode(red, green, blue);
|
clr_buf[i] = encode(red, green, blue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fptr);
|
fclose(fptr);
|
||||||
|
free(img_buf);
|
||||||
printf("Encoding done!\n");
|
printf("Encoding done!\n");
|
||||||
return buf;
|
return clr_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
@@ -166,6 +181,9 @@ ppm_apply_brightness(unsigned int* buf, const Image img, const float factor)
|
|||||||
static inline void
|
static inline void
|
||||||
ppm_decode(const unsigned int* buf, const Image img)
|
ppm_decode(const unsigned int* buf, const Image img)
|
||||||
{
|
{
|
||||||
|
unsigned char red, green, blue;
|
||||||
|
unsigned char* dst_buf = malloc(img.len * 3);
|
||||||
|
if (!dst_buf) return;
|
||||||
printf("Starting decode...\n");
|
printf("Starting decode...\n");
|
||||||
FILE* fptr;
|
FILE* fptr;
|
||||||
|
|
||||||
@@ -178,9 +196,9 @@ ppm_decode(const unsigned int* buf, const Image img)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < img.len; i++)
|
for (int i = 0; i < img.len; i++)
|
||||||
{
|
{
|
||||||
unsigned char red = decode_r(buf[i]);
|
red = decode_r(buf[i]);
|
||||||
unsigned char green = decode_g(buf[i]);
|
green = decode_g(buf[i]);
|
||||||
unsigned char blue = decode_b(buf[i]);
|
blue = decode_b(buf[i]);
|
||||||
fprintf(fptr, "%d %d %d ", red, green, blue);
|
fprintf(fptr, "%d %d %d ", red, green, blue);
|
||||||
if (i % 7 == 0)
|
if (i % 7 == 0)
|
||||||
{
|
{
|
||||||
@@ -192,15 +210,14 @@ ppm_decode(const unsigned int* buf, const Image img)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < img.len; i++)
|
for (int i = 0; i < img.len; i++)
|
||||||
{
|
{
|
||||||
unsigned char red = decode_r(buf[i]);
|
dst_buf[i * 3] = decode_r(buf[i]);
|
||||||
unsigned char green = decode_g(buf[i]);
|
dst_buf[i * 3 + 1] = decode_g(buf[i]);
|
||||||
unsigned char blue = decode_b(buf[i]);
|
dst_buf[i * 3 + 2] = decode_b(buf[i]);
|
||||||
fwrite(&red, 1, 1, fptr);
|
|
||||||
fwrite(&green, 1, 1, fptr);
|
|
||||||
fwrite(&blue, 1, 1, fptr);
|
|
||||||
}
|
}
|
||||||
|
fwrite(dst_buf, 1, img.len * 3, fptr);
|
||||||
}
|
}
|
||||||
fclose(fptr);
|
fclose(fptr);
|
||||||
|
free(dst_buf);
|
||||||
printf("Decode Done!\n");
|
printf("Decode Done!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,6 +235,9 @@ channel_mask(const unsigned int clr, const char keep)
|
|||||||
case 'b':
|
case 'b':
|
||||||
return clr & (BYTE_A | BYTE_B);
|
return clr & (BYTE_A | BYTE_B);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -228,7 +248,7 @@ chromatic_circular_shift(const unsigned int clr, const int n)
|
|||||||
unsigned int red = decode_r(clr);
|
unsigned int red = decode_r(clr);
|
||||||
unsigned int green = decode_g(clr);
|
unsigned int green = decode_g(clr);
|
||||||
unsigned int blue = decode_b(clr);
|
unsigned int blue = decode_b(clr);
|
||||||
|
if (n > 8) return 0;
|
||||||
red = ((red << n) | (red >> (SHIFT_8 - n))) & BYTE;
|
red = ((red << n) | (red >> (SHIFT_8 - n))) & BYTE;
|
||||||
green = ((green << n) | (green >> (SHIFT_8 - n))) & BYTE;
|
green = ((green << n) | (green >> (SHIFT_8 - n))) & BYTE;
|
||||||
blue = ((blue << n) | (blue >> (SHIFT_8 - n))) & BYTE;
|
blue = ((blue << n) | (blue >> (SHIFT_8 - n))) & BYTE;
|
||||||
@@ -310,30 +330,69 @@ rotate(unsigned int* buf, Image* img, int len)
|
|||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
get_neighbor_idx(int curr_idx, int width, char* direction)
|
||||||
|
{
|
||||||
|
if (strncmp(direction, "above", 5) == 0) return curr_idx - width;
|
||||||
|
if (strncmp(direction, "below", 5) == 0) return curr_idx + width;
|
||||||
|
if (strncmp(direction, "right", 5) == 0) return curr_idx + 1;
|
||||||
|
if (strncmp(direction, "left", 4) == 0) return curr_idx + 1;
|
||||||
|
|
||||||
|
return curr_idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
is_valid(int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
if (x < 0 || x >= width) return 0;
|
||||||
|
if (y < 0 || y >= height) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline unsigned int
|
||||||
|
brightness_mirror(unsigned int clr)
|
||||||
|
{
|
||||||
|
unsigned char red = decode_r(clr);
|
||||||
|
unsigned char blue = decode_b(clr);
|
||||||
|
unsigned char green = (red + blue) / 2;
|
||||||
|
return encode(red, green, blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline unsigned int
|
||||||
|
get_pixel_safe(unsigned int* buf, int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
if (is_valid(x, y, width, height))
|
||||||
|
{
|
||||||
|
return buf[(y * width + x)];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline unsigned int*
|
||||||
|
box_blur(unsigned int* src_buf, Image* img, int len)
|
||||||
|
{
|
||||||
|
unsigned int* dst_buf = malloc(len * sizeof(unsigned int));
|
||||||
|
// Loop every pixel
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
free(src_buf);
|
||||||
|
return dst_buf;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
Image img = { .width = 3,
|
Image img = { .width = 3,
|
||||||
.height = 2,
|
.height = 2,
|
||||||
.name = "../color.ppm",
|
.name = "../color.ppm",
|
||||||
.fmt = "P3",
|
.fmt = "P6",
|
||||||
.len = 0 };
|
.len = 0 };
|
||||||
|
|
||||||
unsigned int* buf = ppm_encode("../assets/8ktest.ppm", &img);
|
unsigned int* buf = ppm_encode("../assets/wtest_p6.ppm.ppm", &img);
|
||||||
if (buf)
|
if (buf)
|
||||||
{
|
{
|
||||||
ppm_apply_brightness(buf, img, 2);
|
|
||||||
|
|
||||||
for (int i = 0; i < img.len; i++)
|
|
||||||
{
|
|
||||||
buf[i] = channel_mask(buf[i], 'r');
|
|
||||||
// buf[i] = chromatic_circular_shift(buf[i], (rand() %
|
|
||||||
// 5)); buf[i] = constrast_adjustment(buf[i], .1f);
|
|
||||||
// buf[i] = adjust_brightness(buf[i], 1.5f);
|
|
||||||
// buf[i] = swap_rb(buf[i]);
|
|
||||||
// buf[i] = to_grayscale(buf[i]);
|
|
||||||
}
|
|
||||||
// buf = rotate(buf, &image, image.len);
|
|
||||||
ppm_decode(buf, img);
|
ppm_decode(buf, img);
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user