'흑백 jpeg'에 해당되는 글 2건

  1. 2009.07.14 libjpeg colormap - 256 color jpeg
  2. 2009.07.03 libjpeg 을 이용하여 흑백 jpg를 bmp로 변환하기
프로그램 사용/libjpeg2009. 7. 14. 17:34
jpeg는 grayscale일 경우에는 무조건 흑백인 듯 하다.
(조금 더 확실하게 검색필요)

그런 이유로, 8bit color일 경우에는 팔레트가 NULL인지 아닌지 확인하고
NULL일 경우에는 팔레트를 만들어주면 된다.

wrbmp.c 파일을 참고 하자면
LOCAL(void)
write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest,
		int map_colors, int map_entry_size)
{
  JSAMPARRAY colormap = cinfo->colormap;
  int num_colors = cinfo->actual_number_of_colors;
  FILE * outfile = dest->pub.output_file;
  int i;

  if (colormap != NULL) {
    if (cinfo->out_color_components == 3) {
      /* Normal case with RGB colormap */
      for (i = 0; i < num_colors; i++) {
	putc(GETJSAMPLE(colormap[2][i]), outfile);
	putc(GETJSAMPLE(colormap[1][i]), outfile);
	putc(GETJSAMPLE(colormap[0][i]), outfile);
	if (map_entry_size == 4)
	  putc(0, outfile);
      }
    } else {
      /* Grayscale colormap (only happens with grayscale quantization) */
      for (i = 0; i < num_colors; i++) {
	putc(GETJSAMPLE(colormap[0][i]), outfile);
	putc(GETJSAMPLE(colormap[0][i]), outfile);
	putc(GETJSAMPLE(colormap[0][i]), outfile);
	if (map_entry_size == 4)
	  putc(0, outfile);
      }
    }
  } else {
    /* If no colormap, must be grayscale data.  Generate a linear "map". */
    for (i = 0; i < 256; i++) {
      putc(i, outfile);
      putc(i, outfile);
      putc(i, outfile);
      if (map_entry_size == 4)
	putc(0, outfile);
    }
  }
  /* Pad colormap with zeros to ensure specified number of colormap entries */ 
  if (i > map_colors)
    ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i);
for (; i < map_colors; i++) { putc(0, outfile); putc(0, outfile); putc(0, outfile); if (map_entry_size == 4) putc(0, outfile);
}
}

이런식으로 0에서 255 까지 팔레트를 생성하면 되고,
256color(=8bit) 일 경우에는 팔레트는 RGBQUAD 로 4바이트 구조이므로,
마지막 바이트는 reserve(혹은 alpha) 값으로 0x00(stfae에서는0x80) 을 해주면 된다.
Posted by 구차니
- 조금 더 조사해보고 다시 써야함 -

libjpeg를 이용하여 jpeg를 변환하는데 다른건 잘되길래
테스트 하기위해 gimp에서 grayscale로 변환하였고, 데이터 상으로는 8bit 이미지로 출력이 되었다.
막상 화면에 뿌리려고 보니 기본 루틴이 24bit라서 이상하게 나오는데,
나오는 모습을 보니 묘하다고 해야 하나.. 아무튼 grayscale이니 1byte 단위로  gray만 출력하는 것으로 보인다.

그런데.. 흑백 bitmap은 구조가 어떻게 되려나?


jpg(좌) bmp(우)


회색 이미지는 GIMP에서 jpg를 grayscale로 변환후, bmp로 저장한 것이다.
이 파일 내용을 보니, 헤더 다음에 위와 같이 000 111 222 이런 식으로 반복되는 것이 있는데,
bitmap은 별도의  grayscale이 존재하는 것이 아니라, 256 indexed color로 저장이되며, 결국 "팔레트로 표현"이 된다.

Posted by 구차니