23 #include <glib/gstdio.h>
33 #define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x80000000; else (c) >>= 1;
55 static int load_image(
char *filename, guint8 *data,
int *len, guint32 *csum) {
65 if ((cp = strchr(filename,
'@')) != NULL) {
67 int offset, last = -1;
73 offset = atoi(cp + 1);
77 "Corrupt filename - has '@' but no ':' ?(%s)", filename);
81 length = atoi(lp + 1);
85 if (!strcmp(
fd_cache[i].name, filename)) {
88 if (last == -1 &&
fd_cache[i].fd == -1) {
96 "fd_cache filled up? unable to load matching cache entry");
101 if ((
fd_cache[last].fd = open(filename, O_RDONLY | O_BINARY)) == -1)
103 if ((
fd_cache[last].fd = open(filename, O_RDONLY)) == -1)
106 LOG(
LOG_WARNING,
"common::load_image",
"unable to load listed cache file %s",
111 strcpy(
fd_cache[last].name, filename);
114 lseek(
fd_cache[i].fd, offset, SEEK_SET);
116 *len = read(
fd_cache[i].fd, data, length);
118 *len = read(
fd_cache[i].fd, data, 65535);
124 if ((fd = open(filename, O_RDONLY | O_BINARY)) == -1) {
127 length = lseek(fd, 0, SEEK_END);
128 lseek(fd, 0, SEEK_SET);
129 *len = read(fd, data, length);
131 if ((fd = open(filename, O_RDONLY)) == -1) {
134 *len = read(fd, data, 65535);
146 for (i = 0; i < *len; i++) {
167 #define IMAGE_HASH 8192
193 for (p = str; *p !=
'\0' && *p !=
'.'; p++) {
201 return hash % tablesize;
217 if (!strcmp(
image_cache[newhash].image_name, str)) {
224 }
while (newhash != hash);
230 "Hash table is full, increase IMAGE_CACHE size");
242 if (hash_entry == -1) {
244 "Unable to find cache entry for %s, %s", imagename, ce->
filename);
254 while (last->
next && last->
next != ce) {
259 "Unable to find cache entry for %s, %s", imagename, ce->
filename);
278 if (hash_entry == -1) {
303 strcmp(
image_cache[newhash].image_name, imagename)) {
309 if (newhash == hash) {
311 "Hash table is full, increase IMAGE_CACHE size");
344 if (line[0] ==
'#') {
352 "Did not parse line %s properly?", line);
375 snprintf(bmaps,
sizeof(bmaps),
"%s/bmaps.client", CF_DATADIR);
376 if ((fp = fopen(bmaps,
"r")) != NULL) {
377 while (fgets(inbuf,
MAX_BUF - 1, fp) != NULL) {
382 snprintf(inbuf,
sizeof(inbuf),
383 "Unable to open %s. You may wish to download and install the image file to improve performance.\n",
388 snprintf(bmaps,
sizeof(bmaps),
"%s/image-cache/bmaps.client",
cache_dir);
389 if ((fp = fopen(bmaps,
"r")) != NULL) {
390 while (fgets(inbuf,
MAX_BUF - 1, fp) != NULL) {
433 guint8 data[65536], *png_tmp;
439 fprintf(stderr,
"finish_face_cmd, pnum=%d, checksum=%d, face=%s\n",
469 "file %s listed in cache file, but unable to load",
filename);
477 if (!(png_tmp =
png_to_data(data, len, &nx, &ny))) {
480 "Got error on png_to_data, image=%s", face);
496 "Got error on create_and_rescale_image_from_data, file=%s",
filename);
537 "Received a 'face' command when we are not caching");
543 face = (
char *)data + 7;
559 if (len < 9 || (len - 9) != plen) {
560 LOG(
LOG_WARNING,
"common::Image2Cmd",
"Lengths don't compare (%d,%d)",
570 static void cache_newpng(
int face, guint8 *buf,
int buflen,
int setnum,
578 "Caching images, but name for %ld not set", face);
584 if (g_access(
filename, R_OK | W_OK | X_OK) == -1) {
590 if (access(
filename, R_OK | W_OK | X_OK) == -1) {
604 snprintf(basename,
sizeof(basename),
"%s.%s",
facetoname[face],
618 }
while (g_access(
filename, F_OK) == -0);
621 if ((tmpfile = fopen(
filename,
"wb")) == NULL)
623 if ((tmpfile = fopen(
filename,
"w")) == NULL)
626 LOG(
LOG_WARNING,
"common::display_newpng",
"Can not open %s for writing",
631 fwrite(buf, buflen, 1, tmpfile);
634 for (i = 0; (int)i < buflen; i++) {
651 if ((tmpfile = fopen(
filename,
"a")) == NULL) {
652 LOG(
LOG_WARNING,
"common::display_newpng",
"Can not open %s for appending",
655 fprintf(tmpfile,
"%s %u %c%c/%s.%d\n",
680 LOG(
LOG_ERROR,
"display_newpng",
"error in PNG data; discarding");
686 "create_and_rescale_image_from_data failed for face %ld", face);
707 char *cp, *lp, *cps[7], buf[
MAX_BUF];
708 int onset = 0, badline = 0, i;
713 cp = strchr(lp,
'\n');
714 if (!cp || (cp - lp) > len) {
720 cp = strchr(lp,
'\n');
721 if (!cp || (cp - lp) > len) {
728 cp = strchr(lp,
'\n');
729 while (cp && (cp - lp) <= len) {
735 if (!(cps[0] = strtok(lp,
":"))) {
738 for (i = 1; i < 7; i++) {
739 if (!(cps[i] = strtok(NULL,
":"))) {
744 LOG(
LOG_WARNING,
"common::get_image_info",
"bad data, ignoring line:/%s/", lp);
746 onset = atoi(cps[0]);
748 LOG(
LOG_WARNING,
"common::get_image_info",
"setnum is too high: %d > %d",
759 cp = strchr(lp,
'\n');
781 snprintf(buf,
sizeof(buf),
"Unable to find match for faceset %s on the server",
803 int stop, imagenum, slen, faceset;
807 cp = strchr((
char *)data,
' ');
808 if (!cp || (cp - data) > len) {
812 while (isspace(*cp)) {
816 cp = strchr(lp,
' ');
817 if (!cp || (cp - data) > len) {
833 while ((cp - data) < len) {
851 if (imagenum > stop) {
853 "Received an image beyond our range? %d > %d", imagenum, stop);