Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorny committed Mar 25, 2012
0 parents commit bc7b393
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
@@ -0,0 +1,14 @@
*.o
/Makefile
/Makefile.in
/aclocal.m4
/autom4te.cache/
/build-aux/
/config.cache
/config.h
/config.h.in
/config.log
/config.status
/configure
/stamp-h1
/pixels2pgf
23 changes: 23 additions & 0 deletions COPYING
@@ -0,0 +1,23 @@
Copyright (c) 2012, Michał Górny
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
5 changes: 5 additions & 0 deletions Makefile.am
@@ -0,0 +1,5 @@
bin_PROGRAMS = pixels2pgf

pixels2pgf_SOURCES = src/pixels2pgf.c
pixels2pgf_CPPFLAGS = $(SDL_CFLAGS)
pixels2pgf_LDADD = $(SDL_LIBS)
12 changes: 12 additions & 0 deletions configure.ac
@@ -0,0 +1,12 @@
AC_PREREQ([2.60])
AC_INIT([pixels2pgf], [0])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([1.6 foreign dist-bzip2 no-dependencies])

PKG_CHECK_MODULES([SDL], [sdl SDL_image])

AC_PROG_CC

AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
86 changes: 86 additions & 0 deletions src/pixels2pgf.c
@@ -0,0 +1,86 @@
/* pixels2pgf -- pixel-exact image to PGF converter
* (c) 2012 Michał Górny
* Released under the terms of the 2-clause BSD license
*/

#include <stdio.h>

#include <SDL.h>
#include <SDL_image.h>

void output_pgf(unsigned char* pixels, int width, int height, int pitch)
{
int y, x;

for (y = 0; y < height; ++y)
{
for (x = 0; x < width; ++x)
{
unsigned char pixel = pixels[y * pitch + x];

if (!(pixel & 0xBF))
printf("\\filldraw (%d, -%d) rectangle ++(1, -1);\n", x, y);
}
}
}

int process(SDL_Surface* input)
{
SDL_PixelFormat nicepf = {
NULL,
8, 1,
0, 2, 4, 6,
6, 6, 6, 6,
0x03, 0x0C, 0x30, 0xC0,
0xC0, 0
};

SDL_Surface* image;

if (!input)
{
fprintf(stderr, "Failed to load image: %s\n", IMG_GetError());
return 1;
}

/* Convert to a nicer format. */
image = SDL_ConvertSurface(input, &nicepf, SDL_SWSURFACE);
SDL_FreeSurface(input);

if (!image)
{
fprintf(stderr, "Unable to convert image: %s\n",
SDL_GetError());
return 1;
}

output_pgf(image->pixels, image->w, image->h, image->pitch);

SDL_FreeSurface(image);
return 0;
}

int main(int argc, char* argv[])
{
int ret;

if (argc != 2)
{
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
return 1;
}

if (SDL_Init(0))
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
IMG_Init(0);

ret = process(IMG_Load(argv[1]));

IMG_Quit();
SDL_Quit();

return ret;
}

0 comments on commit bc7b393

Please sign in to comment.