├── .gitignore ├── .npmignore ├── History.md ├── Makefile ├── Readme.md ├── src └── img.c └── test ├── apple.png ├── firefox.png ├── google.gif ├── icon.gif ├── jpeg ├── puffer.gif └── tux.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.sock 4 | img 5 | *.o 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.0.2 / 2011-09-08 3 | ================== 4 | 5 | * Fixed sign 6 | 7 | 0.0.1 / 2011-08-28 8 | ================== 9 | 10 | * Initial commit 11 | 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | SRC = $(shell find src/*.c) 3 | OBJ = $(SRC:.c=.o) 4 | CFLAGS = -std=c99 5 | PREFIX = /usr/local 6 | 7 | img: $(OBJ) 8 | $(CC) $^ -o $@ 9 | 10 | install: img 11 | cp -f $< $(PREFIX)/bin/$< 12 | 13 | uninstall: 14 | rm -f $(PREFIX)/bin/img 15 | 16 | test: 17 | @echo PNG 18 | @./img test/firefox.png 19 | @./img test/apple.png 20 | @./img test/tux.png 21 | @echo 22 | @echo GIF 23 | @./img test/puffer.gif 24 | @./img test/google.gif 25 | @echo 26 | @echo JPEG 27 | @./img test/jpeg 28 | 29 | clean: 30 | rm -f img 31 | 32 | .PHONY: clean test install uninstall -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # img 3 | 4 | image dimension reader. 5 | 6 | ## Why? 7 | 8 | Because I'm lazy. Typically CSS compilers like SASS and Stylus support functions that allow you to read image dimensions directly in the language, however if and when you are working with regular CSS this can be a pain in the ass. 9 | 10 | ## Installation 11 | 12 | $ make install 13 | 14 | ## Usage 15 | 16 | ``` 17 | 18 | Usage: img 19 | 20 | ``` 21 | 22 | ## Examples 23 | 24 | ```bash 25 | $ img test/firefox.png 26 | width: 350px; 27 | height: 279px; 28 | ``` 29 | 30 | ## Format support 31 | 32 | Currently only __PNG__ and __GIF__ are supported, because it's 33 | pretty rare (for myself at least) to use JPEGs or others in 34 | CSS, though feel free to submit a pull-request. 35 | 36 | ## License 37 | 38 | (The MIT License) 39 | 40 | Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca> 41 | 42 | Permission is hereby granted, free of charge, to any person obtaining 43 | a copy of this software and associated documentation files (the 44 | 'Software'), to deal in the Software without restriction, including 45 | without limitation the rights to use, copy, modify, merge, publish, 46 | distribute, sublicense, and/or sell copies of the Software, and to 47 | permit persons to whom the Software is furnished to do so, subject to 48 | the following conditions: 49 | 50 | The above copyright notice and this permission notice shall be 51 | included in all copies or substantial portions of the Software. 52 | 53 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 54 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 55 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 56 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 57 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 58 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 59 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /src/img.c: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // img.c 4 | // 5 | // Copyright (c) 2011 TJ Holowaychuk 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | /* 15 | * Program version. 16 | */ 17 | 18 | #define VERSION "0.0.2" 19 | 20 | /* 21 | * Sniff bytes for JPEG's magic number ff d8. 22 | */ 23 | 24 | int 25 | is_jpeg(uint8_t *buf) { 26 | return 0xff == buf[0] && 0xd8 == buf[1]; 27 | } 28 | 29 | /* 30 | * Sniff bytes 0..2 for "GIF". 31 | */ 32 | 33 | int 34 | is_gif(uint8_t *buf) { 35 | return 'G' == buf[0] && 'I' == buf[1] && 'F' == buf[2]; 36 | } 37 | 38 | /* 39 | * Sniff bytes 1..3 for "PNG". 40 | */ 41 | 42 | int 43 | is_png(uint8_t *buf) { 44 | return 'P' == buf[1] && 'N' == buf[2] && 'G' == buf[3]; 45 | } 46 | 47 | /* 48 | * Read big-endian uint32 from `buf`. 49 | */ 50 | 51 | int 52 | read_uint32(uint8_t *buf) { 53 | return buf[0] << 24 54 | | buf[1] << 16 55 | | buf[2] << 8 56 | | buf[3]; 57 | } 58 | 59 | /* 60 | * Read little-endian uint16 from `buf`. 61 | */ 62 | 63 | int 64 | read_uint16(uint8_t *buf) { 65 | return buf[1] << 8 66 | | buf[0]; 67 | } 68 | 69 | /* 70 | * Output CSS for `width` / `height`. 71 | */ 72 | 73 | void 74 | output_css_dimensions(int width, int height) { 75 | printf("width: %dpx;\n", width); 76 | printf("height: %dpx;\n", height); 77 | } 78 | 79 | /* 80 | * Read `len` bytes at the given `offset` into `buf`. 81 | */ 82 | 83 | void 84 | read_from(const char *path, char *buf, int len, int offset) { 85 | // open file 86 | int fd = open(path, O_RDONLY); 87 | if (fd < 0) { 88 | perror("open()"); 89 | exit(1); 90 | } 91 | 92 | // read bytes 93 | if (pread(fd, buf, len, offset) < 0) { 94 | perror("read()"); 95 | exit(1); 96 | } 97 | close(fd); 98 | } 99 | 100 | /* 101 | * Output GIF dimensions for `path`. 102 | */ 103 | 104 | void 105 | output_gif_dimensions(const char *path) { 106 | char buf[4]; 107 | read_from(path, buf, 4, 6); 108 | output_css_dimensions( 109 | read_uint16(buf) 110 | , read_uint16(buf + 2)); 111 | } 112 | 113 | /* 114 | * Output PNG dimensions for `path`. 115 | */ 116 | 117 | void 118 | output_png_dimensions(const char *path) { 119 | // read bytes 120 | char buf[8]; 121 | read_from(path, buf, 8, 16); 122 | output_css_dimensions( 123 | read_uint32(buf) 124 | , read_uint32(buf + 4)); 125 | } 126 | 127 | /* 128 | * Output JPEG dimensions for `path`. 129 | */ 130 | 131 | void 132 | output_jpeg_dimensions(const char *path) { 133 | fprintf(stderr, "jpeg not yet supported\n"); 134 | exit(1); 135 | } 136 | 137 | /* 138 | * Output usage information. 139 | */ 140 | 141 | void 142 | usage() { 143 | printf( 144 | "\n" 145 | " Usage: img \n" 146 | "\n"); 147 | exit(1); 148 | } 149 | 150 | /* 151 | * Handle args. 152 | */ 153 | 154 | int 155 | main(int argc, const char **argv){ 156 | // path required 157 | if (argc < 2) usage(); 158 | 159 | const char *path = argv[1]; 160 | 161 | // open file 162 | int fd = open(path, O_RDONLY); 163 | if (fd < 0) { 164 | perror("open()"); 165 | exit(1); 166 | } 167 | 168 | // read bytes 169 | char buf[5]; 170 | if (read(fd, &buf, 5) < 0) { 171 | perror("read()"); 172 | exit(1); 173 | } 174 | close(fd); 175 | 176 | // sniff bytes 177 | if (is_gif(buf)) output_gif_dimensions(path); 178 | else if (is_png(buf)) output_png_dimensions(path); 179 | else if (is_jpeg(buf)) output_jpeg_dimensions(path); 180 | 181 | return 0; 182 | } -------------------------------------------------------------------------------- /test/apple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/apple.png -------------------------------------------------------------------------------- /test/firefox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/firefox.png -------------------------------------------------------------------------------- /test/google.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/google.gif -------------------------------------------------------------------------------- /test/icon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/icon.gif -------------------------------------------------------------------------------- /test/jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/jpeg -------------------------------------------------------------------------------- /test/puffer.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/puffer.gif -------------------------------------------------------------------------------- /test/tux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tj/img/e5a9fa685c6f065eca4c8b6d58e50e3b36f068ba/test/tux.png --------------------------------------------------------------------------------