Ejemplo n.º 1
0
void GSM_ReverseBitmap(GSM_Bitmap *Bitmap)
{
	size_t x, y;

	for (x=0;x<Bitmap->BitmapWidth;x++) {
		for (y=0;y<Bitmap->BitmapHeight;y++) {
			if (GSM_IsPointBitmap(Bitmap,x,y)) {
				GSM_ClearPointBitmap(Bitmap, x, y);
			} else {
				GSM_SetPointBitmap(Bitmap, x, y);
			}
		}
	}
}
Ejemplo n.º 2
0
Archivo: bitmap.c Proyecto: Bakus/gammu
int BitmapFromPython(PyObject * dict, GSM_Bitmap * entry)
{
	char *s;
	int i, j;
	int w, h, cols, chars;
	char c, black = 0, transp = 0;
	char buffer[1000];
	int x, y;
	PyObject *o;
	PyObject *item;
	int len;

	if (!PyDict_Check(dict)) {
		PyErr_Format(PyExc_ValueError, "Bitmap is not a dictionary");
		return 0;
	}

	memset(entry, 0, sizeof(GSM_Bitmap));

	s = GetCharFromDict(dict, "Type");
	if (s == NULL)
		return 0;
	entry->Type = StringToBitmapType(s);
	if (entry->Type == 0)
		return 0;

	s = GetCharFromDict(dict, "NetworkCode");
	if (s == NULL) {
		PyErr_Clear();
		entry->NetworkCode[0] = 0;
	} else {
		mystrncpy(entry->NetworkCode, s, 6);
	}

	i = GetIntFromDict(dict, "Location");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->Location = i;
	}

	i = GetIntFromDict(dict, "BitmapEnabled");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->BitmapEnabled = i;
	}
	i = GetIntFromDict(dict, "DefaultName");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->DefaultName = i;
	}
	i = GetIntFromDict(dict, "DefaultBitmap");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->DefaultBitmap = i;
	}
	i = GetIntFromDict(dict, "DefaultRingtone");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->DefaultRingtone = i;
	}
	i = GetIntFromDict(dict, "RingtoneID");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->RingtoneID = i;
	}
	i = GetIntFromDict(dict, "ID");
	if (i == INT_INVALID) {
		PyErr_Clear();
	} else {
		entry->ID = i;
	}

	if (!CopyStringFromDict
	    (dict, "Text", GSM_BITMAP_TEXT_LENGTH, entry->Text)) {
		PyErr_Clear();
		entry->Text[0] = 0;
		entry->Text[1] = 0;
	}

	if (!CopyStringFromDict
	    (dict, "Sender", GSM_MAX_NUMBER_LENGTH, entry->Sender)) {
		PyErr_Clear();
		entry->Text[0] = 0;
		entry->Text[1] = 0;
	}

	o = PyDict_GetItemString(dict, "XPM");
	if (o == NULL) {
		PyErr_Format(PyExc_ValueError, "Could not get XPM for bitmap!");
		return 0;
	}

	if (!PyList_Check(o)) {
		PyErr_Format(PyExc_ValueError, "XPM isn't list!");
		return 0;
	}

	len = PyList_Size(o);

	if (len < 3) {
		PyErr_Format(PyExc_ValueError, "XPM list too small!");
		return 0;
	}
#define GetString(s, x) \
    item = PyList_GetItem(o, x);\
    if (!PyString_Check(item)) {\
        PyErr_Format(PyExc_ValueError, "XPM contains something different than string!");\
        return 0;\
    }\
\
    s = PyString_AsString(item);\
    if (s == NULL) {\
        PyErr_Format(PyExc_ValueError, "XPM contains something different than string!");\
        return 0;\
    }

	GetString(s, 0);

	if (sscanf(s, "%d %d %d %d", &w, &h, &cols, &chars) != 4) {
		PyErr_Format(PyExc_ValueError, "Bad first XPM row");
		return 0;
	}

	if (chars != 1 || cols != 2) {
		PyErr_Format(PyExc_ValueError,
			     "Only two color XPM with one char per pixel supported");
		return 0;
	}

	if (w > 255 || h > 255 || w < 0 || h < 0 || w * h / 8 > GSM_BITMAP_SIZE) {
		PyErr_Format(PyExc_ValueError, "Bad size of bitmap");
		return 0;
	}

	entry->BitmapWidth = w;
	entry->BitmapHeight = h;

	for (i = 1; i < 3; i++) {
		GetString(s, i);

		if (sscanf(s, "%c c %999s", &c, buffer) != 2) {
			PyErr_Format(PyExc_ValueError,
				     "Can not parse XPM line: '%s'", s);
			return 0;
		}

		j = 0;
		while (buffer[j] != 0) {
			buffer[j] = tolower((int)buffer[j]);
			j++;
		}

		if (strcmp(buffer, "none") == 0 ||
		    strcmp(buffer, "#fff") == 0 ||
		    strcmp(buffer, "#ffffff") == 0 ||
		    strcmp(buffer, "white") == 0)
			transp = c;
		else if (strcmp(buffer, "#000") == 0 ||
			 strcmp(buffer, "#000000") == 0 ||
			 strcmp(buffer, "black") == 0)
			black = c;
		else {
			PyErr_Format(PyExc_ValueError,
				     "Only black and write are supported (guven was %s)",
				     buffer);
			return 0;
		}
	}
	if (black == 0 || transp == 0) {
		PyErr_Format(PyExc_ValueError,
			     "At least one XPM color was not found");
		return 0;
	}
	for (y = 0; y < h; y++) {
		GetString(s, y + 3);
		if ((ssize_t) strlen(s) != w) {
			PyErr_Format(PyExc_ValueError,
				     "XPM line has bad length: '%s'", s);
			return 0;
		}
		for (x = 0; x < w; x++) {
			if (s[x] == black)
				GSM_SetPointBitmap(entry, x, y);
			else if (s[x] == transp)
				GSM_ClearPointBitmap(entry, x, y);
			else {
				PyErr_Format(PyExc_ValueError,
					     "Bad character in XPM data: '%c'",
					     s[x]);
				return 0;
			}
		}
	}

	return 1;
}