15 writedata(Biobuf *fd, Image *image, Memimage *memimage)
19 int i, x, y, ndata, depth, col, pix, xmask, pmask;
25 depth = memimage->depth;
26 chan = memimage->chan;
34 * Read image data into memory
35 * potentially one extra byte on each end of each scan line
37 ndata = Dy(r)*(2+Dx(r)*depth/8);
40 return "WritePPM: malloc failed";
42 ndata = unloadmemimage(memimage, r, data, ndata);
44 ndata = unloadimage(image, r, data, ndata);
48 return "WritePPM: malloc failed";
49 snprint(err, ERRMAX, "WriteGIF: %r");
54 /* Encode and emit the data */
61 xmask = 7>>drawlog2[depth];
62 for(y=r.min.y; y<r.max.y; y++){
63 i = (y-r.min.y)*bytesperline(r, depth);
64 for(x=r.min.x; x<r.max.x; x++){
65 pix = (data[i]>>depth*((xmask-x)&xmask))&pmask;
66 if(((x+1)&xmask) == 0)
68 col += Bprint(fd, "%d ", pix);
69 if(col >= MAXLINE-(2+1)){
73 col += Bprint(fd, " ");
78 for(i=0; i<ndata; i++){
79 col += Bprint(fd, "%d ", data[i]);
80 if(col >= MAXLINE-(4+1)){
84 col += Bprint(fd, " ");
88 for(i=0; i<ndata; i+=3){
89 col += Bprint(fd, "%d %d %d", data[i+2], data[i+1], data[i]);
90 if(col >= MAXLINE-(4+4+4+1)){
94 col += Bprint(fd, " ");
98 return "WritePPM: can't handle channel type";
106 writeppm0(Biobuf *fd, Image *image, Memimage *memimage, Rectangle r, int chan, char *comment)
123 return "WritePPM: can't handle channel type";
126 if(comment!=nil && comment[0]!='\0'){
127 Bprint(fd, "# %s", comment);
128 if(comment[strlen(comment)-1] != '\n')
131 Bprint(fd, "%d %d\n", Dx(r), Dy(r));
133 /* maximum pixel value */
136 Bprint(fd, "%d\n", 3);
139 Bprint(fd, "%d\n", 15);
143 Bprint(fd, "%d\n", 255);
147 err = writedata(fd, image, memimage);
155 writeppm(Biobuf *fd, Image *image, char *comment)
157 return writeppm0(fd, image, nil, image->r, image->chan, comment);
161 memwriteppm(Biobuf *fd, Memimage *memimage, char *comment)
163 return writeppm0(fd, nil, memimage, memimage->r, memimage->chan, comment);