달력

3

« 2024/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2009. 9. 7. 23:11

PNG File 에 대한 간략 정리 Tip & Tips2009. 9. 7. 23:11


최근에 아는 지인과 이야기 하다가, PNG 파일의 변환 이야기가 나왔다. 하고 있는 일이 있는데, 몇가지 풀리지 않는 이미징 프로세싱상의 문제점이었는데, 나의 호기심을 자극하였다.

내가 그동안 알고 있는 PNG파일에 대한 내용은
1. 라이센스가 없다.
2. R,G,B 와 알파 채널을 제공한다.
이것이 전부이다. 그래서, 쉽게 쓰고, 편하게 쓰자 였다.

요즘 왠만한 웹사이트와 개발되는 프로그램들은 PNG파일들을 대부분 기본으로 지원하고 있다. 이는 개발툴과 Application 기본적으로 지원하고 있다는 말이다.  iPhone에서도 기본 이미지는 png 파일을 이용해서 메뉴 아이콘을 구성하고 있다.

관련 자료들은 내가 잘 이용하는 위키디피아에서 찾아 보았다.
PNG파일데 대한 다음 링크를 브라우져에서 열어보면, 아래와 같은 목차가 나타난다.


세부 사항들은 위에서 각 링크를 찾아 보면 되고, 간략하게 PNG파일에 대해서 요약하면,
PNG 는 Potalble Network Graphics의 약자로, 무손실 데이터 압축을 사용하는 비트맵 이미지 포맷이다. PNG는 GIF 포맷을 개선하고, 대체하기 위해 만들어진 포맷으로 파일 포맷에 대한 라이센스를 필요로 하지 않는다. PNG는 palette기반의 24bit RGB color와 greyscale RGB 그리고 RGBA (알파체널을 포함한 RGB) 이미지들을 지원한다.
그리고, 이 파일 포맷은 인터넷상에서 전송을 위한 목적으로 설계되었기 때문에, 전문 적인 디자인을 위한 CMYK와 같은 color space는 제공하지 않고 있다.

마지막으로 PNG 파일은 "PNG" 또는 "png" 확장자로 사용되고, 인터넷에서 주로 사용하고 있는 Mine media type으로는 "image/png" 로 정의해서 사용한다.

PNG file의 헤더는 8 byte로 되어 있다. (필드의 세부 내용은 아래 참조)
Bytes Purpose
89 Has the high bit set to detect transmission systems that do not support 8 bit data and to reduce the chance that a text file is mistakenly interpreted as a PNG, or vice versa.
50 4E 47 In ASCII, the letters "PNG", allowing a person to identify the format easily if it is viewed in a text editor.
0D 0A A DOS style line ending (CRLF) to detect DOS-UNIX line ending conversion of the data.
1A A byte that stops display of the file under DOS when the command type has been used—the end-of-file character
0A A UNIX style line ending (LF) to detect UNIX-DOS line ending conversion.



PNG 파일을 사용하는 .Net Framework의 예제는 아래와 같다.
(물론 여러 툴에서 각각 UI 에서 지원하기 위한 클래스와 API가 있지만, 단지 내가 좋아하는 VB를 좋아하기 때문에 예제도 MSDN에서 찾아 보았다.)

다음 링크를 따라 가면, 2개의 예제가 있다.

사용하는 예제는 무척 간단한다. Encode 또는 Decode 클래스를 이용하면되는데, 기본적인 사용법을 알아두면, GIF 나 JPEG 의 Encode 또는 Decode 클래스를 쉽게 이용할 수 있다.

[PNG Image를  Decodeing 하는 예제]
' Open a Stream and decode a PNG image
Dim imageStreamSource As New FileStream("smiley.png", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim decoder As New PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource As BitmapSource = decoder.Frames(0)

' Draw the Image
Dim myImage As New Image()
myImage.Source = bitmapSource
myImage.Stretch = Stretch.None
myImage.Margin = New Thickness(20)

[PNG Image를  Encodeing 하는 예제]
Dim width As Integer = 128
Dim height As Integer = 128
Dim stride As Integer = width
Dim pixels(height * stride) As Byte
' Define the image palette
Dim myPalette As BitmapPalette = BitmapPalettes.Halftone256
' Creates a new empty image with the pre-defined palette
Dim image As BitmapSource = System.Windows.Media.Imaging.BitmapSource.Create( _
width, height, 96, 96, PixelFormats.Indexed8, myPalette, pixels, stride)
Dim stream As New FileStream("new.png", FileMode.Create)
Dim encoder As New PngBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Interlace = PngInterlaceOption.On
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)


:
Posted by 행복상자