123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- #include "sys-defines.h"
- #include "extern.h"
- #include "xmi.h"
- #define MAX_PBM_PIXELS_PER_LINE 70
- #define MAX_PGM_PIXELS_PER_LINE 16
- #define MAX_PPM_PIXELS_PER_LINE 5
- static int best_image_type (miPixel **pixmap, int width, int height);
- #define FAST_PRINT(integer_to_print, linebuf, pos) \
- { \
- int k, hundreds, tens, ones; \
- bool force_tens; \
- \
- k = (integer_to_print); \
- hundreds = k / 100; \
- k -= (100 * hundreds); \
- tens = k / 10; \
- ones = k - (10 * tens); \
- \
- force_tens = false; \
- if (hundreds) \
- { \
- linebuf[pos++] = hundreds + '0'; \
- force_tens = true; \
- } \
- if (force_tens || tens) \
- linebuf[pos++] = tens + '0'; \
- linebuf[pos++] = ones + '0'; \
- }
- int
- _pl_n_maybe_output_image (S___(Plotter *_plotter))
- {
-
- if (_plotter->data->page_number == 1)
-
- _pl_n_write_pnm (S___(_plotter));
- return true;
- }
- void
- _pl_n_write_pnm (S___(Plotter *_plotter))
- {
- int type;
- int width, height;
- miPixel **pixmap;
- width = _plotter->b_xn;
- height = _plotter->b_yn;
- pixmap = ((miCanvas *)(_plotter->b_canvas))->drawable->pixmap;
- type = best_image_type (pixmap, width, height);
- switch (type)
- {
- case 0:
- _pl_n_write_pbm (S___(_plotter));
- break;
- case 1:
- _pl_n_write_pgm (S___(_plotter));
- break;
- case 2:
- default:
- _pl_n_write_ppm (S___(_plotter));
- break;
- }
- }
- void
- _pl_n_write_pbm (S___(Plotter *_plotter))
- {
- int i, j;
- bool portable = _plotter->n_portable_output;
- miPixel **pixmap = ((miCanvas *)(_plotter->b_canvas))->drawable->pixmap;
- int width = _plotter->b_xn;
- int height = _plotter->b_yn;
- FILE *fp = _plotter->data->outfp;
- #ifdef LIBPLOTTER
- ostream *stream = _plotter->data->outstream;
- #endif
- #ifdef LIBPLOTTER
- if (fp == NULL && stream == NULL)
- return;
- #else
- if (fp == NULL)
- return;
- #endif
- if (fp)
- {
- if (portable)
- {
- unsigned char linebuf[MAX_PBM_PIXELS_PER_LINE];
- int pos = 0;
- fprintf (fp, "\
- P1\n\
- # CREATOR: GNU libplot drawing library, version %s\n\
- %d %d\n", PL_LIBPLOT_VER_STRING, width, height);
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
- if (pixmap[j][i].u.rgb[0] == 0)
- linebuf[pos++] = '1';
- else
- linebuf[pos++] = '0';
- if (pos >= MAX_PBM_PIXELS_PER_LINE || i == (width - 1))
- {
- fwrite ((void *)linebuf, sizeof(unsigned char), pos, fp);
- putc ('\n', fp);
- pos = 0;
- }
- }
- }
- else
- {
- int bitcount, bytecount;
- unsigned char outbyte, set;
- unsigned char *rowbuf;
-
- fprintf (fp, "\
- P4\n\
- # CREATOR: GNU libplot drawing library, version %s\n\
- %d %d\n", PL_LIBPLOT_VER_STRING, width, height);
-
-
- rowbuf = (unsigned char *)_pl_xmalloc (((width + 7) / 8) * sizeof (unsigned char));
- for (j = 0; j < height; j++)
- {
- bitcount = 0;
- bytecount = 0;
- outbyte = 0;
- for (i = 0; i < width; i++)
- {
- set = (pixmap[j][i].u.rgb[0] == 0 ? 1 : 0);
- outbyte = (outbyte << 1) | set;
- bitcount++;
- if (bitcount == 8)
- {
- rowbuf[bytecount++] = outbyte;
- outbyte = 0;
- bitcount = 0;
- }
- }
- if (bitcount)
- {
- outbyte = (outbyte << (8 - bitcount));
- rowbuf[bytecount++] = outbyte;
- }
-
- fwrite ((void *)rowbuf, sizeof(unsigned char), bytecount, fp);
- }
- free (rowbuf);
- }
- }
- #ifdef LIBPLOTTER
- else if (stream)
- {
- if (portable)
- {
- unsigned char linebuf[MAX_PBM_PIXELS_PER_LINE];
- int pos = 0;
- (*stream) << "\
- P1\n\
- # CREATOR: GNU libplot drawing library, version "
- << PL_LIBPLOT_VER_STRING << '\n'
- << width << ' ' << height << '\n';
-
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
- if (pixmap[j][i].u.rgb[0] == 0)
- linebuf[pos++] = '1';
- else
- linebuf[pos++] = '0';
- if (pos >= MAX_PBM_PIXELS_PER_LINE || i == (width - 1))
- {
- stream->write ((const char *)linebuf, pos);
- stream->put ('\n');
- pos = 0;
- }
- }
- }
- else
- {
- int bitcount, bytecount;
- unsigned char outbyte, set;
- unsigned char *rowbuf;
- (*stream) << "\
- P4\n\
- # CREATOR: GNU libplot drawing library, version "
- << PL_LIBPLOT_VER_STRING << '\n'
- << width << ' ' << height << '\n';
-
-
-
- rowbuf = (unsigned char *)_pl_xmalloc (((width + 7) / 8) * sizeof (unsigned char));
- for (j = 0; j < height; j++)
- {
- bitcount = 0;
- bytecount = 0;
- outbyte = 0;
- for (i = 0; i < width; i++)
- {
- set = (pixmap[j][i].u.rgb[0] == 0 ? 1 : 0);
- outbyte = (outbyte << 1) | set;
- bitcount++;
- if (bitcount == 8)
- {
- rowbuf[bytecount++] = outbyte;
- outbyte = 0;
- bitcount = 0;
- }
- }
- if (bitcount)
- {
- outbyte = (outbyte << (8 - bitcount));
- rowbuf[bytecount++] = outbyte;
- }
-
- stream->write ((const char *)rowbuf, bytecount);
- }
- free (rowbuf);
- }
- }
- #endif
- }
- void
- _pl_n_write_pgm (S___(Plotter *_plotter))
- {
- int i, j;
- bool portable = _plotter->n_portable_output;
- miPixel **pixmap = ((miCanvas *)(_plotter->b_canvas))->drawable->pixmap;
- int width = _plotter->b_xn;
- int height = _plotter->b_yn;
- FILE *fp = _plotter->data->outfp;
- #ifdef LIBPLOTTER
- ostream *stream = _plotter->data->outstream;
- #endif
- #ifdef LIBPLOTTER
- if (fp == NULL && stream == NULL)
- return;
- #else
- if (fp == NULL)
- return;
- #endif
- if (fp)
- {
- if (portable)
- {
-
- unsigned char linebuf[4 * MAX_PGM_PIXELS_PER_LINE];
- int pos = 0;
- int num_pixels = 0;
- fprintf (fp, "\
- P2\n\
- # CREATOR: GNU libplot drawing library, version %s\n\
- %d %d\n\
- 255\n", PL_LIBPLOT_VER_STRING, width, height);
-
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
-
- FAST_PRINT (pixmap[j][i].u.rgb[0], linebuf, pos)
- num_pixels++;
- if (num_pixels >= MAX_PGM_PIXELS_PER_LINE || i == (width - 1))
- {
- fwrite ((void *)linebuf, sizeof(unsigned char), pos, fp);
- putc ('\n', fp);
- num_pixels = 0;
- pos = 0;
- }
- else
- linebuf[pos++] = ' ';
- }
- }
- else
- {
- unsigned char *rowbuf;
-
- rowbuf = (unsigned char *)_pl_xmalloc (width * sizeof (unsigned char));
- fprintf (fp, "\
- P5\n\
- # CREATOR: GNU libplot drawing library, version %s\n\
- %d %d\n\
- 255\n", PL_LIBPLOT_VER_STRING, width, height);
-
- for (j = 0; j < height; j++)
- {
- for (i = 0; i < width; i++)
- rowbuf[i] = pixmap[j][i].u.rgb[0];
- fwrite ((void *)rowbuf, sizeof(unsigned char), width, fp);
- }
- free (rowbuf);
- }
- }
- #ifdef LIBPLOTTER
- else if (stream)
- {
- if (portable)
- {
-
- unsigned char linebuf[4 * MAX_PGM_PIXELS_PER_LINE];
- int pos = 0;
- int num_pixels = 0;
- (*stream) << "\
- P2\n\
- # CREATOR: GNU libplot drawing library, version "
- << PL_LIBPLOT_VER_STRING << '\n'
- << width << ' ' << height << '\n'
- << "255" << '\n';
-
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
-
- FAST_PRINT (pixmap[j][i].u.rgb[0], linebuf, pos)
- num_pixels++;
- if (num_pixels >= MAX_PGM_PIXELS_PER_LINE || i == (width - 1))
- {
- stream->write ((const char *)linebuf, pos);
- stream->put ('\n');
- num_pixels = 0;
- pos = 0;
- }
- else
- linebuf[pos++] = ' ';
- }
- }
- else
- {
- unsigned char *rowbuf;
-
- (*stream) << "\
- P5\n\
- # CREATOR: GNU libplot drawing library, version "
- << PL_LIBPLOT_VER_STRING << '\n'
- << width << ' ' << height << '\n'
- << "255" << '\n';
-
- rowbuf = (unsigned char *)_pl_xmalloc (width * sizeof (unsigned char));
- for (j = 0; j < height; j++)
- {
- for (i = 0; i < width; i++)
- rowbuf[i] = pixmap[j][i].u.rgb[0];
- stream->write ((const char *)rowbuf, width);
- }
- free (rowbuf);
- }
- }
- #endif
- }
- void
- _pl_n_write_ppm (S___(Plotter *_plotter))
- {
- int i, j;
- bool portable = _plotter->n_portable_output;
- miPixel **pixmap = ((miCanvas *)(_plotter->b_canvas))->drawable->pixmap;
- int width = _plotter->b_xn;
- int height = _plotter->b_yn;
- FILE *fp = _plotter->data->outfp;
- #ifdef LIBPLOTTER
- ostream *stream = _plotter->data->outstream;
- #endif
- #ifdef LIBPLOTTER
- if (fp == NULL && stream == NULL)
- return;
- #else
- if (fp == NULL)
- return;
- #endif
- if (fp)
- {
- if (portable)
- {
-
- unsigned char linebuf[4 * MAX_PGM_PIXELS_PER_LINE];
- int pos = 0;
- int num_pixels = 0;
- fprintf (fp, "\
- P3\n\
- # CREATOR: GNU libplot drawing library, version %s\n\
- %d %d\n\
- 255\n", PL_LIBPLOT_VER_STRING, width, height);
-
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
-
- FAST_PRINT (pixmap[j][i].u.rgb[0], linebuf, pos)
- linebuf[pos++] = ' ';
- FAST_PRINT (pixmap[j][i].u.rgb[1], linebuf, pos)
- linebuf[pos++] = ' ';
- FAST_PRINT (pixmap[j][i].u.rgb[2], linebuf, pos)
- num_pixels++;
- if (num_pixels >= MAX_PPM_PIXELS_PER_LINE || i == (width - 1))
- {
- fwrite ((void *)linebuf, sizeof(unsigned char), pos, fp);
- putc ('\n', fp);
- num_pixels = 0;
- pos = 0;
- }
- else
- linebuf[pos++] = ' ';
- }
- }
- else
- {
- unsigned char *rowbuf;
- int component;
- fprintf (fp, "\
- P6\n\
- # CREATOR: GNU libplot drawing library, version %s\n\
- %d %d\n\
- 255\n", PL_LIBPLOT_VER_STRING, width, height);
-
- rowbuf = (unsigned char *)_pl_xmalloc (3 * width * sizeof (unsigned char));
- for (j = 0; j < height; j++)
- {
- for (i = 0; i < width; i++)
- for (component = 0; component < 3; component++)
- rowbuf[3 * i + component] = pixmap[j][i].u.rgb[component];
- fwrite ((void *)rowbuf, sizeof(unsigned char), 3 * width, fp);
- }
- free (rowbuf);
- }
- }
- #ifdef LIBPLOTTER
- else if (stream)
- {
- if (portable)
- {
-
- unsigned char linebuf[4 * MAX_PGM_PIXELS_PER_LINE];
- int pos = 0;
- int num_pixels = 0;
- (*stream) << "\
- P3\n\
- # CREATOR: GNU libplot drawing library, version "
- << PL_LIBPLOT_VER_STRING << '\n'
- << width << ' ' << height << '\n'
- << "255" << '\n';
-
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
-
- FAST_PRINT (pixmap[j][i].u.rgb[0], linebuf, pos)
- linebuf[pos++] = ' ';
- FAST_PRINT (pixmap[j][i].u.rgb[1], linebuf, pos)
- linebuf[pos++] = ' ';
- FAST_PRINT (pixmap[j][i].u.rgb[2], linebuf, pos)
- num_pixels++;
- if (num_pixels >= MAX_PPM_PIXELS_PER_LINE || i == (width - 1))
- {
- stream->write ((const char *)linebuf, pos);
- stream->put ('\n');
- num_pixels = 0;
- pos = 0;
- }
- else
- linebuf[pos++] = ' ';
- }
- }
- else
- {
- unsigned char *rowbuf;
- int component;
-
- (*stream) << "\
- P6\n\
- # CREATOR: GNU libplot drawing library, version "
- << PL_LIBPLOT_VER_STRING << '\n'
- << width << ' ' << height << '\n'
- << "255" << '\n';
-
- rowbuf = (unsigned char *)_pl_xmalloc (3 * width * sizeof (unsigned char));
- for (j = 0; j < height; j++)
- {
- for (i = 0; i < width; i++)
- for (component = 0; component < 3; component++)
- rowbuf[3 * i + component] = pixmap[j][i].u.rgb[component];
- stream->write ((const char *)rowbuf, 3 * width);
- }
- free (rowbuf);
- }
- }
- #endif
- }
- static int
- best_image_type (miPixel **pixmap, int width, int height)
- {
- int i, j;
- int type = 0;
-
- for (j = 0; j < height; j++)
- for (i = 0; i < width; i++)
- {
- unsigned char red, green, blue;
-
- red = pixmap[j][i].u.rgb[0];
- green = pixmap[j][i].u.rgb[1];
- blue = pixmap[j][i].u.rgb[2];
- if (type == 0)
- {
- if (! ((red == (unsigned char)0 && green == (unsigned char)0
- && blue == (unsigned char)0)
- || (red == (unsigned char)255 && green == (unsigned char)255
- && blue == (unsigned char)255)))
- {
- if (red == green && red == blue)
- type = 1;
- else
- {
- type = 2;
- return type;
- }
- }
- }
- else if (type == 1)
- {
- if (red != green || red != blue)
- {
- type = 2;
- return type;
- }
- }
- }
- return type;
- }
|