2009-07-08 3 views
2

저는 현재 Brendan Tompkins ImageQuantization dll을 사용하고 있습니다. http://codebetter.com/blogs/brendan.tompkins/archive/2007/06/14/gif-image-color-quantizer-now-with-safe-goodness.aspx마샬링없이 이미지 퀀 타이즈를 안전하게 할 수있는 방법이 있습니까?

그러나 중간 신뢰에는 asp.net에서 실행되지 않습니다.

중간 신뢰에서 실행되는 이미지 양자화 라이브러리를 아는 사람이 있습니까?

업데이트 솔루션이 느린 지 신경 쓰지 않습니다. 그저 효과가있는 것이 필요합니다.

답변

3

Marshal을 사용하여 코드를 바꿀 수 있어야합니다. BinaryReader와 같은 것을 통해 기본 스트림을 명시 적으로 읽어야합니다. 이는 관리되는 메모리로 스트림 전체를 읽거나 신속하게 액세스 할 수있는 관리되지 않는 메모리의 복사본에 의존하지 않고 스트림을 검색해야하기 때문에 느려질 수 있습니다. 근본적으로 유일한 옵션입니다.

독점 작업 만 수행하는 경우에도 매체 신뢰 컨텍스트에서 관리되지 않는 메모리로 간단하게 갈 수 없습니다.

링크 된 코드를 살펴본 결과 이런 종류의 일을 할 수없는 이유가 있습니다. 처음에는 IntPtr의 64/32 비트 측면을 무시했습니다!

그가 사용하는 기본 BitMapData 클래스는 자유로운 임의의 메모리에 대한 읽기 액세스를 전제로하며 완전히 중간 신뢰 하에서는 발생하지 않습니다.
BitMap을 직접 (느린 GetPixel 호출로) 사용하거나 기존 스트림 API를 통해 직접 데이터를 읽고 배열에 놓은 다음 직접 구문 분석하면 기본 기능이 크게 다시 작성됩니다. 이들 중 어느 것도 즐겁지 않을 것입니다. 전자는 훨씬 더 느릴 것입니다 (픽셀 당 높은 오버 헤드로 인해 크기가 늘어날 것으로 예상합니다), 나중에는 느리지 만 (느린 속도이지만) 이미지 데이터의 저급 파싱을 다시 작성하는 것과 관련하여 훨씬 많은 노력이 필요합니다. . 당신이 다른 기능에 거의 동일한 기능을 수행 할 필요가 Quantizer.cs

public Bitmap Quantize(Image source) 
{ 
    // Get the size of the source image 
    int height = source.Height; 
    int width = source.Width; 
    // And construct a rectangle from these dimensions 
    Rectangle bounds = new Rectangle(0, 0, width, height); 
    // First off take a 32bpp copy of the image 
    Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb); 
    // And construct an 8bpp version 
    Bitmap output = new Bitmap(width, height, PixelFormat.Format8bppIndexed); 
    // Now lock the bitmap into memory 
    using (Graphics g = Graphics.FromImage(copy)) 
    { 
     g.PageUnit = GraphicsUnit.Pixel; 
     // Draw the source image onto the copy bitmap, 
     // which will effect a widening as appropriate. 
      g.DrawImage(source, bounds); 
    } 

    //!! BEGIN CHANGES - no locking here 
    //!! simply use copy not a pointer to it 
    //!! you could also simply write directly to a buffer then make the final immage in one go but I don't bother here 

    // Call the FirstPass function if not a single pass algorithm. 
    // For something like an octree quantizer, this will run through 
    // all image pixels, build a data structure, and create a palette. 
    if (!_singlePass) 
     FirstPass(copy, width, height); 

    // Then set the color palette on the output bitmap. I'm passing in the current palette 
    // as there's no way to construct a new, empty palette. 
    output.Palette = GetPalette(output.Palette); 
    // Then call the second pass which actually does the conversion 
    SecondPass(copy, output, width, height, bounds); 
    //!! END CHANGES 
    // Last but not least, return the output bitmap 
    return output; 
} 

//!! Completely changed, note that I assume all the code is changed to just use Color rather than Color32 
protected virtual void FirstPass(Bitmap source, int width, int height) 
{ 
    // Loop through each row 
    for (int row = 0; row < height; row++) 
    { 
     // And loop through each column 
     for (int col = 0; col < width; col++) 
     {    
      InitialQuantizePixel(source.GetPixel(col, row)); 
     } // Now I have the pixel, call the FirstPassQuantize function... 
    } 
} 

에서

:

여기에 거친 현재 코드에 따라 변경해야 할 무엇을 안내합니다. 이것은 Color32가 필요 없으면 Bitmap 클래스가이를 처리합니다.

Bitmap.SetPixel()은 두 번째 패스를 처리합니다. 이것은 포팅하는 가장 쉬운 방법이지만 중간 신뢰 환경에서 수행하는 가장 빠른 방법은 아닙니다.

+0

마샬링을 호출하면 보통 신뢰가 실패합니다. :-( –

+0

메모리에서 모두 비 관리 코드가 필요한 링크 요구 사항이있는 모든 항목. 기본 비트 맵을 직접 읽을 수 없습니까? – ShuggyCoUk

+0

Bitmap.GetPixel (x, y)를 합법적으로 호출하면 색상, 천천히 비록 – ShuggyCoUk

관련 문제