- mm_log((1,"i_writeppm(im* 0x%x,fd %d)\n",im,fd));
- if (im->channels!=3) {
- mm_log((1,"i_writeppm: ppm is 3 channel only (current image is %d)\n",im->channels));
- return(0);
+ mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
+ i_clear_error();
+
+ /* Add code to get the filename info from the iolayer */
+ /* Also add code to check for mmapped code */
+
+ io_glue_commit_types(ig);
+
+ if (im->channels == 3) {
+ sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
+ if (ig->writecb(ig,header,strlen(header))<0) {
+ i_push_error(errno, "could not write ppm header");
+ mm_log((1,"i_writeppm: unable to write ppm header.\n"));
+ return(0);
+ }
+
+ if (!im->virtual && im->bits == i_8_bits && im->type == i_direct_type) {
+ rc = ig->writecb(ig,im->idata,im->bytes);
+ }
+ else {
+ unsigned char *data = mymalloc(3 * im->xsize);
+ if (data != NULL) {
+ int y = 0;
+ static int rgb_chan[3] = { 0, 1, 2 };
+
+ rc = 0;
+ while (y < im->ysize && rc >= 0) {
+ i_gsamp(im, 0, im->xsize, y, data, rgb_chan, 3);
+ rc = ig->writecb(ig, data, im->xsize * 3);
+ ++y;
+ }
+ myfree(data);
+ }
+ else {
+ i_push_error(0, "Out of memory");
+ return 0;
+ }
+ }
+ if (rc<0) {
+ i_push_error(errno, "could not write ppm data");
+ mm_log((1,"i_writeppm: unable to write ppm data.\n"));
+ return(0);
+ }