resize.c - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
resize.c (2650B)
---
1 #ifdef WIN32
2 /* fcntl.h required for Windows binary mode */
3 #include <fcntl.h>
4 #include <winsock2.h>
5 #else
6 #include <arpa/inet.h>
7 #endif
8
9 #include <errno.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #define STB_IMAGE_RESIZE_IMPLEMENTATION
16 #include "stb_image_resize.h"
17
18 #define MAX_DIMENSION 1000000
19
20 struct image {
21 uint32_t width;
22 uint32_t height;
23 uint16_t *data;
24 };
25
26 struct header {
27 char sig[8];
28 uint32_t width;
29 uint32_t height;
30 };
31
32 int
33 main(int argc, char *argv[])
34 {
35 struct header hdr;
36 struct image in, tmp, out;
37 size_t i, in_len, out_len, tmp_len;
38 char *end;
39
40 if (argc != 3) {
41 fprintf(stderr, "usage: %s [width] [height]\n", argv[0]);
42 return 1;
43 }
44
45 /* required for Windows binary mode aka more retarded bullshit. */
46 #if _WIN32
47 _setmode(_fileno(stdin), _O_BINARY);
48 _setmode(_fileno(stdout), _O_BINARY);
49 _setmode(_fileno(stderr), _O_BINARY);
50 #endif
51
52 out.width = strtoul(argv[1], &end, 10);
53 if (!end || !out.width || out.width > MAX_DIMENSION) {
54 fprintf(stderr, "bad width given\n");
55 return 1;
56 }
57 out.height = strtoul(argv[2], &end, 10);
58 if (!end || !out.height || out.height > MAX_DIMENSION) {
59 fprintf(stderr, "bad height given\n");
60 return 1;
61 }
62
63 if (fread(&hdr, 1, sizeof(hdr), stdin) != sizeof(hdr)) {
64 fprintf(stderr, "incomplete header\n");
65 return 1;
66 }
67 if (memcmp("farbfeld", hdr.sig, strlen("farbfeld"))) {
68 fprintf(stderr, "invalid magic\n");
69 return 1;
70 }
71 in.width = ntohl(hdr.width);
72 in.height = ntohl(hdr.height);
73
74 hdr.width = htonl(out.width);
75 hdr.height = htonl(out.height);
76 if (fwrite(&hdr, sizeof(hdr), 1, stdout) != 1) {
77 fprintf(stderr, "unable to write header\n");
78 return 1;
79 }
80
81 tmp.width = in.height;
82 tmp.height = out.width;
83
84 in_len = in.width * in.height * 4;
85 out_len = out.width * out.height * 4;
86 tmp_len = tmp.width * tmp.height * 4;
87
88 in.data = malloc(in_len * sizeof(uint16_t));
89 out.data = malloc(out_len * sizeof(uint16_t));
90 tmp.data = malloc(tmp_len * sizeof(uint16_t));
91
92 if (!in.data || !out.data || !tmp.data) {
93 fprintf(stderr, "unable to allocate memory.\n");
94 return 1;
95 }
96
97 if (fread(in.data, sizeof(uint16_t), in_len, stdin) != in_len) {
98 fprintf(stderr, "unexpected EOF\n");
99 return 1;
100 }
101 for (i = 0; i < in_len; i++) {
102 in.data[i] = ntohs(in.data[i]);
103 }
104
105 stbir_resize_uint16_generic(in.data, in.width, in.height, 0,
106 out.data, out.width, out.height, 0,
107 4, 3, STBIR_FILTER_DEFAULT);
108
109 for (i = 0; i < out_len; i++) {
110 out.data[i] = htons(out.data[i]);
111 }
112 if (fwrite(out.data, sizeof(uint16_t), out_len, stdout) != out_len) {
113 fprintf(stderr, "write error\n");
114 return 1;
115 }
116 return 0;
117 }