Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IMAGE Class Reference

#include <img.h>

Public Member Functions

 IMAGE ()
 
 ~IMAGE ()
 
IMAGEoperator= (IMAGE &source)
 
inT8 create (inT32 x, inT32 y, inT8 bits_per_pixel)
 
inT8 capture (uinT8 *pixels, inT32 x, inT32 y, inT8 bits_per_pixel)
 
void destroy ()
 
inT32 get_xsize ()
 
inT32 get_ysize ()
 
inT8 get_bpp ()
 
inT8 get_bps ()
 
BOOL8 white_high ()
 
uinT8 get_white_level ()
 
inT32 get_res ()
 
void set_res (inT32 resolution)
 
uinT8get_buffer ()
 
uinT8 pixel (inT32 x, inT32 y)
 
void fast_get_line (inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
 
void get_line (inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
 
void get_column (inT32 x, inT32 y, inT32 height, IMAGELINE *linebuf, inT32 margins)
 
void fast_put_line (inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
 
void put_line (inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
 
void put_column (inT32 x, inT32 y, inT32 height, IMAGELINE *linebuf, inT32 margins)
 
void check_legal_access (inT32 x, inT32 y, inT32 xext)
 
Pix * ToPix ()
 
void FromPix (const Pix *src_pix)
 
void convolver (inT32 win_width, inT32 win_height, void(*convolve)(uinT8 **pixels, uinT8 bytespp, inT32 win_wd, inT32 win_ht, uinT8 ret_white_value, uinT8 *result))
 

Friends

DLLSYM void copy_sub_image (IMAGE *source, inT32 xstart, inT32 ystart, inT32 xext, inT32 yext, IMAGE *dest, inT32 xdest, inT32 ydest, BOOL8 adjust_grey)
 
DLLSYM void enlarge_sub_image (IMAGE *source, inT32 xstart, inT32 ystart, IMAGE *dest, inT32 xdest, inT32 ydest, inT32 xext, inT32 yext, inT32 scale, BOOL8 adjust_grey)
 
DLLSYM void fast_reduce_sub_image (IMAGE *source, inT32 xstart, inT32 ystart, inT32 xext, inT32 yext, IMAGE *dest, inT32 xdest, inT32 ydest, inT32 scale, BOOL8 adjust_grey)
 
DLLSYM void reduce_sub_image (IMAGE *source, inT32 xstart, inT32 ystart, inT32 xext, inT32 yext, IMAGE *dest, inT32 xdest, inT32 ydest, inT32 scale, BOOL8 adjust_grey)
 

Detailed Description

encapsulated image

Definition at line 51 of file img.h.

Constructor & Destructor Documentation

IMAGE::IMAGE ( )

Definition at line 72 of file imgs.cpp.

72  { //construct an image
73  bpp = 0; //all illegal
74  fd = -1;
75  image = NULL;
76  photo_interp = 1;
78 }
#define NULL
Definition: host.h:144
EXTERN int image_default_resolution
Definition: imgs.cpp:64
IMAGE::~IMAGE ( )
inline

Definition at line 56 of file img.h.

56  { //destructor
57  destroy(); //free memory
58  }
void destroy()
Definition: imgs.cpp:150

Member Function Documentation

inT8 IMAGE::capture ( uinT8 pixels,
inT32  x,
inT32  y,
inT8  bits_per_pixel 
)

capture raw image

Parameters
pixelspixels to capture
xx size required
yy size required
bits_per_pixelbpp required

Definition at line 170 of file imgs.cpp.

175  {
176  destroy();
177  xdim = check_legal_image_size (x, y, bits_per_pixel);
178  if (xdim < 0)
179  return -1;
180  xsize = x;
181  ysize = y;
182  bufheight = y;
183  bpp = bits_per_pixel;
184  bps = bpp == 24 ? 8 : bpp;
185  photo_interp = 1;
186  bytespp = (bpp + 7) / 8;
187  image = pixels; //assign image area
188  ymin = 0;
189  ymax = bufheight; //read it all
190  captured = TRUE;
192  return 0; //success
193 }
void destroy()
Definition: imgs.cpp:150
inT32 check_legal_image_size(inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:240
EXTERN int image_default_resolution
Definition: imgs.cpp:64
#define TRUE
Definition: capi.h:27
void IMAGE::check_legal_access ( inT32  x,
inT32  y,
inT32  xext 
)

check coordinates

Parameters
xxcoord to check
yycoord to check
xextextent

Definition at line 1484 of file imgs.cpp.

1488  {
1489  if (x < 0 || x >= xsize || y < 0 || y >= ysize || x + xext > xsize)
1490  BADIMAGECOORDS.error ("IMAGE::check_legal_access",
1491  ABORT, "(%d+%d,%d)", x, xext, y);
1492  if (y < ymin || y >= ymax)
1493  BADIMAGESEEK.error ("IMAGE::check_legal_access", ABORT, "(%d,%d)", x, y);
1494 }
Definition: errcode.h:30
const ERRCODE BADIMAGESEEK
Definition: imgerrs.h:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
const ERRCODE BADIMAGECOORDS
Definition: imgerrs.h:29
void IMAGE::convolver ( inT32  win_width,
inT32  win_height,
void(*)(uinT8 **pixels, uinT8 bytespp, inT32 win_wd, inT32 win_ht, uinT8 ret_white_value, uinT8 *result)  convolve 
)

Map function over window

Parameters
win_widthWindow width
win_heightWindow height
convolveConv function

Definition at line 1638 of file imgs.cpp.

1648  {
1649  IMAGELINE new_row; //Replacement pixels
1650  IMAGELINE *old_rows; //Rows being processed
1651  inT32 oldest_imline; //Next imline to replace
1652  uinT8 **window; //ptrs to pixel rows
1653  uinT8 **winmax; //ptrs to pixel rows
1654  uinT8 **win; //ptrs to pixel rows
1655  inT32 current_row; //Row being calculated
1656  inT32 current_col; //Col being calculated
1657  inT32 row = 0; //Next row to get
1658 
1659  inT32 i, j;
1660  uinT8 *pix;
1661  uinT8 *max;
1662  inT32 xmargin = win_width / 2;
1663  inT32 ymargin = win_height / 2;
1664  uinT8 white = get_white_level ();
1665  const uinT8 max_white = 255;
1666  float white_scale = (float) 255 / get_white_level ();
1667 
1668  if (((win_width % 2) == 0) ||
1669  ((win_height % 2) == 0) ||
1670  (win_height < 3) ||
1671  (win_width < 3) || (win_height > ysize / 2) || (win_width > xsize / 2))
1672  BADWINDOW.error ("IMAGE::convolver",
1673  ABORT, "(%d x %d)", win_width, win_height);
1674 
1675  new_row.init (xsize * bytespp);
1676  new_row.set_bpp (bpp);
1677  old_rows = new IMAGELINE[win_height];
1678  for (i = 0; i < win_height; i++) {
1679  old_rows[i].init ((xsize + 2 * xmargin) * bytespp);
1680  old_rows[i].set_bpp (bpp);
1681  }
1682 
1683  window = (uinT8 **) alloc_mem (win_height * sizeof (uinT8 *));
1684  winmax = window + win_height;
1685 
1686  /* Make bottom border */
1687  for (oldest_imline = 0; oldest_imline < ymargin; oldest_imline++) {
1688  pix = old_rows[oldest_imline].pixels;
1689  max = pix + (xsize + 2 * xmargin) * bytespp;
1690  while (pix < max)
1691  *pix++ = max_white;
1692  }
1693  /* Initialise remaining rows but one*/
1694  for (; oldest_imline < win_height - 1; oldest_imline++) {
1695  get_line (0, row++, xsize, &old_rows[oldest_imline], xmargin);
1696  if (max_white != white) {
1697  pix = old_rows[oldest_imline].pixels;
1698  max = pix + (xsize + 2 * xmargin) * bytespp;
1699  while (pix < max) {
1700  *pix = (uinT8) (*pix * white_scale);
1701  ++pix;
1702  }
1703  }
1704  }
1705 
1706  /* Image Processing */
1707 
1708  for (current_row = 0; current_row < ysize;) {
1709  /* Get next row and re-initialise window array */
1710  if (row < ysize) {
1711  get_line (0, row++, xsize, &old_rows[oldest_imline], xmargin);
1712  if (max_white != white) {
1713  pix = old_rows[oldest_imline].pixels;
1714  max = pix + (xsize + 2 * xmargin) * bytespp;
1715  while (pix < max) {
1716  *pix = (uinT8) (*pix * white_scale);
1717  ++pix;
1718  }
1719  }
1720  }
1721  else {
1722  pix = old_rows[oldest_imline].pixels;
1723  max = pix + (xsize + 2 * xmargin) * bytespp;
1724  while (pix < max)
1725  *pix++ = max_white;
1726  }
1727  oldest_imline++;
1728  if (oldest_imline >= win_height)
1729  oldest_imline = 0;
1730 
1731  /* Process line */
1732  pix = new_row.pixels;
1733  for (current_col = 0; current_col < xsize;) {
1734  /* Set up window ptrs */
1735  if (current_col == 0) {
1736  j = oldest_imline;
1737  for (i = 0; i < win_height; i++) {
1738  window[i] = old_rows[j++].pixels;
1739  if (j >= win_height)
1740  j = 0;
1741  }
1742  }
1743  else {
1744  for (win = window; win < winmax; (*win++) += bytespp);
1745  //Move along rows
1746  }
1747 
1748  convolve(window, bytespp, win_width, win_height, white, pix);
1749  pix += bytespp;
1750  current_col++;
1751  }
1752 
1753  put_line (0, current_row, xsize, &new_row, 0);
1754  new_row.init ();
1755  new_row.set_bpp (bpp);
1756  current_row++;
1757  }
1758 }
Definition: img.h:325
Definition: errcode.h:30
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
void * alloc_mem(inT32 count)
Definition: memry.cpp:48
void set_bpp(inT8 new_bpp)
Definition: img.h:362
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
uinT8 get_white_level()
Definition: img.h:121
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
uinT8 * pixels
image pixels
Definition: img.h:328
void get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1003
const ERRCODE BADWINDOW
Definition: imgerrs.h:34
unsigned char uinT8
Definition: host.h:99
inT8 IMAGE::create ( inT32  x,
inT32  y,
inT8  bits_per_pixel 
)

create blank image

Parameters
xx size required
yy size required
bits_per_pixelbpp required

Definition at line 121 of file imgs.cpp.

125  {
126  uinT8 *pixels; //memory for image
127 
128  xdim = check_legal_image_size (x, y, bits_per_pixel);
129  if (xdim < 0)
130  return -1;
131  pixels = (uinT8 *) alloc_big_zeros ((size_t) (xdim * y * sizeof (uinT8)));
132  if (pixels == NULL) {
133  MEMORY_OUT.error ("IMAGE::create", ABORT, "Size=(%d,%d)", xdim, y);
134  return -1;
135  }
136  //allocate to image
137  this->capture (pixels, x, y, bits_per_pixel);
138  captured = FALSE;
140  return 0; //success
141 }
Definition: errcode.h:30
#define NULL
Definition: host.h:144
inT8 capture(uinT8 *pixels, inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:170
#define FALSE
Definition: capi.h:28
inT32 check_legal_image_size(inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:240
void * alloc_big_zeros(inT32 count)
Definition: memry.cpp:52
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:41
EXTERN int image_default_resolution
Definition: imgs.cpp:64
unsigned char uinT8
Definition: host.h:99
const ERRCODE MEMORY_OUT
Definition: stderr.h:25
void IMAGE::destroy ( )

destroy image

Definition at line 150 of file imgs.cpp.

150  { //get rid of image
151  if (image != NULL && !captured) {
152  free_big_mem(image);
153  }
154  image = NULL;
155  if (fd >= 0) {
156  close(fd);
157  fd = -1;
158  }
159  bpp = 0;
160 }
#define NULL
Definition: host.h:144
void free_big_mem(void *oldchunk)
Definition: memry.cpp:60
void IMAGE::fast_get_line ( inT32  x,
inT32  y,
inT32  width,
IMAGELINE linebuf 
)

get image line

Parameters
xcoord to start at
yline to get
widthline to get
linebufline to copy to

Definition at line 978 of file imgs.cpp.

983  {
984  if (width > 0 && bpp > 4) {
985  check_legal_access(x, y, width);
986  //get pointer only
987  linebuf->pixels = image + xdim * (ymax - 1 - y) + x * bytespp;
988  }
989  else
990  //just copy it
991  this->get_line (x, y, width, linebuf, 0);
992  linebuf->bpp = bpp;
993 }
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 bpp
bits per pixel
Definition: img.h:329
void get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1003
void IMAGE::fast_put_line ( inT32  x,
inT32  y,
inT32  width,
IMAGELINE linebuf 
)

put image line

Parameters
xcoord to start at
yline to put
widthnumber of pixels to put
linebufline to copy from

Definition at line 1203 of file imgs.cpp.

1208  {
1209  if (width > 0 && (bpp <= 4 || linebuf->pixels == linebuf->line))
1210  //just copy it
1211  put_line (x, y, width, linebuf, 0);
1212 }
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
void IMAGE::FromPix ( const Pix *  src_pix)

Definition at line 1561 of file imgs.cpp.

1561  {
1562  // Leptonica doesn't const its inputs, but we don't change the input.
1563  Pix* pix = const_cast<Pix*>(src_pix);
1564  Pix* destroy_this_pix = NULL;
1565 
1566  int depth = pixGetDepth(pix);
1567  if (depth > 1 && depth < 8) {
1568  // Convert funny depths to 8 bit.
1569  destroy_this_pix = pixConvertTo8(pix, false);
1570  pix = destroy_this_pix;
1571  depth = pixGetDepth(pix);
1572  }
1573  int width = pixGetWidth(pix);
1574  int height = pixGetHeight(pix);
1575  const l_uint32* data = pixGetData(pix);
1576  this->create(width, height, depth == 32 ? 24 : depth);
1577  // For each line in the image, fill the IMAGELINE class and put it into the
1578  // destination image. Note that Tesseract stores images with the
1579  // bottom at y=0 and 0 is always black in grey and binary.
1580  IMAGELINE line;
1581  if (depth == 32) {
1582  line.init(width * 3);
1583  line.set_bpp(24);
1584  } else {
1585  line.init(width);
1586  }
1587  switch (depth) {
1588  case 1:
1589  // Binary images just flip the data bit.
1590  for (int y = height - 1 ; y >= 0; --y) {
1591  for (int x = 0; x < width; ++x)
1592  line.pixels[x] = GET_DATA_BIT((void *)data, x) ^ 1;
1593  this->put_line(0, y, width, &line, 0);
1594  data += pixGetWpl(pix);
1595  }
1596  break;
1597 
1598  case 8:
1599  // Greyscale just copies the bytes in the right order.
1600  for (int y = height - 1 ; y >= 0; --y) {
1601  for (int x = 0; x < width; ++x)
1602  line.pixels[x] = GET_DATA_BYTE((void *)data, x);
1603  this->put_line(0, y, width, &line, 0);
1604  data += pixGetWpl(pix);
1605  }
1606  break;
1607 
1608  case 32:
1609  // Put the colors in the correct places in the line buffer.
1610  for (int y = height - 1 ; y >= 0; --y) {
1611  for (int x = 0; x < width; ++x, ++data) {
1612  line[x][RED_PIX] = GET_DATA_BYTE((void *)data, COLOR_RED);
1613  line[x][GREEN_PIX] = GET_DATA_BYTE((void *)data, COLOR_GREEN);
1614  line[x][BLUE_PIX] = GET_DATA_BYTE((void *)data, COLOR_BLUE);
1615  }
1616  this->put_line(0, y, width, &line, 0);
1617  }
1618  break;
1619 
1620  default:
1621  tprintf("Cannot convert Pix to image with bpp = %d\n", depth);
1622  }
1623  if (destroy_this_pix != NULL)
1624  pixDestroy(&destroy_this_pix);
1625 }
Definition: img.h:43
Definition: img.h:45
Definition: img.h:325
#define NULL
Definition: host.h:144
void init(inT32 width)
Definition: img.h:343
Definition: img.h:44
void set_bpp(inT8 new_bpp)
Definition: img.h:362
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 create(inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:121
inT8 IMAGE::get_bpp ( )
inline

access function

Returns
bits per pixel

Definition at line 106 of file img.h.

106  {
107  return bpp;
108  }
inT8 IMAGE::get_bps ( )
inline

access function

Returns
bits per sample

Definition at line 113 of file img.h.

113  {
114  return bps;
115  }
uinT8* IMAGE::get_buffer ( )
inline

Definition at line 132 of file img.h.

132  {
133  return image;
134  }
void IMAGE::get_column ( inT32  x,
inT32  y,
inT32  height,
IMAGELINE linebuf,
inT32  margins 
)

get image column

Parameters
xcoord to start at
yline to get
heightnumber of pixels to get
linebufline to copy to
marginssize of margins

Definition at line 1105 of file imgs.cpp.

1111  {
1112  uinT8 *src; //source pointer
1113  uinT8 *dest; //destination pointer
1114  inT8 bit; //bit index
1115  inT8 pixperbyte; //pixels per byte
1116  uinT8 white; //white colour
1117  inT32 pixel; //pixel index
1118 
1119  //test coords
1120  this->check_legal_access (x, y, 1);
1121  //test coords
1122  this->check_legal_access (x, y + height - 1, 1);
1123  if (height > ysize - y)
1124  height = ysize - y; //clip to image
1125  linebuf->init (height * bytespp + margins * bytespp * 2);
1126  //start of line
1127  src = image + xdim * (ymax - 1 - y);
1128  dest = linebuf->line; //destination line
1129  linebuf->pixels = dest;
1130  white = (1 << bpp) - 1; //max value of pixel
1131  for (pixel = margins * bytespp; pixel > 0; pixel--) {
1132  *dest++ = white; //margins are white
1133  }
1134  if (height > 0) {
1135  if (bpp == 24) {
1136  src += x * bytespp; //offset
1137  for (; height > 0; --height) {
1138  *dest++ = *src; //copy bytes
1139  *dest++ = *(src + 1);
1140  *dest++ = *(src + 2);
1141  src -= xdim;
1142  }
1143  }
1144  else if (bpp > 4) {
1145  src += x;
1146  for (; height > 0; --height) {
1147  *dest++ = *src; //copy bytes
1148  src -= xdim;
1149  }
1150  }
1151  else if (bpp == 4) {
1152  src += x / 2; //offset on line
1153  if (x & 1) {
1154  for (; height > 0; --height) {
1155  //get coded nibble
1156  *dest++ = bpp4table[*src][1];
1157  src -= xdim;
1158  }
1159  }
1160  else {
1161  for (; height > 0; --height) {
1162  //get coded nibble
1163  *dest++ = bpp4table[*src][0];
1164  src -= xdim;
1165  }
1166  }
1167  }
1168  else if (bpp == 2) {
1169  pixperbyte = 4;
1170  src += x / 4; //offset on line
1171  bit = (inT8) (x % 4); //offset in byte
1172  for (; height > 0; --height) {
1173  //get coded bits
1174  *dest++ = bpp2table[*src][bit];
1175  src -= xdim;
1176  }
1177  }
1178  else {
1179  pixperbyte = 8;
1180  src += x / 8; //offset on line
1181  bit = (inT8) (x % 8); //offset in byte
1182  for (; height > 0; --height) {
1183  //get coded bits
1184  *dest++ = bpp1table[*src][bit];
1185  src -= xdim;
1186  }
1187  }
1188  }
1189  for (pixel = margins * bytespp; pixel > 0; pixel--) {
1190  *dest++ = white; //margins are white
1191  }
1192 }
const uinT8 bpp1table[256][8]
Definition: imgunpk.h:351
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
const uinT8 bpp2table[256][4]
Definition: imgunpk.h:92
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
const uinT8 bpp4table[256][2]
Definition: imgunpk.h:25
void IMAGE::get_line ( inT32  x,
inT32  y,
inT32  width,
IMAGELINE linebuf,
inT32  margins 
)

get image line

Parameters
xcoord to start at
yline to get
widthline to get
linebufline to copy to
marginssize of margins

Definition at line 1003 of file imgs.cpp.

1009  {
1010  uinT8 *src; // source pointer
1011  uinT8 *dest; // destination pointer
1012  const uinT8 *unpacksrc; // unpacking pointer
1013  inT8 bit; // bit index
1014  inT8 pixperbyte; // pixels per byte
1015  uinT8 white; // white colour
1016  inT32 pixel; // pixel index
1017 
1018  this->check_legal_access (x, y, width);
1019  if (width > xsize - x)
1020  width = xsize - x; //clip to image
1021  width *= bytespp;
1022  linebuf->init (width + margins * bytespp * 2);
1023  linebuf->bpp = bpp;
1024  //start of line
1025  src = image + xdim * (ymax - 1 - y);
1026  dest = linebuf->line; //destination line
1027  linebuf->pixels = dest;
1028  white = (1 << bpp) - 1; //max value of pixel
1029  for (pixel = margins * bytespp; pixel > 0; pixel--) {
1030  *dest++ = white; //margins are white
1031  }
1032  if (width > 0) {
1033  if (bpp > 4) {
1034  src += x; //offset
1035  //easy way
1036  memmove (dest, src, (unsigned) width);
1037  }
1038  else if (bpp == 4) {
1039  src += x / 2; //offset on line
1040  if (x & 1) {
1041  //get coded nibble
1042  *dest++ = bpp4table[*src++][1];
1043  width--;
1044  }
1045  while (width >= 2) {
1046  //get coded bits
1047  unpacksrc = bpp4table[*src++];
1048  *dest++ = *unpacksrc++;
1049  *dest++ = *unpacksrc++; //copy nibbles
1050  width -= 2;
1051  }
1052  if (width) {
1053  //get coded nibble
1054  *dest++ = bpp4table[*src++][0];
1055  }
1056  }
1057  else if (bpp == 2) {
1058  pixperbyte = 4;
1059  src += x / 4; //offset on line
1060  bit = (inT8) (x % 4); //offset in byte
1061  width += bit;
1062  while (width > 0) { //until all done
1063  if (width < pixperbyte)
1064  //less on last byte
1065  pixperbyte = (inT8) width;
1066  //get coded bits
1067  unpacksrc = &bpp2table[*src++][bit];
1068  for (; bit < pixperbyte; bit++)
1069  *dest++ = *unpacksrc++;//copy bytes
1070  width -= pixperbyte;
1071  bit = 0;
1072  }
1073  }
1074  else {
1075  pixperbyte = 8;
1076  src += x / 8; //offset on line
1077  bit = (inT8) (x % 8); //offset in byte
1078  width += bit;
1079  while (width > 0) { //until all done
1080  if (width < pixperbyte)
1081  //less on last byte
1082  pixperbyte = (inT8) width;
1083  //get coded bits
1084  unpacksrc = &bpp1table[*src++][bit];
1085  for (; bit < pixperbyte; bit++)
1086  *dest++ = *unpacksrc++;//copy bytes
1087  width -= pixperbyte;
1088  bit = 0;
1089  }
1090  }
1091  }
1092  for (pixel = margins * bytespp; pixel > 0; pixel--) {
1093  *dest++ = white; //margins are white
1094  }
1095 }
const uinT8 bpp1table[256][8]
Definition: imgunpk.h:351
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
const uinT8 bpp2table[256][4]
Definition: imgunpk.h:92
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 bpp
bits per pixel
Definition: img.h:329
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
const uinT8 bpp4table[256][2]
Definition: imgunpk.h:25
inT32 IMAGE::get_res ( )
inline

get resolution

Definition at line 125 of file img.h.

125  {
126  return res;
127  }
uinT8 IMAGE::get_white_level ( )
inline

access function

Definition at line 121 of file img.h.

121  {
122  return (1 << bpp) - 1;
123  }
inT32 IMAGE::get_xsize ( )
inline

access function

Returns
xsize

Definition at line 92 of file img.h.

92  {
93  return xsize;
94  }
inT32 IMAGE::get_ysize ( )
inline

access function

Returns
ysize

Definition at line 99 of file img.h.

99  {
100  return ysize;
101  }
IMAGE & IMAGE::operator= ( IMAGE source)

Definition at line 87 of file imgs.cpp.

89  {
90  destroy();
91  bpp = source.bpp;
92  photo_interp = source.photo_interp;
93  bps = source.bps;
94  bytespp = (bpp + 7) / 8;
95  lineskip = source.lineskip; //copy everything
96  captured = source.captured;
97  xsize = source.xsize;
98  ysize = source.ysize;
99  res = source.res;
100  image = source.image;
101  xdim = source.xdim;
102  bufheight = source.bufheight;
103  fd = source.fd;
104  reader = source.reader;
105  ymin = source.ymin;
106  ymax = source.ymax;
107 
108  source.captured = TRUE; //source now captured
109  source.fd = -1;
110 
111  return *this;
112 }
void destroy()
Definition: imgs.cpp:150
#define TRUE
Definition: capi.h:27
uinT8 IMAGE::pixel ( inT32  x,
inT32  y 
)

access pixel

Parameters
xcoord
ycoord

Definition at line 202 of file imgs.cpp.

205  {
206  if (x < 0)
207  x = 0; //silently clip
208  else if (x >= xsize)
209  x = xsize - 1;
210  if (y < 0)
211  y = 0;
212  else if (y >= ysize)
213  y = ysize - 1;
214  check_legal_access (x, y, 1);
215  switch (bpp) {
216  case 5:
217  case 6:
218  case 8:
219  return image[(ymax - 1 - y) * xdim + x];
220  case 4:
221  return bpp4table[image[(ymax - 1 - y) * xdim + x / 2]][x & 1];
222  case 2:
223  return bpp2table[image[(ymax - 1 - y) * xdim + x / 4]][x & 3];
224  case 1:
225  return bpp1table[image[(ymax - 1 - y) * xdim + x / 8]][x & 7];
226  default:
227  tprintf ("Unexpected bits per pixel %d\n", bpp);
228  return 0;
229  }
230 }
const uinT8 bpp1table[256][8]
Definition: imgunpk.h:351
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
const uinT8 bpp2table[256][4]
Definition: imgunpk.h:92
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
const uinT8 bpp4table[256][2]
Definition: imgunpk.h:25
void IMAGE::put_column ( inT32  x,
inT32  y,
inT32  height,
IMAGELINE linebuf,
inT32  margins 
)

put image column

Parameters
xcoord to start at
yline to put
heightnumber of pixels to put
linebufline to copy to
marginssize of margins

Definition at line 1371 of file imgs.cpp.

1377  {
1378  uinT8 *src; //source pointer
1379  uinT8 *dest; //destination pointer
1380  inT8 bit; //bit index
1381  uinT8 pixel; //collected bits
1382  inT8 bytesperpix; //in source
1383 
1384  this->check_legal_access (x, y, 1);
1385  this->check_legal_access (x, y + height - 1, 1);
1386  if (height > ysize - y)
1387  height = ysize - y; //clip to image
1388  if (height <= 0)
1389  return; //nothing to do
1390  //source line
1391  src = linebuf->pixels + margins;
1392  //start of line
1393  dest = image + xdim * (ymax - 1 - y);
1394 
1395  if (linebuf->bpp == 24) {
1396  src++;
1397  bytesperpix = 3;
1398  }
1399  else
1400  bytesperpix = 1;
1401 
1402  if (bpp == 24 && linebuf->bpp == 24) {
1403  dest += x * bytesperpix;
1404  src--;
1405  for (; height > 0; --height) {
1406  *dest = *src++;
1407  *(dest + 1) = *src++;
1408  *(dest + 2) = *src++;
1409  dest -= xdim;
1410  }
1411  }
1412  else if (bpp == 24) {
1413  src--;
1414  dest += x * bytesperpix;
1415  for (; height > 0; --height) {
1416  pixel = *src++;
1417  *dest = pixel;
1418  *(dest + 1) = pixel;
1419  *(dest + 2) = pixel;
1420  dest -= xdim;
1421  }
1422  }
1423  else if (bpp > 4) {
1424  dest += x; //offset
1425  for (; height > 0; --height) {
1426  *dest = *src;
1427  src += bytesperpix;
1428  dest -= xdim;
1429  }
1430  }
1431  else if (bpp == 4) {
1432  dest += x / 2; //offset on line
1433  if (x & 1) {
1434  for (; height > 0; --height) {
1435  *dest &= 0xf0; //clean odd byte
1436  *dest |= *src & 0x0f; //and copy it
1437  src += bytesperpix;
1438  dest -= xdim;
1439  }
1440  }
1441  else {
1442  for (; height > 0; --height) {
1443  *dest &= 0x0f; //clean odd byte
1444  *dest |= *src << 4;
1445  src += bytesperpix;
1446  dest -= xdim;
1447  }
1448  }
1449  }
1450  else if (bpp == 2) {
1451  dest += x / 4; //offset on line
1452  bit = (inT8) (x % 4); //offset in byte
1453  bit = 6 - bit - bit; //bit shift
1454  pixel = ~(3 << bit); //mask
1455  for (; height > 0; --height) {
1456  //change 2 bits
1457  *dest = (*dest & pixel) | ((*src & 3) << bit);
1458  src += bytesperpix;
1459  dest -= xdim;
1460  }
1461  }
1462  else {
1463  dest += x / 8; //offset on line
1464  bit = (inT8) (x % 8); //offset in byte
1465  bit = 7 - bit;
1466  pixel = ~(1 << bit);
1467  for (; height > 0; --height) {
1468  //change 1 bit
1469  *dest = (*dest & pixel) | ((*src & 1) << bit);
1470  src += bytesperpix;
1471  dest -= xdim;
1472  }
1473  }
1474 }
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 bpp
bits per pixel
Definition: img.h:329
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
void IMAGE::put_line ( inT32  x,
inT32  y,
inT32  width,
IMAGELINE linebuf,
inT32  margins 
)

put image line

Parameters
xcoord to start at
yline to put
widthnumber of pixels to put
linebufline to copy from
marginssize of margins

Definition at line 1222 of file imgs.cpp.

1228  {
1229  uinT8 *src; //source pointer
1230  uinT8 *dest; //destination pointer
1231  inT8 bit; //bit index
1232  uinT8 pixel; //collected bits
1233  inT8 pixperbyte; //pixels in a byte
1234  inT8 bytesperpix; //in source
1235 
1236  this->check_legal_access (x, y, width);
1237  if (width > xsize - x)
1238  width = xsize - x; //clip to image
1239  if (width <= 0)
1240  return; //nothing to do
1241  //source line
1242  src = linebuf->pixels + margins;
1243  //start of line
1244  dest = image + xdim * (ymax - 1 - y);
1245 
1246  if (linebuf->bpp == 24) {
1247  src++;
1248  bytesperpix = 3;
1249  }
1250  else
1251  bytesperpix = 1;
1252  if (bpp == 24 && linebuf->bpp == 24) {
1253  dest += x * bytespp;
1254  width *= bytespp;
1255  memmove (dest, src - 1, (unsigned) width);
1256  }
1257  else if (bpp == 24) {
1258  src--;
1259  dest += x * bytespp;
1260  while (width > 0) {
1261  pixel = *src++;
1262  *dest++ = pixel;
1263  *dest++ = pixel;
1264  *dest++ = pixel;
1265  width--;
1266  }
1267  }
1268  else if (bpp > 4) {
1269  dest += x; //offset
1270  if (linebuf->bpp == 24) {
1271  while (width > 0) {
1272  *dest++ = *src;
1273  src += 3;
1274  width--;
1275  }
1276  }
1277  else
1278  //easy way
1279  memmove (dest, src, (unsigned) width);
1280  }
1281  else if (bpp == 4) {
1282  dest += x / 2; //offset on line
1283  if (x & 1) {
1284  *dest &= 0xf0; //clean odd byte
1285  *dest++ |= *src & 0x0f; //and copy it
1286  src += bytesperpix;
1287  width--;
1288  }
1289  while (width >= 2) {
1290  pixel = *src << 4; //left pixel
1291  src += bytesperpix;
1292  pixel |= *src & 0x0f; //right pixel
1293  src += bytesperpix;
1294  *dest++ = pixel;
1295  width -= 2;
1296  }
1297  if (width) {
1298  *dest &= 0x0f; //clean odd byte
1299  *dest |= *src << 4;
1300  }
1301  }
1302  else if (bpp == 2) {
1303  pixperbyte = 4;
1304  dest += x / 4; //offset on line
1305  bit = (inT8) (x % 4); //offset in byte
1306  width += bit;
1307  pixel = *dest >> (8 - bit - bit);
1308  while (width >= 4) { //until all done
1309  for (; bit < 4; bit++) {
1310  pixel <<= 2; //make space for new one
1311  pixel |= *src & 3;
1312  src += bytesperpix;
1313  }
1314  *dest++ = pixel; //new pixel
1315  width -= 4;
1316  bit = 0;
1317  }
1318  if (width > 0) { //until all done
1319  for (bit = 0; bit < width; bit++) {
1320  pixel <<= 2; //make space for new one
1321  pixel |= *src & 3;
1322  src += bytesperpix;
1323  }
1324  pixel <<= (8 - bit - bit); //shift rest
1325  //keep trainling bits
1326  pixel |= *dest & ((1 << (8 - bit - bit)) - 1);
1327  *dest++ = pixel; //new pixel
1328  }
1329  }
1330  else {
1331  pixperbyte = 8;
1332  dest += x / 8; //offset on line
1333  bit = (inT8) (x % 8); //offset in byte
1334  width += bit;
1335  pixel = *dest >> (8 - bit);
1336  while (width >= 8) { //until all done
1337  for (; bit < 8; bit++) {
1338  pixel <<= 1; //make space for new one
1339  pixel |= *src & 1;
1340  src += bytesperpix;
1341  }
1342  *dest++ = pixel; //new pixel
1343  width -= 8;
1344  bit = 0;
1345  }
1346  width -= bit;
1347  if (width > 0) { //until all done
1348  while (width > 0) {
1349  pixel <<= 1; //make space for new one
1350  pixel |= *src & 1;
1351  src += bytesperpix;
1352  bit++;
1353  width--;
1354  }
1355  pixel <<= (8 - bit); //shift rest
1356  //keep trainling bits
1357  pixel |= *dest & ((1 << (8 - bit)) - 1);
1358  *dest++ = pixel; //new pixel
1359  }
1360  }
1361 }
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 bpp
bits per pixel
Definition: img.h:329
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
void IMAGE::set_res ( inT32  resolution)
inline

set resolution

Definition at line 129 of file img.h.

129  {
130  res = resolution;
131  }
Pix * IMAGE::ToPix ( )

Methods to convert image types. Only available if Leptonica is available.

Definition at line 1501 of file imgs.cpp.

1501  {
1502  int width = this->get_xsize();
1503  int height = this->get_ysize();
1504  int bpp = this->get_bpp();
1505  Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
1506  l_uint32* data = pixGetData(pix);
1507  IMAGELINE line;
1508  if (bpp == 24) {
1509  line.init(width * 3);
1510  line.set_bpp(24);
1511  } else {
1512  line.init(width);
1513  }
1514  switch (bpp) {
1515  case 1:
1516  for (int y = height - 1 ; y >= 0; --y) {
1517  this->get_line(0, y, width, &line, 0);
1518  for (int x = 0; x < width; ++x) {
1519  if (line.pixels[x])
1520  CLEAR_DATA_BIT(data, x);
1521  else
1522  SET_DATA_BIT(data, x);
1523  }
1524  data += pixGetWpl(pix);
1525  }
1526  break;
1527 
1528  case 8:
1529  // Greyscale just copies the bytes in the right order.
1530  for (int y = height - 1 ; y >= 0; --y) {
1531  this->get_line(0, y, width, &line, 0);
1532  for (int x = 0; x < width; ++x)
1533  SET_DATA_BYTE(data, x, line.pixels[x]);
1534  data += pixGetWpl(pix);
1535  }
1536  break;
1537 
1538  case 24:
1539  // Put the colors in the correct places in the line buffer.
1540  for (int y = height - 1 ; y >= 0; --y) {
1541  this->get_line(0, y, width, &line, 0);
1542  for (int x = 0; x < width; ++x, ++data) {
1543  SET_DATA_BYTE(data, COLOR_RED, line[x][RED_PIX]);
1544  SET_DATA_BYTE(data, COLOR_GREEN, line[x][GREEN_PIX]);
1545  SET_DATA_BYTE(data, COLOR_BLUE, line[x][BLUE_PIX]);
1546  }
1547  }
1548  break;
1549 
1550  default:
1551  tprintf("Cannot convert image to Pix with bpp = %d\n", bpp);
1552  }
1553  return pix;
1554 }
Definition: img.h:43
Definition: img.h:45
Definition: img.h:325
void init(inT32 width)
Definition: img.h:343
Definition: img.h:44
void set_bpp(inT8 new_bpp)
Definition: img.h:362
inT32 get_ysize()
Definition: img.h:99
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
uinT8 * pixels
image pixels
Definition: img.h:328
void get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1003
inT8 get_bpp()
Definition: img.h:106
inT32 get_xsize()
Definition: img.h:92
BOOL8 IMAGE::white_high ( )
inline

photo interp

Definition at line 117 of file img.h.

117  {
118  return photo_interp;
119  }

Friends And Related Function Documentation

DLLSYM void copy_sub_image ( IMAGE source,
inT32  xstart,
inT32  ystart,
inT32  xext,
inT32  yext,
IMAGE dest,
inT32  xdest,
inT32  ydest,
BOOL8  adjust_grey 
)
friend

Definition at line 270 of file imgs.cpp.

280  {
281  IMAGELINE copyline; //copy of line
282  uinT8 *copy; //source pointer
283  inT8 shift; //shift factor
284  inT32 pixel; //pixel index
285  inT32 y; //line index
286  inT32 yoffset; //current adjusted offset
287  inT32 bytesize; //no of bytes to copy
288  inT32 srcppb; //pixels per byte
289  BOOL8 aligned;
290 
291  if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
292  return;
293  if (xext <= 0)
294  xext = source->xsize; //default to all
295  if (xext > source->xsize - xstart)
296  //clip to smallest
297  xext = source->xsize - xstart;
298  if (xext > dest->xsize - xdest)
299  xext = dest->xsize - xdest;
300  if (yext <= 0)
301  yext = source->ysize; //default to all
302  if (yext > source->ysize - ystart)
303  //clip to smallest
304  yext = source->ysize - ystart;
305  if (yext > dest->ysize - ydest)
306  yext = dest->ysize - ydest;
307  if (xext <= 0 || yext <= 0)
308  return; //nothing to do
309 
310  srcppb = 8 / source->bpp; //pixels per byte
311  if (source->bpp == dest->bpp || !adjust_grey)
312  shift = 0; //no adjustment
313  else {
314  shift = source->bps - dest->bps;
315  if (shift < 0)
316  shift = -shift; //keep positive
317  }
318  aligned = source->bpp == dest->bpp;
319  if (aligned && srcppb != 0) {
320  aligned = xstart % srcppb == 0
321  && xdest % srcppb == 0
322  && (xext % srcppb == 0 || xdest + xext == dest->xsize);
323  }
324  for (y = 0; y < yext; y++) {
325  if (ystart >= ydest)
326  yoffset = y; //top down
327  else
328  yoffset = yext - y - 1; //bottom up
329  source->check_legal_access (xstart, ystart + yoffset, xext);
330  dest->check_legal_access (xdest, ydest + yoffset, xext);
331  if (aligned) {
332  bytesize = COMPUTE_IMAGE_XDIM (xext, source->bpp);
333  //get bytes per line
334  if (srcppb == 0)
335  //do cheap move
336  memmove (dest->image + (dest->ymax - 1 - ydest - yoffset) * dest->xdim + xdest * 3, source->image + (source->ymax - 1 - ystart - yoffset) * source->xdim + xstart * 3, (unsigned) bytesize);
337  else
338  //do cheap move
339  memmove (dest->image + (dest->ymax - 1 - ydest - yoffset) * dest->xdim + xdest / srcppb, source->image + (source->ymax - 1 - ystart - yoffset) * source->xdim + xstart / srcppb, (unsigned) bytesize);
340  }
341  else {
342  if (shift == 0) {
343  source->fast_get_line (xstart, ystart + yoffset, xext,
344  &copyline);
345  }
346  else if (source->bpp < dest->bpp) {
347  source->get_line (xstart, ystart + yoffset, xext, &copyline, 0);
348  if (source->bpp <= shift
349  && (source->bpp == 1 || source->bpp == 4)) {
350  if (source->bpp == 1) {
351  for (pixel = 0, copy = copyline.pixels; pixel < xext;
352  pixel++, copy++)
353  if (*copy)
354  *copy = 0xff;
355  }
356  else {
357  for (pixel = 0, copy = copyline.pixels; pixel < xext;
358  pixel++, copy++)
359  //scale up
360  *copy = (*copy << shift) | *copy;
361  }
362  }
363  else {
364  for (pixel = 0, copy = copyline.pixels; pixel < xext;
365  pixel++)
366  *copy++ <<= shift; //scale up
367  }
368  }
369  else {
370  source->get_line (xstart, ystart + yoffset, xext, &copyline, 0);
371  if (source->bpp == 24) {
372  for (pixel = 0, copy = copyline.pixels + 1; pixel < xext;
373  pixel++) {
374  *copy >>= shift;
375  copy += 3;
376  }
377  }
378  else {
379  for (pixel = 0, copy = copyline.pixels; pixel < xext;
380  pixel++)
381  *copy++ >>= shift; //scale down
382  }
383  }
384  dest->put_line (xdest, ydest + yoffset, xext, &copyline, 0);
385  }
386  }
387 }
Definition: img.h:325
unsigned char BOOL8
Definition: host.h:113
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
int inT32
Definition: host.h:102
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
#define COMPUTE_IMAGE_XDIM(xsize, bpp)
Definition: img.h:33
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
SIGNED char inT8
Definition: host.h:98
void get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1003
unsigned char uinT8
Definition: host.h:99
void fast_get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
Definition: imgs.cpp:978
DLLSYM void enlarge_sub_image ( IMAGE source,
inT32  xstart,
inT32  ystart,
IMAGE dest,
inT32  xdest,
inT32  ydest,
inT32  xext,
inT32  yext,
inT32  scale,
BOOL8  adjust_grey 
)
friend

Definition at line 398 of file imgs.cpp.

409  {
410  inT8 shift; //shift factor
411  uinT8 pixel; //current pixel
412  inT32 srcext; //source extent
413  inT32 xoffset; //column index
414  inT32 yoffset; //line index
415  inT32 xindex, yindex; //index in super pixel
416  inT32 startxindex; //initial x index
417  inT32 xscale; //x scale factor
418  uinT8 *src; //source pixels
419  uinT8 *destpix; //dest pixels
420  IMAGELINE copyline; //copy of line
421  IMAGELINE bigline; //expanded line
422 
423  if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
424  return;
425 
426  if (xext <= 0)
427  xext = dest->xsize; //default to all
428  if (xext > source->xsize * scale - xstart)
429  //clip to smallest
430  xext = source->xsize * scale - xstart;
431  if (xext > dest->xsize - xdest)
432  xext = dest->xsize - xdest;
433  if (yext <= 0)
434  yext = dest->ysize; //default to all
435  if (yext > source->ysize * scale - ystart)
436  yext = source->ysize * scale - ystart;
437  if (yext > dest->ysize - ydest)
438  yext = dest->ysize - ydest;
439  if (xext <= 0 || yext <= 0)
440  return; //nothing to do
441 
442  xindex = xstart % scale; //offset in super pixel
443  startxindex = xindex;
444  yindex = ystart % scale;
445  //no of source pixels
446  srcext = (xext + xindex + scale - 1) / scale;
447  xstart /= scale; //actual start
448  ystart /= scale;
449  if (adjust_grey) {
450  shift = dest->bps - source->bps;
451  }
452  else
453  shift = 0; //no adjustment
454  bigline.init (xext * 3);
455  bigline.bpp = dest->bpp == 24 ? source->bpp : dest->bpp;
456 
457  for (yoffset = 0; yoffset < yext; ystart++) {
458  source->check_legal_access (xstart, ystart, srcext);
459  dest->check_legal_access (xdest, ydest + yoffset, xext);
460  source->fast_get_line (xstart, ystart, srcext, &copyline);
461  src = copyline.pixels;
462  destpix = bigline.pixels;
463  xscale = scale; //enlargement factor
464  if (source->bpp == 24 && dest->bpp == 24) {
465  for (xoffset = 0, xindex = startxindex; xoffset < xext;
466  src += source->bytespp) {
467  xoffset += xscale - xindex;
468  if (xoffset > xext)
469  xscale -= xoffset - xext;
470  for (; xindex < xscale; xindex++) {
471  *destpix++ = *src;
472  *destpix++ = *(src + 1);
473  *destpix++ = *(src + 2);
474  }
475  xindex = 0;
476  }
477  }
478  else {
479  if (source->bpp == 24)
480  src++;
481  for (xoffset = 0, xindex = startxindex; xoffset < xext;
482  src += source->bytespp) {
483  xoffset += xscale - xindex;
484  if (xoffset > xext)
485  //clip to dest limit
486  xscale -= xoffset - xext;
487  if (shift == 0)
488  pixel = *src;
489  else if (shift > 0)
490  pixel = *src << shift;
491  else
492  pixel = *src >> (-shift);
493  for (; xindex < xscale; xindex++)
494  *destpix++ = pixel; //duplicate pixel
495  xindex = 0;
496  }
497  }
498  for (; yoffset < yext && yindex < scale; yindex++, yoffset++) {
499  dest->put_line (xdest, ydest + yoffset, xext, &bigline, 0);
500  }
501  yindex = 0;
502  }
503 }
Definition: img.h:325
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 bpp
bits per pixel
Definition: img.h:329
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
void fast_get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
Definition: imgs.cpp:978
DLLSYM void fast_reduce_sub_image ( IMAGE source,
inT32  xstart,
inT32  ystart,
inT32  xext,
inT32  yext,
IMAGE dest,
inT32  xdest,
inT32  ydest,
inT32  scale,
BOOL8  adjust_grey 
)
friend

Definition at line 516 of file imgs.cpp.

527  {
528  inT8 shift; //shift factor
529  inT32 xfactor; //run on x coord
530  inT32 divisor; //total cell area
531  inT32 xindex, yindex; //into averaging square
532  inT32 xcoord; //current x coord
533  inT32 destext; //destination size
534  inT32 yoffset; //current adjusted offset
535  uinT8 *pixel; //ptr to source pixels
536  inT32 *sums; //ptr to sums array
537  IMAGELINE copyline; //copy of line
538  inT32 *linesums; //averaging sums
539 
540  if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
541  return;
542  if (xext <= 0)
543  xext = source->xsize; //default to all
544  if (xext > source->xsize - xstart)
545  //clip to smallest
546  xext = source->xsize - xstart;
547  if (xext > (dest->xsize - xdest) * scale)
548  xext = (dest->xsize - xdest) * scale;
549  if (yext <= 0)
550  yext = source->ysize; //default to all
551  if (yext > source->ysize - ystart)
552  //clip to smallest
553  yext = source->ysize - ystart;
554  if (yext > (dest->ysize - ydest) * scale)
555  yext = (dest->ysize - ydest) * scale;
556  if (xext <= 0 || yext <= 0)
557  return; //nothing to do
558 
559  xfactor = xext % scale; //left overs
560  if (xfactor == 0)
561  xfactor = scale;
562  //destination pixels
563  destext = (xext + scale - 1) / scale;
564  if (adjust_grey)
565  //shift factor
566  shift = dest->bps - source->bps;
567  else
568  shift = 0; //no adjustment
569  linesums = new inT32[destext * source->bytespp];
570 
571  for (yoffset = 0; yoffset < yext; ydest++) {
572  source->check_legal_access (xstart, ystart + yoffset, xext);
573  dest->check_legal_access (xdest, ydest, destext);
574  for (xindex = destext * source->bytespp - 1; xindex >= 0; xindex--)
575  linesums[xindex] = 0; //zero sums
576  for (yindex = 0; yindex < scale
577  && ystart + yoffset < source->ysize; yindex += 3) {
578  source->fast_get_line (xstart, ystart + yoffset, xext, &copyline);
579  pixel = copyline.pixels; //start of line
580  if (source->bpp == 24) {
581  for (xcoord = 1, sums = linesums; xcoord < destext;
582  xcoord++, sums += 3) {
583  for (xindex = 0; xindex < scale; xindex += 2) {
584  *sums += *pixel++;
585  *(sums + 1) += *pixel++;
586  *(sums + 2) += *pixel++;
587  pixel += 3;
588  }
589  if (scale & 1)
590  pixel -= 3; //correct position
591  }
592  for (xindex = 0; xindex < xfactor; xindex += 2) {
593  *sums += *pixel++;
594  *(sums + 1) += *pixel++;
595  *(sums + 2) += *pixel++;
596  pixel += 3;
597  }
598  }
599  else {
600  for (xcoord = 1, sums = linesums; xcoord < destext;
601  xcoord++, sums++) {
602  for (xindex = 0; xindex < scale; xindex += 2) {
603  *sums += *pixel;
604  pixel += 2;
605  }
606  if (scale & 1)
607  pixel--; //correct position
608  }
609  for (xindex = 0; xindex < xfactor; xindex += 2) {
610  *sums += *pixel;
611  pixel += 2;
612  }
613  }
614  yoffset += 3; //every 3 lines
615  }
616  if (yindex > scale)
617  yoffset -= yindex - scale; //back on right scale
618  copyline.init (); //set pixels back to array
619  copyline.bpp = source->bpp;
620  pixel = copyline.pixels;
621  //pixels in block
622  divisor = ((yindex + 2) / 3) * ((scale + 1) / 2);
623  if (shift <= 0) {
624  divisor <<= (-shift); //do greyscale correction
625  for (sums = linesums, xindex = (destext - 1) * source->bytespp;
626  xindex > 0; xindex--)
627  //turn to destination value
628  *pixel++ = (uinT8) (*sums++ / divisor);
629  for (xindex = source->bytespp; xindex > 0; xindex--)
630  *pixel++ = *sums++
631  / (((yindex + 2) / 3) * ((xfactor + 1) / 2) << (-shift));
632  //lastone different
633  }
634  else {
635  for (sums = linesums, xindex = (destext - 1) * source->bytespp;
636  xindex > 0; xindex--)
637  *pixel++ = (uinT8) ((*sums++ << shift) / divisor);
638  //destination value
639  for (xindex = source->bytespp; xindex > 0; xindex--)
640  //last one different
641  *pixel++ = (*(sums++) << shift) / (((yindex + 2) / 3) * ((xfactor + 1) / 2));
642  }
643  //put in destination
644  dest->put_line (xdest, ydest, destext, &copyline, 0);
645  }
646  delete [] linesums;
647 }
Definition: img.h:325
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
uinT8 * pixels
image pixels
Definition: img.h:328
inT8 bpp
bits per pixel
Definition: img.h:329
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
void fast_get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
Definition: imgs.cpp:978
DLLSYM void reduce_sub_image ( IMAGE source,
inT32  xstart,
inT32  ystart,
inT32  xext,
inT32  yext,
IMAGE dest,
inT32  xdest,
inT32  ydest,
inT32  scale,
BOOL8  adjust_grey 
)
friend

Definition at line 658 of file imgs.cpp.

669  {
670  inT8 shift; //shift factor
671  inT32 xfactor; //run on x coord
672  inT32 divisor; //total cell area
673  inT32 div2; //total cell area divided by 2
674  inT32 xindex, yindex; //into averaging square
675  inT32 xcoord; //current x coord
676  inT32 destext; //destination size
677  inT32 yoffset; //current adjusted offset
678  uinT8 *pixel; //ptr to source pixels
679  inT32 *sums; //ptr to sums array
680  IMAGELINE copyline; //copy of line
681  inT32 *linesums; //averaging sums
682 
683  if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
684  return;
685  if (xext <= 0)
686  xext = source->xsize; //default to all
687  if (xext > source->xsize - xstart)
688  //clip to smallest
689  xext = source->xsize - xstart;
690  if (xext > (dest->xsize - xdest) * scale)
691  xext = (dest->xsize - xdest) * scale;
692  if (yext <= 0)
693  yext = source->ysize; //default to all
694  if (yext > source->ysize - ystart)
695  //clip to smallest
696  yext = source->ysize - ystart;
697  if (yext > (dest->ysize - ydest) * scale)
698  yext = (dest->ysize - ydest) * scale;
699  if (xext <= 0 || yext <= 0)
700  return; //nothing to do
701 
702  xfactor = xext % scale; //left overs
703  if (xfactor == 0)
704  xfactor = scale;
705  //destination pixels
706  destext = (xext + scale - 1) / scale;
707  if (adjust_grey)
708  //shift factor
709  shift = dest->bps - source->bps;
710  else
711  shift = 0; //no adjustment
712  linesums = new inT32[destext * source->bytespp];
713 
714  for (yoffset = 0; yoffset < yext; ydest++) {
715  source->check_legal_access (xstart, ystart + yoffset, xext);
716  dest->check_legal_access (xdest, ydest, destext);
717  for (xindex = 0; xindex < (destext) * source->bytespp; xindex++)
718  linesums[xindex] = 0; //zero sums
719  for (yindex = 0; yindex < scale && ystart + yoffset < source->ysize;
720  yindex++) {
721  source->fast_get_line (xstart, ystart + yoffset, xext, &copyline);
722  pixel = copyline.pixels; //start of line
723  if (source->bpp == 24) {
724  for (xcoord = 1, sums = linesums; xcoord < destext;
725  xcoord++, sums += 3) {
726  for (xindex = 0; xindex < scale; xindex++) {
727  *sums += *pixel++;
728  *(sums + 1) += *pixel++;
729  *(sums + 2) += *pixel++;
730  }
731  }
732  for (xindex = 0; xindex < xfactor; xindex++) {
733  *sums += *pixel++;
734  *(sums + 1) += *pixel++;
735  *(sums + 2) += *pixel++;
736  }
737  }
738  else {
739  for (xcoord = 1, sums = linesums; xcoord < destext;
740  xcoord++, sums++) {
741  for (xindex = 0; xindex < scale; xindex++)
742  *sums += *pixel++;
743  }
744  for (xindex = 0; xindex < xfactor; xindex++)
745  *sums += *pixel++;
746  }
747  yoffset++; //next line
748  }
749  copyline.init (); //set pixels back to array
750  copyline.set_bpp (source->bpp);
751  pixel = copyline.pixels;
752  divisor = yindex * scale;
753  if (divisor == 0) {
754  tprintf
755  ("Impossible:divisor=0!, yindex=%d, scale=%d, yoffset=%d,yext=%d\n",
756  yindex, scale, yoffset, yext);
757  break;
758  }
759  if (shift <= 0) {
760  divisor <<= (-shift); //do greyscale correction
761  div2 = divisor / 2;
762  for (sums = linesums, xindex = (destext - 1) * source->bytespp;
763  xindex > 0; xindex--)
764  *pixel++ = (uinT8) ((div2 + *sums++) / divisor);
765  //turn to destination value
766  div2 = (yindex * xfactor << (-shift)) / 2;
767  for (xindex = source->bytespp; xindex > 0; xindex--)
768  *pixel++ =
769  (uinT8) ((div2 + *sums++) / (yindex * xfactor << (-shift)));
770  //lastone different
771  }
772  else {
773  div2 = divisor / 2;
774  for (sums = linesums, xindex = (destext - 1) * source->bytespp;
775  xindex > 0; xindex--)
776  *pixel++ = (uinT8) ((div2 + (*sums++ << shift)) / divisor);
777  //destination value
778  div2 = (yindex * xfactor) / 2;
779  for (xindex = source->bytespp; xindex > 0; xindex--)
780  *pixel++ =
781  (uinT8) ((div2 + (*sums++ << shift)) / (yindex * xfactor));
782  //last one different
783  }
784  //put in destination
785  dest->put_line (xdest, ydest, destext, &copyline, 0);
786  }
787  delete [] linesums;
788 }
Definition: img.h:325
void check_legal_access(inT32 x, inT32 y, inT32 xext)
Definition: imgs.cpp:1484
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
void set_bpp(inT8 new_bpp)
Definition: img.h:362
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
uinT8 pixel(inT32 x, inT32 y)
Definition: imgs.cpp:202
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
uinT8 * pixels
image pixels
Definition: img.h:328
SIGNED char inT8
Definition: host.h:98
unsigned char uinT8
Definition: host.h:99
void fast_get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
Definition: imgs.cpp:978

The documentation for this class was generated from the following files: