Created
August 20, 2016 23:53
-
-
Save ShawnMcGrath/4bc2d6a3f0627c684e19556dc73ce70f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//by @sssmcgrath | |
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
#define STB_IMAGE_WRITE_IMPLEMENTATION | |
#include "stb_image_write.h" | |
#include <stdio.h> | |
#define isPowerOfTwo(v) (((v) & ((v) - 1)) == 0) | |
typedef unsigned char ubyte; | |
void flipv(ubyte *data, int dim, int channel_size, int num_channels) { | |
int row_size = dim * channel_size * num_channels; | |
ubyte *temp_buff = (ubyte *)malloc(row_size); | |
for (int i = 0; i < dim >> 1; ++i) { | |
memcpy(temp_buff, data + i * dim * channel_size * num_channels, row_size); | |
memcpy(data + i * dim * channel_size * num_channels, data + (dim - i - 1) * dim * channel_size * num_channels, row_size); | |
memcpy(data + (dim - i - 1) * dim * channel_size * num_channels, temp_buff, row_size); | |
} | |
free(temp_buff); | |
} | |
void fliph(ubyte *data, int dim, int channel_size, int num_channels) { | |
ubyte temp[256]; | |
for (int i = 0; i < dim; ++i) { | |
for (int j = 0; j < dim >> 1; ++j) { | |
int x0 = j; | |
int x1 = dim - j - 1; | |
memcpy(temp, data + i * dim * channel_size * num_channels + channel_size * num_channels * x0, channel_size * num_channels); | |
memcpy(data + i * dim * channel_size * num_channels + channel_size * num_channels * x0, data + i * dim * channel_size * num_channels + channel_size * num_channels * x1, channel_size * num_channels); | |
memcpy(data + i * dim * channel_size * num_channels + channel_size * num_channels * x1, temp, channel_size * num_channels); | |
} | |
} | |
} | |
void rot90cw(ubyte *data, int dim, int channel_size, int num_channels) { | |
ubyte *new_data = (ubyte *)malloc(dim * dim * channel_size * num_channels); | |
for (int i = 0; i < dim; ++i) { | |
for (int j = 0; j < dim; ++j) { | |
int i0 = i * dim + j; | |
int i1 = dim * (dim - j - 1) + i; | |
memcpy(new_data + i0 * channel_size * num_channels, data + i1 * channel_size * num_channels, channel_size * num_channels); | |
} | |
} | |
memcpy(data, new_data, dim * dim * channel_size * num_channels); | |
free(new_data); | |
} | |
void rot90ccw(ubyte *data, int dim, int channel_size, int num_channels) { | |
ubyte *new_data = (ubyte *)malloc(dim * dim * channel_size * num_channels); | |
for (int i = 0; i < dim; ++i) { | |
for (int j = 0; j < dim; ++j) { | |
int i0 = i * dim + j; | |
int i1 = dim * (j + 1) - 1 - i; | |
memcpy(new_data + i0 * channel_size * num_channels, data + i1 * channel_size * num_channels, channel_size * num_channels); | |
} | |
} | |
memcpy(data, new_data, dim * dim * channel_size * num_channels); | |
free(new_data); | |
} | |
int main(int argc, char **argv) { | |
int w, h, num_channels; | |
char buff[1024]; | |
const char *face_names[] = { | |
"posx", "negx", "posy", "negy", "posz", "negz" | |
}; | |
for (int i = 0; i < 6; ++i) { | |
sprintf(buff, "cubemap/%s.jpg", face_names[i]); | |
ubyte *data = stbi_load(buff, &w, &h, &num_channels, 0); | |
if (!data) { | |
printf("Unable to open file: %s\n", buff); | |
return -1; | |
} | |
if (w != h) { | |
printf("%s is not a square image, %d != %d", w, h); | |
return -2; | |
} | |
if (!isPowerOfTwo(w)) { | |
printf("%s is not a power of 2. width = %d\n", buff, w); | |
return -3; | |
} | |
if (i == 0) { // +X | |
sprintf(buff, "cubemap/out/%s.png", face_names[0]); | |
fliph(data, w, 1, num_channels); | |
rot90ccw(data, w, 1, num_channels); | |
} else if (i == 1) { // -X | |
sprintf(buff, "cubemap/out/%s.png", face_names[1]); | |
flipv(data, w, 1, num_channels); | |
rot90ccw(data, w, 1, num_channels); | |
} else if (i == 2) { // +Y | |
sprintf(buff, "cubemap/out/%s.png", face_names[4]); | |
flipv(data, w, 1, num_channels); | |
} else if (i == 3) { // -Y | |
sprintf(buff, "cubemap/out/%s.png", face_names[5]); | |
fliph(data, w, 1, num_channels); | |
} else if (i == 4) { // +Z | |
sprintf(buff, "cubemap/out/%s.png", face_names[2]); | |
flipv(data, w, 1, num_channels); | |
} else if (i == 5) { // -Z | |
sprintf(buff, "cubemap/out/%s.png", face_names[3]); | |
fliph(data, w, 1, num_channels); | |
} | |
stbi_write_png(buff, w, h, num_channels, data, 0); | |
free(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment