Last active
December 18, 2015 21:12
-
-
Save masterzorag/9b1fea7a13b74c29addc to your computer and use it in GitHub Desktop.
C simple scale 2X, pixelate
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
/* gcc -o demo draw_png_2x.c | |
# ./demo 8 | |
0000 00 | |
0000 01 | |
1000 02 | |
0000 03 | |
00000000000000000000000000000000 00 | |
00000000000000000000000000000000 01 | |
00000000000000000000000000000000 02 | |
00000000000000000000000000000000 03 | |
11000000000000000000000000000000 04 | |
11000000000000000000000000000000 05 | |
00000000000000000000000000000000 06 | |
00000000000000000000000000000000 07 | |
# ./demo 5 | |
0000 00 | |
0100 01 | |
0001 02 | |
0000 03 | |
00000000000000000000000000000000 00 | |
00000000000000000000000000000000 01 | |
00110000000000000000000000000000 02 | |
00110000000000000000000000000000 03 | |
00000011000000000000000000000000 04 | |
00000011000000000000000000000000 05 | |
00000000000000000000000000000000 06 | |
00000000000000000000000000000000 07 | |
*/ | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define CANVAS_W 32 | |
#define CANVAS_H 8 | |
int32_t canvas[CANVAS_W * CANVAS_H]; | |
#define PNG_W 4 | |
int32_t png[PNG_W * 4]; | |
void print(int32_t *d, int32_t x, int32_t y) | |
{ | |
int w, h; | |
for(h = 0; h < y; h++) | |
{ | |
for(w = 0; w < x; w++) printf("%x", d[(h * x) + w]); // (H * CANVAS_W) + W | |
printf(" %.2d\n", h); | |
} | |
} | |
/*********************************************************************** | |
* draw png part into frame. | |
* | |
* int32_t idx = index of loaded png | |
* int32_t can_x = start x coordinate into canvas | |
* int32_t can_y = start y coordinate into canvas | |
* int32_t png_x = start x coordinate into png | |
* int32_t png_y = start y coordinate into png | |
* int32_t w = width of png part to blit | |
* int32_t h = height of png part to blit | |
***********************************************************************/ | |
int32_t draw_png(const int32_t idx, | |
const int32_t c_x, const int32_t c_y, // coordinate into canvas | |
const int32_t p_x, const int32_t p_y, // coordinate into png | |
const int32_t w, const int32_t h) // size of png part to blit | |
{ | |
int32_t i, k; | |
int32_t *px = NULL, *src = NULL; | |
for(i = 0; i < h; i++) | |
for(k = 0; k < w; k++) | |
{ | |
if((c_x + k < CANVAS_W) | |
&& (c_y + i < CANVAS_H)) | |
{ | |
// *px = mix_color(*px, ctx.png[idx].addr[(p_x + p_y * ctx.png[idx].w) + (k + i * ctx.png[idx].w)]); | |
src = &png[(p_x + p_y * PNG_W) + (k + i * PNG_W)]; | |
// px = &ctx.canvas[(c_y + i) * CANVAS_W + c_x + k]; // (H * CANVAS_W) + W | |
/* | |
(2* H ) * CANVAS_W) + 2* W, | |
(2* H ) * CANVAS_W) + 2* W +1, | |
(2* H +1) * CANVAS_W) + 2* W, | |
(2* H +1) * CANVAS_W) + 2* W +1, | |
*/ | |
px = &canvas[ 2* (c_y + i) * CANVAS_W + 2* (c_x + k) ]; *px = *src; | |
px = &canvas[ 2* (c_y + i) * CANVAS_W + 2* (c_x + k) +1]; *px = *src; | |
px = &canvas[(2* (c_y + i) +1) * CANVAS_W + 2* (c_x + k) ]; *px = *src; | |
px = &canvas[(2* (c_y + i) +1) * CANVAS_W + 2* (c_x + k) +1]; *px = *src; | |
} | |
} | |
return (c_x + w); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
memset(&canvas, 0, sizeof(canvas)); | |
png[atoi(argv[1])] = png[atoi(argv[1]) +PNG_W +2] = 1; | |
print(png, PNG_W, 4); | |
draw_png(0, 0, 0, 0, 0, 4, 4); | |
print(canvas, CANVAS_W, CANVAS_H); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment