后记中如何包含img [英] How include img in postscript

查看:70
本文介绍了后记中如何包含img的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像添加到我的后记代码中

I want to add image to my postscript code

%!PS-Adobe-3.0

/Times-Roman findfont
12 scalefont setfont

50 700 moveto
(text) show
showpage

但我不知道要这样做。有人帮忙吗?

but i dont have idea to do that. Someone help?

推荐答案

; tldr



跳到中间并开始从简单的工作流程开始阅读。

Postscript语言参考手册中对此进行了记录,但是该信息可能有点难以理解

It is documented in the Postscript Language Reference Manual, but the information can be a little hard to digest.

正如Ken所说,您需要使用 image 运算符。我通常会采用旧式格式,即:

As Ken says, you need to use the image operator. I usually go for the "old school" form which is


宽度高度/像素矩阵proc  图片

对于某些随机图像文件,通常希望使用 convert 从ImageMagick获取文本格式。当然,您也可以将其转换为eps,但是要学习,您必须将手指放在其中。

For some random image file, you usually want to use something like convert from ImageMagick to get a textual format. Of course you could also just convert it to an eps, but to learn, you've got to stick your fingers in it.

% convert image.png image.xbm

这将为您提供一个文件,例如:

This will give you a file like:

  1 #define glasses_width 320
  2 #define glasses_height 240
  3 static char glasses_bits[] = {
  4   0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5   0x00, 0x00, 0x00, 0x00, 0x45, 0x65, 0xDB, 0x65, 0xB5, 0x6F, 0xBF, 0xEF,
  6   0xFF, 0xFF, 0xFF, 0xBF, 0xB5, 0xED, 0x3C, 0xBF, 0xB3, 0xDB, 0xAD, 0xF6,
  7   0xE6, 0x4A, 0xAA, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
  8   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0xA8, 0x66, 0xD6,
  9   0xDF, 0xF9, 0xF7, 0xBF, 0xFF, 0xFD, 0xFF, 0xFE, 0xFF, 0x7F, 0xFB, 0xEA,
 10   0xDD, 0x5A, 0x9A, 0x69, 0xB9, 0xBE, 0x55, 0x65, 0x00, 0x00, 0x00, 0x00,
...
803   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
804   };

因此,在vi中,要做一些类似的事情

So, in vi, do a few things like

:%s/^#/%#/         #comment-out the #defines
:g/[{}]/d          #delete the array brackets
:%s/0x//g          #remove the 0x prefixes
:%s/, //g          #remove the spaces

提供以下内容:

  1 %#define glasses_width 320
  2 %#define glasses_height 240 
  3   000000000003000000000000
  4   000000004565DB65B56FBFEF
  5   FFFFFFBFB5ED3CBFB3DBADF6
  6   E64AAABA0000000000020000
  7   000000000000000099A866D6
  8   DFF9F7BFFFFDFFFEFF7FFBEA
  9   DD5A9A69B9BE556500000000
 10   000C00000000000000000000
...
802   000000000000000000000000

然后使用数字在 image 调用中,也修剪这些行,并将数据直接插入这些行之后

Then you use the numbers in the image call, trim those lines too, and insert the data directly after these lines

%width height depth [ x-scale x-skew y-skew y-scale x-offset y-offset ]=matrix 
320    240    1     [ 1       0      0      -1      0        240 ]
%  {proc-yielding-string-data}                  call(image)
   { currentfile 80 string readhexstring pop }  image

这假设您的位图数据的y向下增加。只要您可以转储某种原始样本,就可以对其他ascii格式进行调整。我建议您暂时避免使用带有解码器代码的压缩图像嵌入蠕虫。 (主要是因为我还不知道该怎么做。我一直在像大型蠕虫一样避免使用它。:D)

This assumes that your bitmap data has y increasing downwards. This approach can be tweaked for other ascii formats, as long as you can get some kind of dump of the raw samples. Embedding compressed images with decoder code is a big can of worms I suggest you avoid for a while. (Mostly because I don't know how to do this, yet. I've been avoiding it like a big can o' worms. :D)

我在上面检查了我的建议,但我忘了一个大障碍。后记喜欢它的位图以大端字节为单位。也就是说,位7是最左,位0是最右位。这与xbm格式相反。因此,上面介绍的已完成程序是:

I checked my advice above, and there is a big snag I forgot. Postscript likes its bitmaps in big-endian bytes. That is, bit 7 is the leftmost and bit 0 is the rightmost bit. This is the reverse of the xbm format. So, the completed program introduced above is:

%!
%reverse the bits in a byte
/reverse {               % b
    dup 1 and            % b b0          % explode the bits
    1 index 2 and        % b b0 b1
    2 index 4 and        % b b0 b1 b2
    3 index 8 and        % b b0 b1 b2 b3
    4 index 16 and       % b b0 b1 b2 b3 b4
    5 index 32 and       % b b0 b1 b2 b3 b4 b5
    6 index 64 and       % b b0 b1 b2 b3 b4 b5 b6
    8 7 roll 128 and     % b0 b1 b2 b3 b4 b5 b6 b7
    -7 bitshift exch     % b0 b1 b2 b3 b4 b5 b7-7=0' b6  % shift and combine
    -5 bitshift or exch  % b0 b1 b2 b3 b4 b0'|b6-5=1' b5
    -3 bitshift or exch  % b0 b1 b2 b3 b0'|b1'|b5-3=2' b4
    -1 bitshift or exch  % b0 b1 b2 b0'|b1'|b2'|b4-1=3' b3
    1 bitshift or exch   % b0 b1 b0'|b1'|b2'|b3'|b3+1=4' b2
    3 bitshift or exch   % b0 b0'|b1'|b2'|b3'|b4'|b2+3=5' b1
    5 bitshift or exch   % b0'|b1'|b2'|b3'|b4'|b5'|b1+5=6' b0
    7 bitshift or        % b0'|b1'|b2'|b3'|b4'|b5'|b6'|b0+7=7'
} def

320 240 1  % width height bitdepth
[ 1 0 0 -1 0 240 ]  % 1-to-1 matrix with descending y, offset by max_y
{ %proc-yielding-string
    currentfile 80 string  % file string
    readhexstring pop  % string         read a line of hex data from THIS FILE
    0 1 2 index length 1 sub  % string 0 1 strlen-1
    {  % string index
        2 copy 2 copy  % str i str i str i
        get reverse    % str i str i rev(str_i)
        put  % str' i
        pop % str'                      % reverse each char (byte)
    } for                               % loop over chars in string
} image
  000000000003000000000000
  000000004565DB65B56FBFEF
  FFFFFFBFB5ED3CBFB3DBADF6
  E64AAABA0000000000020000
  000000000000000099A866D6
  DFF9F7BFFFFDFFFEFF7FFBEA
  ...

完整程序可用,并附有完整图像数据此处

Full program available with full image data appended here.

更简单,但仍然是动手操作,它将转换为使用相同的 pbm 位序约定作为后记。然后 xxd -ps 将产生一个后记十六进制转储。以下三个示例都使用以此方式准备的十六进制数据。但是此方法仍然需要您手动测量标头的长度(使用 xxd 查找宽度和高度后的空白字符后的字节偏移)。

Simpler, but still "hand-on", is to convert to pbm which uses the same bit-ordering conventions as postscript. Then xxd -ps will produce a "postscript" hexdump. The following three examples all use hex data prepared this way. But this method still requires you to manually measure the length of the header (use xxd to find the byte offset after the whitespace-char after the width and height).

%!
% swar.ps
%
%image example
%image origin:  http://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Spacewar%21-PDP-1-20070512.jpg/320px-Spacewar%21-PDP-1-20070512.jpg
%
% bash commands to prepare image file:
%
% $ wget http://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Spacewar%21-PDP-1-20070512.jpg/320px-Spacewar%21-PDP-1-20070512.jpg
% $ identify 320px-Spacewar\!-PDP-1-20070512.jpg 
% $ convert 320px-Spacewar\!-PDP-1-20070512.jpg spacewar.pbm
% $ xxd -ps spacewar.pbm > spacewar.asc
% % gs swar.ps


/infile (spacewar.asc)(r)file def
/buf 256 string def

% use $ xxd spacewar.pbm | head
% to find the length of the header and read that length
% into the buffer and discard, leaving only samples.
infile buf 0 16#5d getinterval readhexstring pop pop 

320 215 1
[ 1 0 0 -1 0 215 ]
{ infile buf readhexstring pop } image

showpage

spacewar.asc 是相同的丑陋的裸十六进制样本块。

The spacewar.asc is the same ugly block of bare hex samples.

$ head spacewar.asc
50340a2346696c6520736f757263653a20687474703a2f2f636f6d6d6f6e
732e77696b696d656469612e6f72672f77696b692f46696c653a53706163
65776172212d5044502d312d32303037303531322e6a70670a3332302032
31350a007fffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffff007fffffffffffffffffffffffdfffffff
ffffffffffffffffffffffffffffffffffffffffffffff007fffffffffff
ffffffffffff803fffffffffffffffffffffffffffffffffffffffffffff
ffffff007ffffffffffffffffffffffff800ffffffffffffffffffffffff
ffffffffffffffffffffffffff007fffffffffffffffffffffff7fe007ff
ffffffffffffffffffffffffffffffffffffffffffffff007fffffffffff

只要解释器​​(或蒸馏器)未设置SAFER选项,此示例块就可以保留在外部,因为这会禁用文件访问运算符。

This block of samples can be left external, so long as the interpreter (or distiller) does not have the SAFER option set, as this disables the file-access operators.

也可以在图像调用后使用 currentfile 如上。

It can also be inlined after the image call by using currentfile as above.

%!
/buf 256 string def

320 215 1
[ 1 0 0 -1 0 215 ]
{ currentfile buf readhexstring pop }
{ 
    infile buf 0 16#5d getinterval readhexstring pop pop  % discard header
    image 
} exec
50340a2346696c6520736f757263653a20687474703a2f2f636f6d6d6f6e
732e77696b696d656469612e6f72672f77696b692f46696c653a53706163
65776172212d5044502d312d32303037303531322e6a70670a3332302032
31350a007fffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffff007fffffffffffffffffffffffdfffffff
ffffffffffffffffffffffffffffffffffffffffffffff007fffffffffff
ffffffffffff803fffffffffffffffffffffffffffffffffffffffffffff
ffffff007ffffffffffffffffffffffff800ffffffffffffffffffffffff
ffffffffffffffffffffffffff007fffffffffffffffffffffff7fe007ff
ffffffffffffffffffffffffffffffffffffffffffffff007fffffffffff
%...

或者,只要数据少于64k,您就可以将其插入
一个字符串。 [注意:这是字符串大小的历史实现限制。我被告知,当前版本的ghostscript可以很好地处理较大的字符串。 ]如果要在文档中多次重复使用图像
,则此功能很有用。

Or, so long as the data is less than 64k, you can slurp it into a string. [ Note: this is a historical implementation limitation on the size of strings. I have been informed that current versions of ghostscript can handle larger strings just fine. ] This is useful if you want to re-use the image several times in a document.

%!
/imgbuf 320 215 mul 8 div ceiling cvi string def   % create a string for byte storage (<64k)

{ 
    currentfile imgbuf 0 16#5d getinterval readhexstring pop pop  % read header
    currentfile imgbuf readhexstring pop pop                 % read data (discarding header data)
} exec
50340a2346696c6520736f757263653a20687474703a2f2f636f6d6d6f6e
732e77696b696d656469612e6f72672f77696b692f46696c653a53706163
65776172212d5044502d312d32303037303531322e6a70670a3332302032
31350a007fffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffff007fffffffffffffffffffffffdfffffff
ffffffffffffffffffffffffffffffffffffffffffffff007fffffffffff
ffffffffffff803fffffffffffffffffffffffffffffffffffffffffffff
ffffff007ffffffffffffffffffffffff800ffffffffffffffffffffffff
ffffffffffffffffffffffffff007fffffffffffffffffffffff7fe007ff
ffffffffffffffffffffffffffffffffffffffffffffff007fffffffffff
%...

320 215 1
[ 1 0 0 -1 0 215 ]
{ imgbuf }
image






我刚才掩盖的位(矩阵参数)...



上面的代码,在解释PLRM方面我非常自由。基本上忽略了一些常见的建议,因为通常(以我的经验)只是妨碍了对过程的理解,但是这里是...


The bit I glossed over earlier (the matrix argument)...

In all of the above code, I'm taking a great liberty in the interpretation of PLRM. Basically ignoring some common advice, because it usually (in my experience) just gets in the way of understanding the process, but here it is...

使用 image 运算符将使用与上面所示不同的矩阵。实际上,矩阵由 image 运算符解释为 inverse 映射。也就是说,要放大 ,您使数字更小,而缩小缩小,您使数字更大。您打算使用它的方式是(修改上面的最后一个示例,因为它是此示例中最短且结构合理的,即假设 imgbuf 已填充为上面带有 readhexstring )。
应将此呼叫更正确地编写:

The recommended method for using the image operator is to use a different kind of matrix than that shown above. The matrix is actually interpreted by the image operator as an inverse map. That is, to scale up you make the numbers smaller, and to scale down you make the numbers bigger. The way you're intended to use it is (modifying the final example above, because it's the shortest and well-factored for this example, ie. assume imgbuf has been populated as above with readhexstring). This call should more properly be written:

320 215 scale  % scale 1x1 image to proper dimensions
320 215 1          % "data" dimensions: w h bit-depth
[ 320 0 0 -215 0 215 ]              % inverse mapping
{ imgbuf }   % data-acquisition (yield data in a string)
image

即矩阵取反(这里是扭曲的诗意,而不是技术意义)将图像转换为1单位X 1单位的正方形,这允许(要求)您缩放坐标系以获取每像素1pt的图像渲染。正确的方法可以为您提供更大的灵活性:现在,您可以使用 320 215缩放比例行进行明智的缩放计算-如果您只想要一个1像素, 1点映射。

That is, the matrix inverts (here, a poetic sense of "twisting-up", not the technical sense) the image into a 1-unit-X-1-unit square, which allows (requires) you to scale the coordinate-system to get a 1pt-per-pixel rendering of the image. The proper approach affords you more flexibility: you now have the 320 215 scale line to make sensible scaling calculations with -- at the expense of repeating yourself if you just want a 1pixel-to-1point mapping.

要使用 proper 代码将图像尺寸加倍,只需替换 320 215 scale 的比例为 640 430 (或添加 2 2的比例)。

To double the dimensions of the image with the proper code, it's a simple matter of replacing 320 215 scale with 640 430 scale (or adding a 2 2 scale).

320 215 scale
2 2 scale % == 640 430 scale
320 215 1                           % w h bit-depth
[ 320 0 0 -215 0 215 ]              % inverse mapping
{ imgbuf }   % data-acquisition
image

但是使用 hackish 方法,实际上您必须将矩阵减半以获得增长。 :D

But with the hackish way, you actually have to halve the matrix to get the inverse of growth. :D

320 215 1
[ .5 0 0 -.5 0 430 ] % "doubled,inverted (ie. halved) with double-offset" matrix
{ imgbuf }
image

这篇关于后记中如何包含img的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆