public void replaceColorTable(ColorTable newTable) throws AWTException {
        boolean[] changedPixels = new boolean[pix.length];
        for (BmpColor color : colorTable) {
            int indexInNewTable = newTable.getIndex(color);
            int indexInThisTable = colorTable.getIndex(color);
            if (indexInNewTable != -1) {
                if (indexInThisTable != indexInNewTable) {
                    for (int i = 0; i < pix.length; i++) {
                        if (!changedPixels[i] && pix[i] == indexInThisTable) {
                            pix[i] = indexInNewTable;
                            changedPixels[i] = true;
                        }
                    }
                }
            }
            else {
                throw new AWTException("Colors in new table do not match the old");
            }
        }
        
        if (colorTable.getNumColors() != newTable.getNumColors()) {
            int diffNumColors = newTable.getNumColors() - colorTable.getNumColors();
            bfSize += diffNumColors * 4;
            bfOffset += diffNumColors * 4;
            biClrUsed += diffNumColors;
        }
            colorTable = newTable;
    }