pixzoom.c (1447B)
1#include <stdlib.h> 2#include <stdio.h> 3#include <stdbool.h> 4#include <err.h> 5 6#define STB_IMAGE_IMPLEMENTATION 7#include "stb_image.h" 8#define STB_IMAGE_WRITE_IMPLEMENTATION 9#include "stb_image_write.h" 10 11int 12main(int argc, const char **argv) 13{ 14 const char *inpath; 15 const char *outpath; 16 uint8_t *src, *dst; 17 uint8_t rgb[3]; 18 int width, height; 19 int x, y, dx, dy; 20 int scale; 21 int i, off; 22 23 scale = 2; 24 inpath = outpath = NULL; 25 for (i = 1; i < argc; i++) { 26 if (!strcmp(argv[i], "-s")) 27 scale = atoi(argv[++i]); 28 else if (!strcmp(argv[i], "-h")) 29 errx(0, "pixzoom [-s SCALE] IN [OUT]"); 30 else if (inpath && outpath) 31 errx(1, "Too many images specified\n"); 32 else if (inpath) 33 outpath = argv[i]; 34 else 35 inpath = argv[i]; 36 } 37 38 if (!inpath) errx(1, "Missing image argument\n"); 39 if (!outpath) outpath = "out.png"; 40 41 src = stbi_load(inpath, &width, &height, NULL, STBI_rgb); 42 if (!src) errx(1, "Failed to load image"); 43 44 dst = calloc(width * height * scale * scale, 3); 45 if (!src) err(1, "calloc"); 46 47 for (y = 0; y < height; y++) { 48 for (x = 0; x < width; x++) { 49 memcpy(rgb, src + (y * width + x) * 3, 3); 50 for (dy = 0; dy < scale; dy++) { 51 for (dx = 0; dx < scale; dx++) { 52 off = (y * scale + dy) * width * scale; 53 off += x * scale + dx; 54 memcpy(dst + off * 3, rgb, 3); 55 } 56 } 57 } 58 } 59 60 stbi_write_png(outpath, width * scale, height * scale, 61 STBI_rgb, dst, width * scale * 3); 62 63 STBI_FREE(src); 64 free(dst); 65}