ByteArray.st
changeset 1456 a544a722ec32
parent 1384 ab9536f4f998
child 1457 fe970e9d2d6c
equal deleted inserted replaced
1455:970b79688f56 1456:a544a722ec32
  1608      outBits inspect
  1608      outBits inspect
  1609     "
  1609     "
  1610 !
  1610 !
  1611 
  1611 
  1612 expandPixels:nBitsPerPixel width:width height:height into:aByteArray
  1612 expandPixels:nBitsPerPixel width:width height:height into:aByteArray
  1613 			 mapping:aMapByteArray
  1613                          mapping:aMapByteArray
  1614 
  1614 
  1615     "given the receiver with nBitsPerPixel-depth pixels, expand them into
  1615     "given the receiver with nBitsPerPixel-depth pixels, expand them into
  1616      aByteArray with 8-bit pixels. The width/height-arguments are needed
  1616      aByteArray with 8-bit pixels. The width/height-arguments are needed
  1617      to skip any padded src-bits. On the fly, the destination pixels
  1617      to skip any padded src-bits. On the fly, the destination pixels
  1618      are translated using aMapByteArray (if non-nil).
  1618      are translated using aMapByteArray (if non-nil).
  1641 
  1641 
  1642     if ((__qClass(self) == @global(ByteArray)) 
  1642     if ((__qClass(self) == @global(ByteArray)) 
  1643      && (__qClass(aByteArray) == @global(ByteArray))
  1643      && (__qClass(aByteArray) == @global(ByteArray))
  1644      && __isSmallInteger(nBitsPerPixel)
  1644      && __isSmallInteger(nBitsPerPixel)
  1645      && __bothSmallInteger(height, width)) {
  1645      && __bothSmallInteger(height, width)) {
  1646 	if ((aMapByteArray != nil)
  1646         if ((aMapByteArray != nil)
  1647 	 && (__Class(aMapByteArray) == @global(ByteArray))) {
  1647          && (__Class(aMapByteArray) == @global(ByteArray))) {
  1648 	    map = __ByteArrayInstPtr(aMapByteArray)->ba_element;
  1648             map = __ByteArrayInstPtr(aMapByteArray)->ba_element;
  1649 	} else {
  1649         } else {
  1650 	    map = (unsigned char *)0;
  1650             map = (unsigned char *)0;
  1651 	}
  1651         }
  1652 
  1652 
  1653 	bitsPerPixel = __intVal(nBitsPerPixel);
  1653         bitsPerPixel = __intVal(nBitsPerPixel);
  1654 	w = __intVal(width);
  1654         w = __intVal(width);
  1655 	h = __intVal(height);
  1655         h = __intVal(height);
  1656 	src = __ByteArrayInstPtr(self)->ba_element;
  1656         src = __ByteArrayInstPtr(self)->ba_element;
  1657 	dst = __ByteArrayInstPtr(aByteArray)->ba_element;
  1657         dst = __ByteArrayInstPtr(aByteArray)->ba_element;
  1658 	switch (bitsPerPixel) {
  1658         switch (bitsPerPixel) {
  1659 	    case 1:
  1659             case 1:
  1660 		mask = 0x01;
  1660                 mask = 0x01;
  1661 		break;
  1661                 break;
  1662 	    case 2:
  1662             case 2:
  1663 		mask = 0x03;
  1663                 mask = 0x03;
  1664 		break;
  1664                 break;
  1665 	    case 4:
  1665             case 4:
  1666 		mask = 0x0F;
  1666                 mask = 0x0F;
  1667 		break;
  1667                 break;
  1668 	    case 8:
  1668             case 8:
  1669 		mask = 0xFF;
  1669                 mask = 0xFF;
  1670 		break;
  1670                 break;
  1671 	    default:
  1671             default:
  1672 		goto fail;
  1672                 fprintf(stderr, "invalid bitsPerPixel\n");
  1673 	}
  1673                 goto fail;
  1674 	ncells = mask + 1;
  1674         }
  1675 	if (map) {
  1675         ncells = mask + 1;
  1676 	    /*
  1676         if (map) {
  1677 	     * if a map is present, it must have the correct size
  1677             /*
  1678 	     * (i.e. 2 raisedTo:nBitsPerPixel)
  1678              * if a map is present, it must have the correct size
  1679 	     */
  1679              * (i.e. 2 raisedTo:nBitsPerPixel)
  1680 	    if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells)
  1680              */
  1681 		goto fail;
  1681             if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) {
  1682 	}
  1682                 fprintf(stderr, "mapArray size\n");
  1683 
  1683                 goto fail;
  1684 	bytesPerRow = (w * bitsPerPixel + 7) / 8;
  1684             }
  1685 	shift0 = 8 - bitsPerPixel;
  1685         }
  1686 	srcBytes = bytesPerRow * h;
  1686 
  1687 	dstBytes = w * h;
  1687         bytesPerRow = (w * bitsPerPixel + 7) / 8;
  1688 
  1688         shift0 = 8 - bitsPerPixel;
  1689 	if ((__byteArraySize(self) >= srcBytes)
  1689         srcBytes = bytesPerRow * h;
  1690 	 && (__byteArraySize(aByteArray) >= dstBytes)) {
  1690         dstBytes = w * h;
  1691 	    for (hrun=h; hrun; hrun--) {
  1691 
  1692 		srcNext = src + bytesPerRow;
  1692         if ((__byteArraySize(self) >= srcBytes)
  1693 		shift = shift0;
  1693          && (__byteArraySize(aByteArray) >= dstBytes)) {
  1694 		if (map) {
  1694             for (hrun=h; hrun; hrun--) {
  1695 		    for (wrun=w; wrun; wrun--) {
  1695                 srcNext = src + bytesPerRow;
  1696 			if (shift == shift0) {
  1696                 shift = shift0;
  1697 			    bits = *src++;
  1697                 if (map) {
  1698 			}
  1698                     for (wrun=w; wrun; wrun--) {
  1699 			*dst++ = map[(bits >> shift) & mask];
  1699                         if (shift == shift0) {
  1700 			shift -= bitsPerPixel;
  1700                             bits = *src++;
  1701 			if (shift < 0) {
  1701                         }
  1702 			    shift = shift0;
  1702                         *dst++ = map[(bits >> shift) & mask];
  1703 			}
  1703                         shift -= bitsPerPixel;
  1704 		    }
  1704                         if (shift < 0) {
  1705 		} else {
  1705                             shift = shift0;
  1706 		    for (wrun=w; wrun; wrun--) {
  1706                         }
  1707 			if (shift == shift0) {
  1707                     }
  1708 			    bits = *src++;
  1708                 } else {
  1709 			}
  1709                     for (wrun=w; wrun; wrun--) {
  1710 			*dst++ = (bits >> shift) & mask;
  1710                         if (shift == shift0) {
  1711 			shift -= bitsPerPixel;
  1711                             bits = *src++;
  1712 			if (shift < 0) {
  1712                         }
  1713 			    shift = shift0;
  1713                         *dst++ = (bits >> shift) & mask;
  1714 			}
  1714                         shift -= bitsPerPixel;
  1715 		    }
  1715                         if (shift < 0) {
  1716 		}
  1716                             shift = shift0;
  1717 		src = srcNext;
  1717                         }
  1718 	    }
  1718                     }
  1719 	    RETURN ( self );
  1719                 }
  1720 	}
  1720                 src = srcNext;
       
  1721             }
       
  1722             RETURN ( self );
       
  1723         }
       
  1724         fprintf(stderr, "not enough bytes\n");
  1721     }
  1725     }
  1722 fail: ;
  1726 fail: ;
  1723 %}
  1727 %}
  1724 .
  1728 .
  1725     self primitiveFailed
  1729     self primitiveFailed
  1729     "
  1733     "
  1730     "
  1734     "
  1731      |inBits outBits|
  1735      |inBits outBits|
  1732 
  1736 
  1733      inBits := #[2r11110000 
  1737      inBits := #[2r11110000 
  1734 		 2r11001100 
  1738                  2r11001100 
  1735 		 2r01010101 
  1739                  2r01010101 
  1736 		 2r00001111].
  1740                  2r00001111].
  1737      outBits := ByteArray new:(8*4).
  1741      outBits := ByteArray new:(8*4).
  1738      inBits expandPixels:1 width:8 height:4
  1742      inBits expandPixels:1 width:8 height:4
  1739 		    into:outBits mapping:nil.
  1743                     into:outBits mapping:nil.
  1740      outBits inspect
  1744      outBits inspect
  1741     "
  1745     "
  1742 
  1746 
  1743     "Example2:
  1747     "Example2:
  1744      expand bit-array into a byteArray, translating 0-bits to 99,
  1748      expand bit-array into a byteArray, translating 0-bits to 99,
  1748      |inBits outBits|
  1752      |inBits outBits|
  1749 
  1753 
  1750      inBits := #[2r11110000 2r11001100].
  1754      inBits := #[2r11110000 2r11001100].
  1751      outBits := ByteArray new:16.
  1755      outBits := ByteArray new:16.
  1752      inBits expandPixels:1 width:16 height:1 
  1756      inBits expandPixels:1 width:16 height:1 
  1753 		    into:outBits mapping:#[99 176].
  1757                     into:outBits mapping:#[99 176].
  1754      outBits inspect
  1758      outBits inspect
  1755     "
  1759     "
  1756 !
  1760 !
  1757 
  1761 
  1758 invert
  1762 invert
  2174 ! !
  2178 ! !
  2175 
  2179 
  2176 !ByteArray class methodsFor:'documentation'!
  2180 !ByteArray class methodsFor:'documentation'!
  2177 
  2181 
  2178 version
  2182 version
  2179     ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.61 1996-05-12 16:16:39 cg Exp $'
  2183     ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.62 1996-06-10 13:25:29 cg Exp $'
  2180 ! !
  2184 ! !