Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit bd596372a332cde3ef75b3e439248a2ef793a0aa @tj committed Aug 28, 2011
@@ -0,0 +1,5 @@
+.DS_Store
+node_modules
+*.sock
+img
+*.o
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
@@ -0,0 +1,29 @@
+
+SRC = $(shell find src/*.c)
+OBJ = $(SRC:.c=.o)
+CFLAGS = -std=c99
+PREFIX = /usr/local
+
+img: $(OBJ)
+ $(CC) $^ -o $@
+
+install: img
+ cp -f $< $(PREFIX)/bin/$<
+
+uninstall:
+ rm -f $(PREFIX)/bin/img
+
+test:
+ @echo PNG
+ @./img test/firefox.png
+ @echo
+ @echo GIF
+ @./img test/puffer.gif
+ @echo
+ @echo JPEG
+ @./img test/jpeg
+
+clean:
+ rm -f img
+
+.PHONY: clean test install uninstall
@@ -0,0 +1,53 @@
+
+# img
+
+ image dimension reader.
+
+## Why?
+
+ 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.
+
+## Installation
+
+ $ make install
+
+## Usage
+
+```
+
+Usage: img <path>
+
+```
+
+## Examples
+
+```bash
+$ img test/firefox.png
+width: 350px;
+height: 279px;
+```
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
+{
+ "name": "img"
+ , "version": "0.0.1"
+ , "description": "image dimension checker"
+ , "keywords": []
+ , "author": "TJ Holowaychuk <tj@vision-media.ca>"
+ , "dependencies": {}
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
@@ -0,0 +1,179 @@
+
+//
+// img.c
+//
+// Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+//
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+/*
+ * Sniff bytes for JPEG's magic number ff d8.
+ */
+
+int
+is_jpeg(uint8_t *buf) {
+ return 0xff == buf[0] && 0xd8 == buf[1];
+}
+
+/*
+ * Sniff bytes 0..2 for "GIF".
+ */
+
+int
+is_gif(uint8_t *buf) {
+ return 'G' == buf[0] && 'I' == buf[1] && 'F' == buf[2];
+}
+
+/*
+ * Sniff bytes 1..3 for "PNG".
+ */
+
+int
+is_png(uint8_t *buf) {
+ return 'P' == buf[1] && 'N' == buf[2] && 'G' == buf[3];
+}
+
+/*
+ * Read big-endian uint32 from `buf`.
+ */
+
+int
+read_uint32(char *buf) {
+ return buf[0] << 24
+ | buf[1] << 16
+ | buf[2] << 8
+ | buf[3];
+}
+
+/*
+ * Read little-endian uint16 from `buf`.
+ */
+
+int
+read_uint16(char *buf) {
+ return buf[1] << 8
+ | buf[0];
+}
+
+/*
+ * Output CSS for `width` / `height`.
+ */
+
+void
+output_css_dimensions(int width, int height) {
+ printf("width: %dpx;\n", width);
+ printf("height: %dpx;\n", height);
+}
+
+/*
+ * Read `len` bytes at the given `offset` into `buf`.
+ */
+
+void
+read_range(const char *path, char *buf, int len, int offset) {
+ // open file
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ perror("open()");
+ exit(1);
+ }
+
+ // read bytes
+ if (pread(fd, buf, len, offset) < 0) {
+ perror("read()");
+ exit(1);
+ }
+ close(fd);
+}
+
+/*
+ * Output GIF dimensions for `path`.
+ */
+
+void
+output_gif_dimensions(const char *path) {
+ char buf[4];
+ read_range(path, buf, 4, 6);
+ output_css_dimensions(
+ read_uint16(buf)
+ , read_uint16(buf + 2));
+}
+
+/*
+ * Output PNG dimensions for `path`.
+ */
+
+void
+output_png_dimensions(const char *path) {
+ // read bytes
+ char buf[8];
+ read_range(path, buf, 8, 16);
+ int width = read_uint32(buf)
+ , height = read_uint32(buf+4);
+
+ output_css_dimensions(
+ read_uint32(buf)
+ , read_uint32(buf + 4));
+}
+
+/*
+ * Output JPEG dimensions for `path`.
+ */
+
+void
+output_jpeg_dimensions(const char *path) {
+ fprintf(stderr, "jpeg not yet supported\n");
+ exit(1);
+}
+
+/*
+ * Output usage information.
+ */
+
+void
+usage() {
+ printf(
+ "\n"
+ " Usage: img <path>\n"
+ "\n");
+ exit(1);
+}
+
+/*
+ * Handle args.
+ */
+
+int
+main(int argc, const char **argv){
+ // path required
+ if (argc < 2) usage();
+
+ const char *path = argv[1];
+
+ // open file
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ perror("open()");
+ exit(1);
+ }
+
+ // read bytes
+ char buf[5];
+ if (read(fd, &buf, 5) < 0) {
+ perror("read()");
+ exit(1);
+ }
+ close(fd);
+
+ // sniff bytes
+ if (is_gif(buf)) output_gif_dimensions(path);
+ else if (is_png(buf)) output_png_dimensions(path);
+ else if (is_jpeg(buf)) output_jpeg_dimensions(path);
+
+ return 0;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bd59637

Please sign in to comment.