Browse Source

* Added img2irc program. I know it does not have "cucul" or "caca" in the

name, but as I said, I do what I want. http://zoy.org/~sam/pb7th.png
tags/v0.99.beta14
Sam Hocevar sam 18 years ago
parent
commit
1084035890
4 changed files with 405 additions and 9 deletions
  1. +17
    -9
      src/Makefile.am
  2. +294
    -0
      src/common-image.c
  3. +25
    -0
      src/common-image.h
  4. +69
    -0
      src/img2irc.c

+ 17
- 9
src/Makefile.am View File

@@ -5,7 +5,7 @@ pkgdata_DATA = caca.txt
EXTRA_DIST = caca.txt
AM_CPPFLAGS = -I$(top_srcdir)/cucul -I$(top_srcdir)/caca -DLIBCACA=1 -DX_DISPLAY_MISSING=1

bin_PROGRAMS = cacafire cacaball cacaplas cacaserver cacamoir cacaplay
bin_PROGRAMS = cacafire cacaball cacaplas cacaserver cacamoir cacaplay img2irc

cacafire_SOURCES = aafire.c
cacafire_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@
@@ -19,15 +19,10 @@ cacaplas_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@ @MATH_LIBS@
cacamoir_SOURCES = cacamoir.c
cacamoir_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@ @MATH_LIBS@

cacaview_SOURCES = cacaview.c
cacaview_SOURCES = cacaview.c common-image.c common-image.h
cacaview_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@
if USE_IMLIB2
cacaview_CFLAGS = `imlib2-config --cflags`
cacaview_LDFLAGS = `imlib2-config --libs`
else
cacaview_CFLAGS =
cacaview_LDFLAGS =
endif
cacaview_CFLAGS = $(img_cflags)
cacaview_LDFLAGS = $(img_ldflags)

cacaplay_SOURCES = cacaplay.c
cacaplay_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@
@@ -35,3 +30,16 @@ cacaplay_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@
cacaserver_SOURCES = cacaserver.c
cacaserver_LDADD = ../caca/libcaca.la ../cucul/libcucul.la @CACA_LIBS@

img2irc_SOURCES = img2irc.c common-image.c common-image.h
img2irc_LDADD = ../cucul/libcucul.la @CACA_LIBS@
img2irc_CFLAGS = $(img_cflags)
img2irc_LDFLAGS = $(img_ldflags)

if USE_IMLIB2
img_cflags = `imlib2-config --cflags`
img_ldflags = `imlib2-config --libs`
else
img_cflags =
img_ldflags =
endif


+ 294
- 0
src/common-image.c View File

@@ -0,0 +1,294 @@
/*
* Imaging tools for cacaview and img2irc
* Copyright (c) 2003, 2004, 2005, 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#if defined(HAVE_IMLIB2_H)
# include <Imlib2.h>
#else
# include <stdio.h>
#endif

#include "cucul.h"
#include "common-image.h"

#if !defined(HAVE_IMLIB2_H)
static int freadint(FILE *);
static int freadshort(FILE *);
static int freadchar(FILE *);
#endif

struct image * load_image(char const * name)
{
struct image * im = malloc(sizeof(struct image));
unsigned int depth, bpp, rmask, gmask, bmask, amask;

#if defined(HAVE_IMLIB2_H)
Imlib_Image image;

/* Load the new image */
image = imlib_load_image(name);

if(!image)
{
free(im);
return NULL;
}

imlib_context_set_image(image);
im->pixels = (char *)imlib_image_get_data_for_reading_only();
im->w = imlib_image_get_width();
im->h = imlib_image_get_height();
rmask = 0x00ff0000;
gmask = 0x0000ff00;
bmask = 0x000000ff;
amask = 0xff000000;
bpp = 32;
depth = 4;

/* Create the libcucul dither */
im->dither = cucul_create_dither(bpp, im->w, im->h, depth * im->w,
rmask, gmask, bmask, amask);
if(!im->dither)
{
imlib_free_image();
free(im);
return NULL;
}

im->priv = (void *)image;

#else
/* Try to load a BMP file */
unsigned int red[256], green[256], blue[256], alpha[256];
unsigned int i, colors, offset, tmp, planes;
FILE *fp;

fp = fopen(name, "rb");
if(!fp)
{
free(im);
return NULL;
}

if(freadshort(fp) != 0x4d42)
{
fclose(fp);
free(im);
return NULL;
}

freadint(fp); /* size */
freadshort(fp); /* reserved 1 */
freadshort(fp); /* reserved 2 */

offset = freadint(fp);

tmp = freadint(fp); /* header size */
if(tmp == 40)
{
im->w = freadint(fp);
im->h = freadint(fp);
planes = freadshort(fp);
bpp = freadshort(fp);

tmp = freadint(fp); /* compression */
if(tmp != 0)
{
fclose(fp);
free(im);
return NULL;
}

freadint(fp); /* sizeimage */
freadint(fp); /* xpelspermeter */
freadint(fp); /* ypelspermeter */
freadint(fp); /* biclrused */
freadint(fp); /* biclrimportantn */

colors = (offset - 54) / 4;
for(i = 0; i < colors && i < 256; i++)
{
blue[i] = freadchar(fp) * 16;
green[i] = freadchar(fp) * 16;
red[i] = freadchar(fp) * 16;
alpha[i] = 0;
freadchar(fp);
}
}
else if(tmp == 12)
{
im->w = freadint(fp);
im->h = freadint(fp);
planes = freadshort(fp);
bpp = freadshort(fp);

colors = (offset - 26) / 3;
for(i = 0; i < colors && i < 256; i++)
{
blue[i] = freadchar(fp);
green[i] = freadchar(fp);
red[i] = freadchar(fp);
alpha[i] = 0;
}
}
else
{
fclose(fp);
free(im);
return NULL;
}

/* Fill the rest of the palette */
for(i = colors; i < 256; i++)
blue[i] = green[i] = red[i] = alpha[i] = 0;

depth = (bpp + 7) / 8;

/* Sanity check */
if(!im->w || im->w > 0x10000 || !im->h || im->h > 0x10000 || planes != 1)
{
fclose(fp);
free(im);
return NULL;
}

/* Allocate the pixel buffer */
im->pixels = malloc(im->w * im->h * depth);
if(!im->pixels)
{
fclose(fp);
free(im);
return NULL;
}

memset(im->pixels, 0, im->w * im->h * depth);

/* Read the bitmap data */
for(i = im->h; i--; )
{
unsigned int j, k, bits = 0;

switch(bpp)
{
case 1:
for(j = 0; j < im->w; j++)
{
k = j % 32;
if(k == 0)
bits = freadint(fp);
im->pixels[im->w * i * depth + j] =
(bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1;
}
break;
case 4:
for(j = 0; j < im->w; j++)
{
k = j % 8;
if(k == 0)
bits = freadint(fp);
im->pixels[im->w * i * depth + j] =
(bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf;
}
break;
default:
/* Works for 8bpp, but also for 16, 24 etc. */
fread(im->pixels + im->w * i * depth, im->w * depth, 1, fp);
/* Pad reads to 4 bytes */
tmp = (im->w * depth) % 4;
tmp = (4 - tmp) % 4;
while(tmp--)
freadchar(fp);
break;
}
}

switch(depth)
{
case 3:
rmask = 0xff0000;
gmask = 0x00ff00;
bmask = 0x0000ff;
amask = 0x000000;
break;
case 2: /* XXX: those are the 16 bits values */
rmask = 0x7c00;
gmask = 0x03e0;
bmask = 0x001f;
amask = 0x0000;
break;
case 1:
default:
bpp = 8;
rmask = gmask = bmask = amask = 0;
break;
}

fclose(fp);

/* Create the libcucul dither */
im->dither = cucul_create_dither(bpp, im->w, im->h, depth * im->w,
rmask, gmask, bmask, amask);
if(!im->dither)
{
free(im->pixels);
free(im);
return NULL;
}

if(bpp == 8)
cucul_set_dither_palette(im->dither, red, green, blue, alpha);
#endif

return im;
}

void unload_image(struct image * im)
{
#if defined(HAVE_IMLIB2_H)
/* Imlib_Image image = (Imlib_Image)im->priv; */
imlib_free_image();
#else
free(im->pixels);
#endif
cucul_free_dither(im->dither);
}

#if !defined(HAVE_IMLIB2_H)
static int freadint(FILE *fp)
{
unsigned char buffer[4];
fread(buffer, 4, 1, fp);
return ((int)buffer[3] << 24) | ((int)buffer[2] << 16)
| ((int)buffer[1] << 8) | ((int)buffer[0]);
}

static int freadshort(FILE *fp)
{
unsigned char buffer[2];
fread(buffer, 2, 1, fp);
return ((int)buffer[1] << 8) | ((int)buffer[0]);
}

static int freadchar(FILE *fp)
{
unsigned char buffer;
fread(&buffer, 1, 1, fp);
return (int)buffer;
}
#endif


+ 25
- 0
src/common-image.h View File

@@ -0,0 +1,25 @@
/*
* Imaging tools for cacaview and img2irc
* Copyright (c) 2003, 2004, 2005, 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

struct image
{
char *pixels;
unsigned int w, h;
struct cucul_dither *dither;
void *priv;
};

/* Local functions */
extern struct image * load_image(char const *);
extern void unload_image(struct image *);


+ 69
- 0
src/img2irc.c View File

@@ -0,0 +1,69 @@
/*
* pic2irc image to IRC converter
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "cucul.h"
#include "common-image.h"

int main(int argc, char **argv)
{
/* libcucul context */
cucul_t *qq;
struct cucul_export *export;
struct image *i;
int cols = 56, lines;

if(argc != 2)
{
fprintf(stderr, "%s: wrong argument count\n", argv[0]);
return 1;
}

qq = cucul_create(0, 0);
if(!qq)
{
fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
return 1;
}

i = load_image(argv[1]);
if(!i)
{
fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[1]);
cucul_free(qq);
return 1;
}

/* Assume a 6×10 font */
lines = cols * i->h * 6 / i->w / 10;

cucul_set_size(qq, cols, lines);
cucul_clear(qq);
cucul_dither_bitmap(qq, 0, 0, cols - 1, lines - 1, i->dither, i->pixels);

unload_image(i);

export = cucul_create_export(qq, "irc");
fwrite(export->buffer, export->size, 1, stdout);
cucul_free_export(export);

cucul_free(qq);

return 0;
}


Loading…
Cancel
Save