9 /* imported from libdraw/arith.c to permit an extern log2 function */
11 -1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4,
12 -1, -1, -1, -1, -1, -1, -1, 4 /* BUG */, -1, -1, -1, -1, -1, -1, -1, 5
20 writedata(Biobuf *fd, Image *image, Memimage *memimage)
24 int i, x, y, ndata, depth, col, pix, xmask, pmask;
30 depth = memimage->depth;
31 chan = memimage->chan;
39 * Read image data into memory
40 * potentially one extra byte on each end of each scan line
42 ndata = Dy(r)*(2+Dx(r)*depth/8);
45 return "WritePPM: malloc failed";
47 ndata = unloadmemimage(memimage, r, data, ndata);
49 ndata = unloadimage(image, r, data, ndata);
53 return "WritePPM: malloc failed";
54 snprint(err, ERRMAX, "WriteGIF: %r");
59 /* Encode and emit the data */
66 xmask = 7>>log2[depth];
67 for(y=r.min.y; y<r.max.y; y++){
68 i = (y-r.min.y)*bytesperline(r, depth);
69 for(x=r.min.x; x<r.max.x; x++){
70 pix = (data[i]>>depth*((xmask-x)&xmask))&pmask;
71 if(((x+1)&xmask) == 0)
73 col += Bprint(fd, "%d ", pix);
74 if(col >= MAXLINE-(2+1)){
78 col += Bprint(fd, " ");
83 for(i=0; i<ndata; i++){
84 col += Bprint(fd, "%d ", data[i]);
85 if(col >= MAXLINE-(4+1)){
89 col += Bprint(fd, " ");
93 for(i=0; i<ndata; i+=3){
94 col += Bprint(fd, "%d %d %d", data[i+2], data[i+1], data[i]);
95 if(col >= MAXLINE-(4+4+4+1)){
99 col += Bprint(fd, " ");
103 return "WritePPM: can't handle channel type";
111 writeppm0(Biobuf *fd, Image *image, Memimage *memimage, Rectangle r, int chan, char *comment)
128 return "WritePPM: can't handle channel type";
131 if(comment!=nil && comment[0]!='\0'){
132 Bprint(fd, "# %s", comment);
133 if(comment[strlen(comment)-1] != '\n')
136 Bprint(fd, "%d %d\n", Dx(r), Dy(r));
138 /* maximum pixel value */
141 Bprint(fd, "%d\n", 3);
144 Bprint(fd, "%d\n", 15);
148 Bprint(fd, "%d\n", 255);
152 err = writedata(fd, image, memimage);
160 writeppm(Biobuf *fd, Image *image, char *comment)
162 return writeppm0(fd, image, nil, image->r, image->chan, comment);
166 memwriteppm(Biobuf *fd, Memimage *memimage, char *comment)
168 return writeppm0(fd, nil, memimage, memimage->r, memimage->chan, comment);