ref: d5721fe394af8f5fd17c07946b237ee12b69faf7
parent: 6b18bfa39eb89fa6315ed478ce80c196303c0334
author: qwx <[email protected]>
date: Mon Nov 6 03:08:31 EST 2023
libdraw: remove unmodified readimage, add missing(??) readtheme
--- a/sys/src/libdraw/readimage.c
+++ /dev/null
@@ -1,136 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include <draw.h>
-
-Image*
-readimage(Display *d, int fd, int dolock)
-{
- char hdr[5*12+1];
- int dy;
- int new;
- uint l, n;
- int m, j, chunk;
- int miny, maxy;
- Rectangle r;
- int ldepth;
- ulong chan;
- uchar *tmp;
- Image *i;
-
- if(readn(fd, hdr, 11) != 11)
- return nil;
- if(memcmp(hdr, "compressed\n", 11) == 0){
- if(i = creadimage(d, fd, dolock))
- goto Done;
- return nil;
- }
-
- if(readn(fd, hdr+11, 5*12-11) != 5*12-11)
- return nil;
- if(d != nil)
- chunk = d->bufsize - 32; /* a little room for header */
- else {
- chunk = iounit(fd);
- if(chunk <= 0)
- chunk = IOUNIT;
- }
-
- /*
- * distinguish new channel descriptor from old ldepth.
- * channel descriptors have letters as well as numbers,
- * while ldepths are a single digit formatted as %-11d.
- */
- new = 0;
- for(m=0; m<10; m++){
- if(hdr[m] != ' '){
- new = 1;
- break;
- }
- }
- if(hdr[11] != ' '){
- werrstr("readimage: bad format");
- return nil;
- }
- if(new){
- hdr[11] = '\0';
- if((chan = strtochan(hdr)) == 0){
- werrstr("readimage: bad channel string %s", hdr);
- return nil;
- }
- }else{
- ldepth = ((int)hdr[10])-'0';
- if(ldepth<0 || ldepth>3){
- werrstr("readimage: bad ldepth %d", ldepth);
- return nil;
- }
- chan = drawld2chan[ldepth];
- }
-
- r.min.x = atoi(hdr+1*12);
- r.min.y = atoi(hdr+2*12);
- r.max.x = atoi(hdr+3*12);
- r.max.y = atoi(hdr+4*12);
- if(r.min.x>r.max.x || r.min.y>r.max.y){
- werrstr("readimage: bad rectangle");
- return nil;
- }
-
- miny = r.min.y;
- maxy = r.max.y;
-
- l = bytesperline(r, chantodepth(chan));
- if(l > chunk)
- chunk = l;
- if(d != nil){
- if(dolock)
- lockdisplay(d);
- i = allocimage(d, r, chan, 0, -1);
- if(dolock)
- unlockdisplay(d);
- if(i == nil)
- return nil;
- }else{
- i = mallocz(sizeof(Image), 1);
- if(i == nil)
- return nil;
- }
- tmp = malloc(chunk);
- if(tmp == nil)
- goto Err;
- while(maxy > miny){
- dy = maxy - miny;
- if(dy*l > chunk)
- dy = chunk/l;
- n = dy*l;
- m = readn(fd, tmp, n);
- if(m != n){
- werrstr("readimage: read count %d not %d: %r", m, n);
- Err:
- if(dolock)
- lockdisplay(d);
- Err1:
- freeimage(i);
- if(dolock)
- unlockdisplay(d);
- free(tmp);
- return nil;
- }
- if(!new) /* an old image: must flip all the bits */
- for(j=0; j<chunk; j++)
- tmp[j] ^= 0xFF;
-
- if(d != nil){
- if(dolock)
- lockdisplay(d);
- if(loadimage(i, Rect(r.min.x, miny, r.max.x, miny+dy), tmp, chunk) <= 0)
- goto Err1;
- if(dolock)
- unlockdisplay(d);
- }
- miny += dy;
- }
- free(tmp);
- Done:
- setmalloctag(i, getcallerpc(&d));
- return i;
-}
--- /dev/null
+++ b/sys/src/libdraw/readtheme.c
@@ -1,0 +1,36 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <bio.h>
+
+int
+readtheme(Theme *col, int ncol, char *file)
+{
+ int i;
+ char *s, *v[3];
+ Biobuf *bf;
+
+ if(col == nil || ncol <= 0){
+ werrstr("no color to hand");
+ return -1;
+ }
+ for(i=0; i<ncol; i++)
+ if(col[i].name == nil){
+ werrstr("invalid color at index %d", i);
+ return -1;
+ }
+ if(file == nil)
+ file = "/dev/theme";
+ if((bf = Bopen(file, OREAD)) == nil)
+ return -1;
+ while((s = Brdline(bf, '\n')) != nil){
+ s[Blinelen(bf)-1] = 0;
+ if(tokenize(s, v, nelem(v)) <= 0)
+ continue;
+ for(i=0; i<ncol; i++)
+ if(strcmp(v[0], col[i].name) == 0)
+ col[i].c = strtoul(v[1], nil, 16)<<8 | 0xff;
+ }
+ Bterm(bf);
+ return 0;
+}