convert the pdf to png(gray) with gs
$gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pnggray -r300 -sOutputFile=/tmp/out.png /tmp/1.pdf
rgb to grayscale
y = 0.3*pxl[0][0]+0.59*pxl[1][0]+0.11*pxl[2][0]
彩色图变为灰度图的问题
2008-05-15 18:02
参照<百度知道 - 灰度图>
什么叫灰度图
任何颜色都有红、绿、蓝三原色组成,假如原来某点的颜色为RGB(R,G,B),那么,我们可以通过下面几种方法,将其转换为灰度:
1.浮点算法:Gray=R*0.3+G*0.59+B*0.11
2.整数方法:Gray=(R*30+G*59++B*11)/100
3.移位方法:Gray =(R*28+G*151+B*77)>>8;
4.平均值法:Gray=(R+G+B)/3;
5.仅取绿色:Gray=G;
通过上述任一种方法求得Gray后,将原来的RGB(R,G,B)中的R,G,B统一用Gray替换,形成新的颜色RGB(Gray,Gray,Gray),用它替换原来的RGB(R,G,B)就是灰度图了。
一个256色的bmp,要将其变为灰度图。 图像中每一个像素应该是在调色板中索引值要灰度化,先取出基R,G,B分量,再乘以一定的系数才行。
---------------------------------------------------------------
灰度级处理
procedure Gray(bmp: TBitmap);
var
p: PByteArray;
w: Integer;
i, j: Integer;
begin
bmp.pixelformat := pf24bit;
for i := 0 to bmp.height - 1 do
begin
p := bmp.scanline[i];
j := 0;
while j < (bmp.width-1) * 3 do
begin
w :=(p[j] * 28 + p[j+1] * 151 + p[j+2]*77);
w := w shr 8;
p[j] := byte(w);
p[j+1] := byte(w);
p[j+2] := byte(w);
inc(j, 3)
end;
end;
end;
**************************
uses
Windows, Graphics;
//方案1
function ConvertBitmapToGrayscale1(Bitmap: TBitmap): TBitmap;
var
i, j: Integer;
Grayshade, Red, Green, Blue: Byte;
PixelColor: Longint;
begin
with Bitmap do
for i := 0 to Width - 1 do
for j := 0 to Height - 1 do
begin
PixelColor := ColorToRGB(Canvas.Pixels[i, j]);
Red := PixelColor;
Green := PixelColor shr 8;
Blue := PixelColor shr 16;
Grayshade := Round(0.3 * Red + 0.6 * Green + 0.1 * Blue);
Canvas.Pixels[i, j] := RGB(Grayshade, Grayshade, Grayshade);
end;
Result := Bitmap;
end;
//方案2
function ConvertBitmapToGrayscale2(Bmp: TBitmap): TBitmap;
type
TRGBArray = array[0..32767] of TRGBTriple;
PRGBArray = ^TRGBArray;
var
x, y, Gray: Integer;
Row: PRGBArray;
begin
Bmp.PixelFormat := pf24Bit;
for y := 0 to Bmp.Height - 1 do
begin
Row := Bmp.ScanLine[y];
for x := 0 to Bmp.Width - 1 do
begin
Gray := (Row[x].rgbtRed + Row[x].rgbtGreen + Row[x].rgbtBlue) div 3;
Row[x].rgbtRed := Gray;
Row[x].rgbtGreen := Gray;
Row[x].rgbtBlue := Gray;
end;
end;
Result := Bmp;
end;
//方案2是最合理的算法
//方案3
function ConvertBitmapToGrayscale3(Bitmap: TBitmap): TBitmap;
type
PPixelRec = ^TPixelRec;
TPixelRec = packed record
B: Byte;
G: Byte;
R: Byte;
Reserved: Byte;
end;
var
X: Integer;
Y: Integer;
P: PPixelRec;
Gray: Byte;
begin
Assert(Bitmap.PixelFormat = pf32Bit);
for Y := 0 to (Bitmap.Height - 1) do
begin
P := Bitmap.ScanLine[Y];
for X := 0 to (Bitmap.Width - 1) do
begin
Gray := Round(0.30 * P.R + 0.59 * P.G + 0.11 * P.B);
// Gray := (P.R shr 2) + (P.R shr 4) + (P.G shr 1) + (P.G shr 4) + (P.B shr 3);
P.R := Gray;
P.G := Gray;
P.B := Gray;
Inc(P);
end;
end;
Result := Bitmap;
end;
转贴自:http://www.delphibbs.com/keylife/iblog_show.asp?xid=20785