Browse Source

* Preliminary support for a string to color routine (rgb(), argb() frgb(), fargb()) as well as named colors

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2810 92316355-f0b4-4df1-b90c-862c8a59935f
remotes/tiles
jylam 16 years ago
parent
commit
3553851ad5
6 changed files with 183 additions and 2 deletions
  1. +4
    -1
      examples/Makefile.am
  2. +20
    -0
      examples/colorstring.c
  3. +1
    -0
      pipi/Makefile.am
  4. +151
    -0
      pipi/colorstring.c
  5. +1
    -0
      pipi/paint/line.c
  6. +6
    -1
      pipi/pipi.h

+ 4
- 1
examples/Makefile.am View File

@@ -2,7 +2,7 @@


AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/pipi AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/pipi


bin_PROGRAMS = edd img2rubik sharpen floodfill line bezier histogram
bin_PROGRAMS = edd img2rubik sharpen floodfill line bezier histogram colorstring


edd_SOURCES = edd.c edd_SOURCES = edd.c
edd_LDADD = ../pipi/libpipi.la edd_LDADD = ../pipi/libpipi.la
@@ -24,3 +24,6 @@ bezier_LDADD = ../pipi/libpipi.la


histogram_SOURCES = histogram.c histogram_SOURCES = histogram.c
histogram_LDADD = ../pipi/libpipi.la histogram_LDADD = ../pipi/libpipi.la

colorstring_SOURCES = colorstring.c
colorstring_LDADD = ../pipi/libpipi.la

+ 20
- 0
examples/colorstring.c View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include <pipi.h>

int main(int argc, char *argv[])
{
if(argc!=2)
{
fprintf(stderr, "Need one argument (only)\n");
return -1;
}

pipi_pixel_t *color = pipi_get_color_from_string(argv[1]);
printf("Color : %f %f %f %f\n",
color->pixel_float.r,
color->pixel_float.g,
color->pixel_float.b,
color->pixel_float.a);

return 0;
}

+ 1
- 0
pipi/Makefile.am View File

@@ -21,6 +21,7 @@ libpipi_la_SOURCES = \
pixels.c \ pixels.c \
codec.c \ codec.c \
stock.c \ stock.c \
colorstring.c \
resize.c \ resize.c \
dither.c \ dither.c \
measure.c \ measure.c \


+ 151
- 0
pipi/colorstring.c View File

@@ -0,0 +1,151 @@
/*
* libpipi Proper image processing implementation library
* Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
* 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
* All Rights Reserved
*
* $Id$
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. 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.
*/

/*
* color.c: color parsing functions
*/

#include "config.h"
#include "common.h"

/*
* libpipi Proper image processing implementation library
* Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
* 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
* All Rights Reserved
*
* $Id$
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. 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.
*/

/*
* colorstring.c: color from string functions
*/

#include "config.h"
#include "common.h"

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

#include "pipi.h"
#include "pipi_internals.h"


struct color_table
{
char name[255];
float a,r,g,b;
};

struct color_table color_table[] =
{
{.name = "black" , 1, 0, 0, 0},
{.name = "white" , 1, 1, 1, 1},
{.name = "red" , 1, 1, 0, 0},
{.name = "green" , 1, 0, 1, 0},
{.name = "blue" , 1, 0, 0, 1},
{.name = "yellow" , 1, 1, 1, 0},
{.name = "cyan" , 1, 0, 1, 1},
{.name = "magenta", 1, 1, 0, 1},
{.name = "grey" , 1, 0.5, 0.5, 0.5},
{.name = "gray" , 1, 0.5, 0.5, 0.5},
{.name = "grey50" , 1, 0.5, 0.5, 0.5},
{.name = "gray50" , 1, 0.5, 0.5, 0.5},
{.name = "grey25" , 1, 0.25, 0.25, 0.25},
{.name = "gray25" , 1, 0.25, 0.25, 0.25},
};



pipi_pixel_t *pipi_get_color_from_string(const char* s)
{
if(!s) return NULL;


pipi_pixel_t *color = malloc(sizeof(pipi_pixel_t));


if(s[0] == '#')
{
uint32_t c = 0;
sscanf(s, "%x", &c);

color->pixel_float.a = ((c&0xFF000000)>>24) / 255.0f;
color->pixel_float.r = ((c&0x00FF0000)>>16) / 255.0f;
color->pixel_float.g = ((c&0x0000FF00)>>8) / 255.0f;
color->pixel_float.b = ((c&0x000000FF)>>0) / 255.0f;
}
else if(!strncmp(s, "rgb(", 4))
{
uint32_t r ,g ,b;
sscanf(s, "rgb(%u,%u,%u)", &r, &g, &b);
color->pixel_float.r = r / 255.0f;
color->pixel_float.g = g / 255.0f;
color->pixel_float.b = b / 255.0f;
}
else if(!strncmp(s, "frgb(", 5))
{
float r ,g ,b;
sscanf(s, "frgb(%f,%f,%f)", &r, &g, &b);
color->pixel_float.r = r;
color->pixel_float.g = g;
color->pixel_float.b = b;
}
else if(!strncmp(s, "argb(", 4))
{
uint32_t a, r ,g ,b;
sscanf(s, "argb(%u,%u,%u,%u)", &a, &r, &g, &b);
color->pixel_float.a = a / 255.0f;
color->pixel_float.r = r / 255.0f;
color->pixel_float.g = g / 255.0f;
color->pixel_float.b = b / 255.0f;
}
else if(!strncmp(s, "fargb(", 5))
{
float a, r ,g ,b;
sscanf(s, "fargb(%f, %f,%f,%f)", &a, &r, &g, &b);
color->pixel_float.a = a;
color->pixel_float.r = r;
color->pixel_float.g = g;
color->pixel_float.b = b;
}
else
{
unsigned int i;
for(i=0; i<sizeof(color_table); i++)
{
if(!strcasecmp(color_table[i].name,s))
{
color->pixel_float.a = color_table[i].a;
color->pixel_float.r = color_table[i].r;
color->pixel_float.g = color_table[i].g;
color->pixel_float.b = color_table[i].b;
break;
}
}


}
return color;
}

+ 1
- 0
pipi/paint/line.c View File

@@ -73,6 +73,7 @@ int pipi_draw_line(pipi_image_t *img , int x1, int y1, int x2, int y2, uint32_t
s.x2 = x2; s.x2 = x2;
s.y2 = y2; s.y2 = y2;



/* No Transparency routine for u32 yet, fallback to float version */ /* No Transparency routine for u32 yet, fallback to float version */
if(img->last_modified == PIPI_PIXELS_RGBA_C) if(img->last_modified == PIPI_PIXELS_RGBA_C)
{ {


+ 6
- 1
pipi/pipi.h View File

@@ -1,6 +1,7 @@
/* /*
* libpipi Proper image processing implementation library * libpipi Proper image processing implementation library
* Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
* 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
* All Rights Reserved * All Rights Reserved
* *
* $Id$ * $Id$
@@ -21,6 +22,7 @@


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


#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@@ -78,8 +80,8 @@ typedef struct
{ {
union union
{ {
struct pixel_u32 pixel_u32;
struct pixel_float pixel_float; struct pixel_float pixel_float;
struct pixel_u32 pixel_u32;
}; };
} }
pipi_pixel_t; pipi_pixel_t;
@@ -102,6 +104,9 @@ typedef struct pipi_context pipi_context_t;
/* pipi_histogram_t: the histogram type */ /* pipi_histogram_t: the histogram type */
typedef struct pipi_histogram pipi_histogram_t; typedef struct pipi_histogram pipi_histogram_t;


extern pipi_pixel_t *pipi_get_color_from_string(const char* s);


extern pipi_context_t *pipi_create_context(void); extern pipi_context_t *pipi_create_context(void);
extern void pipi_destroy_context(pipi_context_t *); extern void pipi_destroy_context(pipi_context_t *);
extern int pipi_command(pipi_context_t *, char const *, ...); extern int pipi_command(pipi_context_t *, char const *, ...);


Loading…
Cancel
Save