Skip to content

Instantly share code, notes, and snippets.

@aspschn
Created August 5, 2022 00:16
Show Gist options
  • Select an option

  • Save aspschn/e51c2e065a620d2f1c2dd7ba7d4fb37e to your computer and use it in GitHub Desktop.

Select an option

Save aspschn/e51c2e065a620d2f1c2dd7ba7d4fb37e to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>
#define WIDTH 3840
#define HEIGHT 2160
struct image_t
{
uint8_t *data;
uint32_t width;
uint32_t height;
};
[[gnu::noinline]]
void resize_with_for(struct image_t *image, uint32_t width, uint32_t height)
{
auto target_width = width;
auto target_height = height;
uint8_t *new_data = (uint8_t*)malloc(
((sizeof(uint8_t) * 4) * (target_width * target_height)));
uint32_t *pixel = (uint32_t*)new_data;
/*
for (uint64_t i = 0; i < (target_width * target_height); ++i) {
*pixel = 0x00000000;
++pixel;
}
*/
/*
if (target_width > image->width) {
for (uint64_t y = 0; y < target_height; ++y) {
uint64_t diff = target_width - image->width;
for (uint64_t x = diff; x < target_width; ++x) {
*(pixel + target_width + x) = 0x00000000;
}
pixel = pixel + diff;
}
}
if (target_height > image->height) {
pixel = (uint32_t*)new_data + (target_width * image->height);
for (uint64_t i = 0; i < target_height; ++i) {
*pixel = 0x00000000;
++pixel;
}
}
*/
/*
memset(new_data, 0x0, (sizeof(uint32_t) * (target_width * target_height)));
*/
auto limit_width = (image->width <= target_width)
? image->width
: target_width;
auto limit_height = (image->height <= target_height)
? image->height
: target_height;
for (uint64_t y = 0; y < image->height; ++y) {
if (y >= limit_height) {
continue;
}
for (uint64_t x = 0; x < image->width; ++x) {
if (x >= limit_width) {
continue;
}
uint64_t src_y = image->width * y;
uint32_t *src = (uint32_t*)(image->data) + (src_y + x);
uint64_t dst_y = target_width * y;
uint32_t *dst =
(uint32_t*)(new_data) + (dst_y + x);
*dst = *src;
}
}
free(image->data);
image->data = new_data;
image->width = width;
image->height = height;
}
[[gnu::noinline]]
void resize_with_memcpy(struct image_t *image, uint32_t width, uint32_t height)
{
auto target_width = width;
auto target_height = height;
uint8_t *new_data = (uint8_t*)malloc(
((sizeof(uint8_t) * 4) * (target_width * target_height)));
uint32_t *pixel = (uint32_t*)new_data;
/*
for (uint64_t i = 0; i < (target_width * target_height); ++i) {
*pixel = 0x00000000;
++pixel;
}
*/
if (target_width > image->width) {
for (uint64_t y = 0; y < target_height; ++y) {
uint64_t diff = target_width - image->width;
for (uint64_t x = diff; x < target_width; ++x) {
*(pixel + target_width + x) = 0x00000000;
}
pixel = pixel + diff;
}
}
if (target_height > image->height) {
pixel = (uint32_t*)new_data + (target_width * image->height);
for (uint64_t i = 0; i < target_height; ++i) {
*pixel = 0x00000000;
++pixel;
}
}
/*
uint8_t *new_data = (uint8_t*)calloc(
(target_width * target_height), sizeof(uint8_t) * 4);
*/
// Doing thing.
auto limit_width = (image->width <= target_width)
? image->width
: target_width;
auto limit_height = (image->height <= target_height)
? image->height
: target_height;
for (uint64_t y = 0; y < limit_height; ++y) {
uint64_t src_y = image->width * y;
uint32_t *src = (uint32_t*)(image->data) + (src_y);
uint64_t dst_y = target_width * y;
uint32_t *dst = (uint32_t*)(new_data) + (dst_y);
memcpy(dst, src, (sizeof(uint32_t)) * limit_width);
}
free(image->data);
image->data = new_data;
image->width = width;
image->height = height;
}
uint64_t get_now_ms()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
int main(int argc, char *argv[])
{
uint64_t begin, end;
uint8_t *data = (uint8_t*)malloc(
((sizeof(uint8_t) * 4) * (WIDTH * HEIGHT)));
struct image_t image;
image.data = data;
image.width = WIDTH;
image.height = HEIGHT;
begin = get_now_ms();
for (uint32_t a = 1024; a < 1920; ++a) {
resize_with_for(&image, a, a);
}
end = get_now_ms();
fprintf(stderr, "For: %ldms\n", end - begin);
begin = get_now_ms();
for (uint32_t a = 1024; a < 1920; ++a) {
resize_with_memcpy(&image, a, a);
}
end = get_now_ms();
fprintf(stderr, "Mem: %ldms\n", end - begin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment