Skip to content
Snippets Groups Projects
Commit bf7f78b9 authored by Rene's avatar Rene
Browse files

Rectangle searcher II zum laufen gebracht

parent ca214595
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <X11/Xlib.h>
......@@ -26,7 +28,8 @@ extern void throwWarning (char *warningMSG);
#define WARN throwWarning
extern unsigned long extractHexFromString(char *hexString);
extern unsigned long extractIntFromString (char *intString);
extern rectangle searchForColorRect (unsigned long color, XImage *image, unsigned width, unsigned height);
extern rectangle searchForColorRect (uint32_t color, XImage *image, unsigned width, unsigned height);
extern rectangle searchForColorRectII (uint32_t color, XImage *image, unsigned width, unsigned height);
extern unsigned long lookingForColorOnCoordinate (XImage *image, unsigned x, unsigned y);
extern void evaluateArguments (int argc, char **argv);
......@@ -95,8 +98,11 @@ int main (int argc, char **argv)
searchColor = lookingForColorOnCoordinate(image, x, y);
colorParam = True;
}
if(colorParam)
searchForColorRect(searchColor, image, width,height);
if(colorParam) {
//searchForColorRect(searchColor, image, width,height);
searchForColorRectII(searchColor, image, width,height);
}
image->f.destroy_image(image);
XDestroyWindow(display, window);
......@@ -163,7 +169,7 @@ void evaluateArguments (int argc, char **argv)
continue;
}
if(!strcmp(OPTION, "-found") || !strcmp(OPTION,"-f")) {
if(!strcmp(OPTION, "-find") || !strcmp(OPTION,"-f")) {
if (pickColor) {
WARN("Pick excludes -found!");
NEXTOPT;
......@@ -190,16 +196,140 @@ void evaluateArguments (int argc, char **argv)
}
}
/* Pixels are getting printet by ff blue ff00 green ff00000 red */
rectangle searchForColorRect (unsigned long color, XImage *image, unsigned width, unsigned height) {
rectangle searchForColorRectII (uint32_t color, XImage *image, unsigned width, unsigned height) {
rectangle rect;
int count = 0;
uint32_t extractedColor;
int *countX = malloc(sizeof(int) * width);
int *countY = malloc(sizeof(int) * height);
Bool foundColor = False;
for (int i = 0; i < width; i++)
{
countX[i] = 0;
}
for (int i = 0; i < height; i++)
{
countY[i] = 0;
}
for (int pixY = 0; pixY < height; pixY++)
{
for (int pixX = 0; pixX < width; pixX++)
{
extractedColor = (unsigned char)image->data[count];
extractedColor += (unsigned char)image->data[count+1] * 256;
extractedColor += (unsigned char)image->data[count+2] * 256 * 256;
if(color == extractedColor) {
countX[pixX]++;
countY[pixY]++;
foundColor = True;
}
count+=4;
}
}
if(!foundColor) {
ERROR("Farbe wurde nicht gefunden");
}
foundColor = False;
int rowMin = 0;
int rowMax = 0;
int columnMin = 0;
int columnMax = 0;
int rowMiddle;
int columnMiddle;
for (int row = 0; row < height; row++)
{
if (countY[row] > rowMax)
{
rowMax = countY[row];
}
if (countX[row] == 0 || countX[0] < rowMin) {
rowMin = countX[row];
}
}
for (int column = 0; column < width; column++)
{
if (countX[column] > columnMax)
{
columnMax = countX[column];
}
if (countX[column] == 0 || countX[0] < columnMin) {
columnMin = countX[column];
}
}
rowMiddle = (rowMax - rowMin)/2;
columnMiddle = (columnMax - columnMin)/2;
for (int row = 0; row < height; row++)
{
if (countY[row] > rowMiddle) {
if(!foundColor) {
rect.y = row;
foundColor = True;
rect.height = 1;
} else {
rect.height++;
}
}
}
foundColor = False;
for (int column = 0; column < width; column++)
{
if (countX[column] > columnMiddle) {
if(!foundColor) {
rect.x = column;
foundColor = True;
rect.width= 1;
} else {
rect.width++;
}
}
}
if(debug) {
printf("DEBUG: row Max is %d and column max is %d\n", rowMax, columnMax);
printf("DEBUG: row middle is %d and column middle is %d\n", rowMiddle, columnMiddle);
/*
for(int i = 0; i < width ;i++){
if(!(countX[i] == 0))
printf("column: %d contains: %d\n", i, countX[i]);
}
for(int i = 0; i < height ;i++){
if(!(countY[i] == 0))
printf("row: %d contains: %d\n", i, countY[i]);
} */
}
printf("%d %d %d %d\n", rect.x, rect.y, rect.width, rect.height);
return rect;
}
/* Pixels are getting printet by ff blue ff00 green ff0000 red */
rectangle searchForColorRect (uint32_t color, XImage *image, unsigned width, unsigned height) {
rectangle rect;
//int tempX, tempY; //stores the information of the column and row curently beeing evalueted
Bool foundColor = False;
int count = 0;
uint32_t extractedColor;
for (int pixY = 0; pixY < height; pixY++)
{
for (int pixX = 0; pixX < width; pixX++)
{
if(color == image->f.get_pixel(image , pixX, pixY)) {
extractedColor = (unsigned char)image->data[count];
extractedColor += (unsigned char)image->data[count+1] * 256;
extractedColor += (unsigned char)image->data[count+2] * 256 * 256;
if(debug) {
//printf("%06" PRIx32 "\n", extractedColor);
//printf("DEBUG: extracted color is %06" PRIx32 " and given color is %06" PRIx32 "\n", extractedColor, color);
}
if(color == extractedColor) {
if(foundColor) {
if (rect.y == pixY) {
rect.width+=1;
......@@ -223,15 +353,18 @@ rectangle searchForColorRect (unsigned long color, XImage *image, unsigned width
}
}
}
count+=4;
}
}
if (debug)
{
if(foundColor) {
printf("%lx found at x %d y %d \n", color, rect.x, rect.y);
printf("with a width of %d and a height of %d\n", rect.width, rect.height);
printf("DEBUG: %06" PRIx32 " found at x %d y %d \n", color, rect.x, rect.y);
printf("DEBUG: with a width of %d and a height of %d\n", rect.width, rect.height);
}
//printf("DEBUG: extracted color is %06" PRIx32 " and given color is %06" PRIx32 "\n", extractedColor, color);
}
if(foundColor) {
printf("%d %d %d %d\n", rect.x, rect.y, rect.width, rect.height);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment