#include #include #include #include #include #include //Functions needed by gimp //This one is called mainly when Gimp starts static void query (void); //This one is called when the users clicks on "Colors"->"Read color from sensor..." static void run ( const gchar *name, gint nparams, const GimpParam *param, gint *nreturn_vals, GimpParam **return_vals ); /* * Opens the /dev/ttyACM0 serial port * Pre: the /dev/ttyACM0 port is avaiable */ static int open_port(void); /* * Sends a signal to the Arduino, get the color code and returns it in the GimpRGB format * Pre: the /dev/ttyACM0 port is avaiable and Arduino send a proper hexadecimal color code */ static GimpRGB read_color(void); /* * Converts an exadecimal digit to an integer * Pre: the input char is a valid exadecimal digit */ static int hex2int(char c); GimpPlugInInfo PLUG_IN_INFO = { NULL, NULL, query, run }; MAIN() static void query (void) { static GimpParamDef args[] = { { GIMP_PDB_INT32, "run-mode", "Run mode" }, { GIMP_PDB_IMAGE, "image", "Input image" }, { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" } }; gimp_install_procedure ( "plug-in-real-colors", "Real colors", "Reads a color and changes the palette foreground color to it", "Giulio Martella", "Copyright Giulio Martella", "2015", "_Read color from sensor...", "RGB*, GRAY*", GIMP_PLUGIN, G_N_ELEMENTS (args), 0, args, NULL ); gimp_plugin_menu_register ( "plug-in-real-colors", "/Colors" ); } static void run ( const gchar *name, gint nparams, const GimpParam *param, gint *nreturn_vals, GimpParam **return_vals ) { static GimpParam values[1]; GimpPDBStatusType status = GIMP_PDB_SUCCESS; GimpRunMode run_mode; /* Setting mandatory output values */ *nreturn_vals = 1; *return_vals = values; values[0].type = GIMP_PDB_STATUS; values[0].data.d_status = status; run_mode = param[0].data.d_int32; GimpRGB color = read_color(); gimp_palette_set_foreground(&color); } static int open_port(void) { int fd; /* File descriptor for the port */ struct termios options; fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { /* * Could not open the port. */ perror("open_port: Unable to open /dev/ttyf1 - "); } else fcntl(fd, F_SETFL, 0); tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~CRTSCTS; options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 5; options.c_cflag |= CREAD | CLOCAL; cfmakeraw(&options); tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &options); return (fd); } static GimpRGB read_color(void) { GimpRGB color; int fd = open_port(); int n = 0, i = 0; char buf = '\0'; write(fd, "A", 1); do { n = read( fd, &buf, 1 ); if( i == 0 ) color.r = hex2int(buf) * 16; else if( i == 1 ) color.r += hex2int(buf); else if( i == 2 ) color.g = hex2int(buf) * 16; else if( i == 3 ) color.g += hex2int(buf); else if( i == 4 ) color.b = hex2int(buf) * 16; else if( i == 5 ) color.b += hex2int(buf); i++; } while (buf != '\r' && n > 0); close(fd); color.r /= 255; color.g /= 255; color.b /= 255; color.a = 1.0; return color; } static int hex2int( char c ) { int ret = 0; if( c >= '0' && c <= '9' ) { ret = c - 48; } else if( c >= 'A' && c <= 'F' ) { ret = c - 55; } else if( c >= 'a' && c <= 'f' ) { ret = c - 87; } return ret; }