SHOW:
|
|
- or go back to the newest paste.
1 | - | // NOTE: compile with g++ filename.cpp -std=c++11 |
1 | + | /* |
2 | - | // also, if this doesn't work for you, try the alternate char-based version |
2 | + | g++ tweetart.cpp -o tweetart |
3 | Original version: http://pastebin.com/uQkCQGhz | |
4 | - | #include <iostream> |
4 | + | Cleaned up version: http://pastebin.com/index/uQkCQGhz |
5 | - | #include <cmath> |
5 | + | */ |
6 | #include <stdio.h> | |
7 | #include <math.h> | |
8 | #include <stdint.h> // uint8_t | |
9 | #define DIM 1024 | |
10 | #define DM1 (DIM-1) | |
11 | #define MAX_COLORS 255 // or 255, or 65535 | |
12 | - | unsigned short GR(int,int); |
12 | + | |
13 | - | unsigned short BL(int,int); |
13 | + | |
14 | #define _cr(x) (unsigned short)(pow((x),1.0/3.0)) // cube root | |
15 | - | unsigned short RD(int i,int j){ |
15 | + | |
16 | - | // YOUR CODE HERE |
16 | + | typedef uint8_t u8; |
17 | typedef uint16_t u16; | |
18 | - | unsigned short GR(int i,int j){ |
18 | + | u16 GR(int,int); |
19 | - | // YOUR CODE HERE |
19 | + | u16 BL(int,int); |
20 | ||
21 | - | unsigned short BL(int i,int j){ |
21 | + | u16 RD(int i,int j){ |
22 | - | // YOUR CODE HERE |
22 | + | return (i&j); // YOUR CODE HERE |
23 | } | |
24 | u16 GR(int i,int j){ | |
25 | - | void pixel_write(int,int); |
25 | + | return (i&j); // YOUR CODE HERE |
26 | } | |
27 | u16 BL(int i,int j){ | |
28 | - | fp = fopen("MathPic","wb"); |
28 | + | return (i&j); // YOUR CODE HERE |
29 | - | fprintf(fp, "P6\n%d %d\n1023\n", DIM, DIM); |
29 | + | |
30 | ||
31 | FILE *fp; | |
32 | void pixel_write(int i, int j){ | |
33 | static u16 color[3]; | |
34 | color[0] = RD(i,j) & MAX_COLORS; | |
35 | color[1] = GR(i,j) & MAX_COLORS; | |
36 | color[2] = BL(i,j) & MAX_COLORS; | |
37 | - | static unsigned short color[3]; |
37 | + | if (MAX_COLORS < 256){ |
38 | - | color[0] = RD(i,j)&DM1; |
38 | + | color[0] &= 0xFF; |
39 | - | color[1] = GR(i,j)&DM1; |
39 | + | color[0] |= ((color[1] & 0xFF) << 8); |
40 | - | color[2] = BL(i,j)&DM1; |
40 | + | color[1] = color[2]; |
41 | - | fwrite(color, 2, 3, fp); |
41 | + | } |
42 | // 8-bit = 3 bytes | |
43 | //16-bit = 6 bytes | |
44 | fwrite(color, 1+(MAX_COLORS>255), 3, fp); | |
45 | } | |
46 | ||
47 | int main(){ | |
48 | fp = fopen("mathpic.ppm","wb"); | |
49 | if( !fp ) return -1; | |
50 | fprintf(fp, "P6\n%d %d\n%d\n", DIM, DIM, MAX_COLORS); | |
51 | for(int j=0;j<DIM;j++) | |
52 | for(int i=0;i<DIM;i++) | |
53 | pixel_write(i,j); | |
54 | fclose(fp); | |
55 | return 0; | |
56 | } |