Ejemplo n.º 1
0
void RemoteSlaveProcess::Open(const char *host, int port, const char *cmdline, const char *envptr, int to)
{
    SVRLOG("RemoteSlaveProcess(" << host << ":" << port << ")=" << cmdline);
    Kill();

    timeout = to;
    Socket::Init();
    String localhost;
    if(host == 0 || *host == 0)
    {
        localhost = Socket::GetHostName();
        host = localhost;
    }
    if(port == 0)
        port = DEFAULT_PORT;
    terminated = false;
    current_part = 0;
    output[0] = output[1] = output[2] = Null;
    if(!ClientSocket(socket, host, port, true, NULL, REQUEST_TIMEOUT))
        throw Exc(NFormat(t_("Opening host '%s' / port %d failed, error = %s"), host, port, Socket::GetErrorText()));
    int len = (int)strlen(cmdline);
    if(envptr && *envptr) {
        const char *e = envptr;
        while(*e)
            e = e + strlen(e) + 1;
        socket.Write(":");
        socket.Write(ASCII85Encode(String(envptr, e + 1)));
        socket.Write("\n");
    }
    socket.Write("=");
    socket.Write(cmdline, len + 1); // send terminating 0 as well
    Recv(0, timeout);
    if(output[0][0] == '-')
        throw Exc(NFormat(t_("Error running process: %s\nCommand: %s"), output[0].Begin() + 1, cmdline));

    if(output[0] != "+")
        throw Exc(NFormat(t_("Communication error; process = %s"), cmdline));
}
Ejemplo n.º 2
0
static void
JPEGtoPS P2(imagedata *, JPEG, FILE *, PSfile) {
    int llx, lly, urx, ury;        /* Bounding box coordinates */
    size_t n;
    float scale, sx, sy;           /* scale factors            */
    time_t t;
    int i;

    /* read image parameters and fill JPEG struct*/
    if (!AnalyzeJPEG(JPEG)) {
        fprintf(stderr, "Error: '%s' is not a proper JPEG file!\n", JPEG->filename);
        return;
    }

    if (!quiet)
        fprintf(stderr, "Note on file '%s': %dx%d pixel, %d color component%s\n",
                JPEG->filename, JPEG->width, JPEG->height, JPEG->components,
                (JPEG->components == 1 ? "" : "s"));

    /* "Use resolution from file" was requested, but we couldn't find any */
    if (JPEG->dpi == DPI_USE_FILE && !quiet) {
        fprintf(stderr,
                "Note: no resolution values found in JPEG file - using standard scaling.\n");
        JPEG->dpi = DPI_IGNORE;
    }

    if (JPEG->dpi == DPI_IGNORE) {
        if (JPEG->width > JPEG->height && autorotate) {	/* switch to landscape if needed */
            JPEG->landscape = TRUE;
            if (!quiet)
                fprintf(stderr,
                        "Note: image width exceeds height - producing landscape output!\n");
        }
        if (!JPEG->landscape) {       /* calculate scaling factors */
            sx = (float) (PageWidth - 2*Margin) / JPEG->width;
            sy = (float) (PageHeight - 2*Margin) / JPEG->height;
        } else {
            sx = (float) (PageHeight - 2*Margin) / JPEG->width;
            sy = (float) (PageWidth - 2*Margin) / JPEG->height;
        }
        scale = min(sx, sy);	/* We use at least one edge of the page */
    } else {
        if (!quiet)
            fprintf(stderr, "Note: Using resolution %d dpi.\n", (int) JPEG->dpi);
        scale = 72 / JPEG->dpi;     /* use given image resolution */
    }

    if (JPEG->landscape) {
        /* landscape: move to (urx, lly) */
        urx = PageWidth - Margin;
        lly = Margin;
        ury = (int) (Margin + scale*JPEG->width + 0.9);    /* ceiling */
        llx = (int) (urx - scale * JPEG->height);          /* floor  */
    } else {
        /* portrait: move to (llx, lly) */
        llx = lly = Margin;
        urx = (int) (llx + scale * JPEG->width + 0.9);     /* ceiling */
        ury = (int) (lly + scale * JPEG->height + 0.9);    /* ceiling */
    }

    time(&t);

    /* produce EPS header comments */
    fprintf(PSfile, "%%!PS-Adobe-3.0 EPSF-3.0\n");
    fprintf(PSfile, "%%%%Creator: jpeg2ps %s by Thomas Merz\n", VERSION);
    fprintf(PSfile, "%%%%Title: %s\n", JPEG->filename);
    fprintf(PSfile, "%%%%CreationDate: %s", ctime(&t));
    fprintf(PSfile, "%%%%BoundingBox: %d %d %d %d\n",
            llx, lly, urx, ury);
    fprintf(PSfile, "%%%%DocumentData: %s\n",
            JPEG->mode == BINARY ? "Binary" : "Clean7Bit");
    fprintf(PSfile, "%%%%LanguageLevel: 2\n");
    fprintf(PSfile, "%%%%EndComments\n");
    fprintf(PSfile, "%%%%BeginProlog\n");
    fprintf(PSfile, "%%%%EndProlog\n");
    fprintf(PSfile, "%%%%Page: 1 1\n");

    fprintf(PSfile, "/languagelevel where {pop languagelevel 2 lt}");
    fprintf(PSfile, "{true} ifelse {\n");
    fprintf(PSfile, "  (JPEG file '%s' needs PostScript Level 2!",
            JPEG->filename);
    fprintf(PSfile, "\\n) dup print flush\n");
    fprintf(PSfile, "  /Helvetica findfont 20 scalefont setfont ");
    fprintf(PSfile, "100 100 moveto show showpage stop\n");
    fprintf(PSfile, "} if\n");

    fprintf(PSfile, "save\n");
    fprintf(PSfile, "/RawData currentfile ");

    if (JPEG->mode == ASCIIHEX)            /* hex representation... */
        fprintf(PSfile, "/ASCIIHexDecode filter ");
    else if (JPEG->mode == ASCII85)        /* ...or ASCII85         */
        fprintf(PSfile, "/ASCII85Decode filter ");
    /* else binary mode: don't use any additional filter! */

    fprintf(PSfile, "def\n");

    fprintf(PSfile, "/Data RawData << ");
    fprintf(PSfile, ">> /DCTDecode filter def\n");

    /* translate to lower left corner of image */
    fprintf(PSfile, "%d %d translate\n", (JPEG->landscape ?
                                          PageWidth - Margin : Margin), Margin);

    if (JPEG->landscape)                 /* rotation for landscape */
        fprintf(PSfile, "90 rotate\n");

    fprintf(PSfile, "%.2f %.2f scale\n", /* scaling */
            JPEG->width * scale, JPEG->height * scale);
    fprintf(PSfile, "/Device%s setcolorspace\n",
            ColorSpaceNames[JPEG->components]);
    fprintf(PSfile, "{ << /ImageType 1\n");
    fprintf(PSfile, "     /Width %d\n", JPEG->width);
    fprintf(PSfile, "     /Height %d\n", JPEG->height);
    fprintf(PSfile, "     /ImageMatrix [ %d 0 0 %d 0 %d ]\n",
            JPEG->width, -JPEG->height, JPEG->height);
    fprintf(PSfile, "     /DataSource Data\n");
    fprintf(PSfile, "     /BitsPerComponent %d\n",
            JPEG->bits_per_component);

    /* workaround for color-inverted CMYK files produced by Adobe Photoshop:
     * compensate for the color inversion in the PostScript code
     */
    if (JPEG->adobe && JPEG->components == 4) {
        if (!quiet)
            fprintf(stderr, "Note: Adobe-conforming CMYK file - applying workaround for color inversion.\n");
        fprintf(PSfile, "     /Decode [1 0 1 0 1 0 1 0]\n");
    } else {
        fprintf(PSfile, "     /Decode [0 1");
        for (i = 1; i < JPEG->components; i++)
            fprintf(PSfile," 0 1");
        fprintf(PSfile, "]\n");
    }

    fprintf(PSfile, "  >> image\n");
    fprintf(PSfile, "  Data closefile\n");
    fprintf(PSfile, "  RawData flushfile\n");
    fprintf(PSfile, "  showpage\n");
    fprintf(PSfile, "  restore\n");
    fprintf(PSfile, "} exec");

    /* seek to start position of JPEG data */
    fseek(JPEG->fp, JPEG->startpos, SEEK_SET);

    switch (JPEG->mode) {
    case BINARY:
        /* important: ONE blank and NO newline */
        fprintf(PSfile, " ");
#ifdef DOS
        fflush(PSfile);         	  /* up to now we have CR/NL mapping */
        setmode(fileno(PSfile), O_BINARY);    /* continue in binary mode */
#endif
        /* copy data without change */
        while ((n = fread(buffer, 1, sizeof(buffer), JPEG->fp)) != 0)
            fwrite(buffer, 1, n, PSfile);
#ifdef DOS
        fflush(PSfile);                  	/* binary yet */
        setmode(fileno(PSfile), O_TEXT);    /* text mode */
#endif
        break;

    case ASCII85:
        fprintf(PSfile, "\n");

        /* ASCII85 representation of image data */
        if (ASCII85Encode(JPEG->fp, PSfile)) {
            fprintf(stderr, "Error: internal problems with ASCII85Encode!\n");
            exit(1);
        }
        break;

    case ASCIIHEX:
        /* hex representation of image data (useful for buggy dvips) */
        ASCIIHexEncode(JPEG->fp, PSfile);
        break;
    }
    fprintf(PSfile, "\n%%%%EOF\n");
}