void generateRandomFile (int channelCount,
                         Compression compression,
                         bool bulkWrite,
                         bool relativeCoords,
                         const std::string & filename)
{
    if (relativeCoords)
        assert(bulkWrite == false);

    cout << "generating " << flush;
    header = Header(displayWindow, dataWindow,
                    1,
                    IMATH_NAMESPACE::V2f (0, 0),
                    1,
                    INCREASING_Y,
                    compression);
    cout << "compression " << compression << " " << flush;

    //
    // Add channels.
    //

    channelTypes.clear();

    for (int i = 0; i < channelCount; i++)
    {
        int type = random_int(3);
        stringstream ss;
        ss << i;
        string str = ss.str();
        if (type == 0)
            header.channels().insert(str, Channel(IMF::UINT));
        if (type == 1)
            header.channels().insert(str, Channel(IMF::HALF));
        if (type == 2)
            header.channels().insert(str, Channel(IMF::FLOAT));
        channelTypes.push_back(type);
    }

    header.setType(DEEPTILE);
    header.setTileDescription(
        TileDescription(random_int(width) + 1, random_int(height) + 1, RIPMAP_LEVELS));


    //
    // Set up the output file
    //
    remove (filename.c_str());
    DeepTiledOutputFile file(filename.c_str(), header, 8);

    DeepFrameBuffer frameBuffer;

    Array<Array2D< void* > > data(channelCount);
    for (int i = 0; i < channelCount; i++)
        data[i].resizeErase(height, width);

    Array2D<unsigned int> sampleCount;
    sampleCount.resizeErase(height, width);

    cout << " tileSizeX " << file.tileXSize()
         << " tileSizeY " << file.tileYSize() << " ";

    sampleCountWhole.resizeErase(file.numYLevels(), file.numXLevels());
    for (int i = 0; i < sampleCountWhole.height(); i++)
        for (int j = 0; j < sampleCountWhole.width(); j++)
            sampleCountWhole[i][j].resizeErase(height, width);

    int memOffset;
    if (relativeCoords)
        memOffset = 0;
    else
        memOffset = dataWindow.min.x + dataWindow.min.y * width;

    frameBuffer.insertSampleCountSlice (Slice (IMF::UINT,
                                        (char *) (&sampleCount[0][0] - memOffset),
                                        sizeof (unsigned int) * 1,
                                        sizeof (unsigned int) * width,
                                        1, 1,
                                        0,
                                        relativeCoords,
                                        relativeCoords));

    for (int i = 0; i < channelCount; i++)
    {
        PixelType type = NUM_PIXELTYPES;
        if (channelTypes[i] == 0)
            type = IMF::UINT;
        if (channelTypes[i] == 1)
            type = IMF::HALF;
        if (channelTypes[i] == 2)
            type = IMF::FLOAT;

        stringstream ss;
        ss << i;
        string str = ss.str();

        int sampleSize = 0;
        if (channelTypes[i] == 0) sampleSize = sizeof (unsigned int);
        if (channelTypes[i] == 1) sampleSize = sizeof (half);
        if (channelTypes[i] == 2) sampleSize = sizeof (float);

        int pointerSize = sizeof (char *);

        frameBuffer.insert (str,
                            DeepSlice (type,
                            (char *) (&data[i][0][0] - memOffset),
                            pointerSize * 1,
                            pointerSize * width,
                            sampleSize,
                            1, 1,
                            0,
                            relativeCoords,
                            relativeCoords));
    }

    file.setFrameBuffer(frameBuffer);

    cout << "writing " << flush;

    if (bulkWrite)
        cout << "bulk " << flush;
    else
    {
        if (relativeCoords == false)
            cout << "per-tile " << flush;
        else
            cout << "per-tile with relative coordinates " << flush;
    }

    for (int ly = 0; ly < file.numYLevels(); ly++)
        for (int lx = 0; lx < file.numXLevels(); lx++)
        {
            Box2i dataWindowL = file.dataWindowForLevel(lx, ly);

            if (bulkWrite)
            {
                //
                // Bulk write (without relative coordinates).
                //

                for (int j = 0; j < file.numYTiles(ly); j++)
                {
                    for (int i = 0; i < file.numXTiles(lx); i++)
                    {
                        Box2i box = file.dataWindowForTile(i, j, lx, ly);
                        for (int y = box.min.y; y <= box.max.y; y++)
                            for (int x = box.min.x; x <= box.max.x; x++)
                            {
                                int dwy = y - dataWindowL.min.y;
                                int dwx = x - dataWindowL.min.x;
                                sampleCount[dwy][dwx] = random_int(10) + 1;
                                sampleCountWhole[ly][lx][dwy][dwx] = sampleCount[dwy][dwx];
                                for (int k = 0; k < channelCount; k++)
                                {
                                    if (channelTypes[k] == 0)
                                        data[k][dwy][dwx] = new unsigned int[sampleCount[dwy][dwx]];
                                    if (channelTypes[k] == 1)
                                        data[k][dwy][dwx] = new half[sampleCount[dwy][dwx]];
                                    if (channelTypes[k] == 2)
                                        data[k][dwy][dwx] = new float[sampleCount[dwy][dwx]];
                                    for (unsigned int l = 0; l < sampleCount[dwy][dwx]; l++)
                                    {
                                        if (channelTypes[k] == 0)
                                            ((unsigned int*)data[k][dwy][dwx])[l] = (dwy * width + dwx) % 2049;
                                        if (channelTypes[k] == 1)
                                            ((half*)data[k][dwy][dwx])[l] = (dwy* width + dwx) % 2049;
                                        if (channelTypes[k] == 2)
                                            ((float*)data[k][dwy][dwx])[l] = (dwy * width + dwx) % 2049;
                                    }
                                }
                            }
                    }
                }

                file.writeTiles(0, file.numXTiles(lx) - 1, 0, file.numYTiles(ly) - 1, lx, ly);
            }
            else if (bulkWrite == false)
            {
                if (relativeCoords == false)
                {
                    //
                    // Per-tile write without relative coordinates.
                    //

                    for (int j = 0; j < file.numYTiles(ly); j++)
                    {
                        for (int i = 0; i < file.numXTiles(lx); i++)
                        {
                            Box2i box = file.dataWindowForTile(i, j, lx, ly);
                            for (int y = box.min.y; y <= box.max.y; y++)
                                for (int x = box.min.x; x <= box.max.x; x++)
                                {
                                    int dwy = y - dataWindowL.min.y;
                                    int dwx = x - dataWindowL.min.x;
                                    sampleCount[dwy][dwx] = random_int(10) + 1;
                                    sampleCountWhole[ly][lx][dwy][dwx] = sampleCount[dwy][dwx];
                                    for (int k = 0; k < channelCount; k++)
                                    {
                                        if (channelTypes[k] == 0)
                                            data[k][dwy][dwx] = new unsigned int[sampleCount[dwy][dwx]];
                                        if (channelTypes[k] == 1)
                                            data[k][dwy][dwx] = new half[sampleCount[dwy][dwx]];
                                        if (channelTypes[k] == 2)
                                            data[k][dwy][dwx] = new float[sampleCount[dwy][dwx]];
                                        for (unsigned int l = 0; l < sampleCount[dwy][dwx]; l++)
                                        {
                                            if (channelTypes[k] == 0)
                                                ((unsigned int*)data[k][dwy][dwx])[l] = (dwy * width + dwx) % 2049;
                                            if (channelTypes[k] == 1)
                                                ((half*)data[k][dwy][dwx])[l] = (dwy* width + dwx) % 2049;
                                            if (channelTypes[k] == 2)
                                                ((float*)data[k][dwy][dwx])[l] = (dwy * width + dwx) % 2049;
                                        }
                                    }
                                }
                            file.writeTile(i, j, lx, ly);
                        }
                    }
                }
                else if (relativeCoords)
                {
                    //
                    // Per-tile write with relative coordinates.
                    //

                    for (int j = 0; j < file.numYTiles(ly); j++)
                    {
                        for (int i = 0; i < file.numXTiles(lx); i++)
                        {
                            Box2i box = file.dataWindowForTile(i, j, lx, ly);
                            for (int y = box.min.y; y <= box.max.y; y++)
                                for (int x = box.min.x; x <= box.max.x; x++)
                                {
                                    int dwy = y - dataWindowL.min.y;
                                    int dwx = x - dataWindowL.min.x;
                                    int ty = y - box.min.y;
                                    int tx = x - box.min.x;
                                    sampleCount[ty][tx] = random_int(10) + 1;
                                    sampleCountWhole[ly][lx][dwy][dwx] = sampleCount[ty][tx];
                                    for (int k = 0; k < channelCount; k++)
                                    {
                                        if (channelTypes[k] == 0)
                                            data[k][ty][tx] = new unsigned int[sampleCount[ty][tx]];
                                        if (channelTypes[k] == 1)
                                            data[k][ty][tx] = new half[sampleCount[ty][tx]];
                                        if (channelTypes[k] == 2)
                                            data[k][ty][tx] = new float[sampleCount[ty][tx]];
                                        for (unsigned int l = 0; l < sampleCount[ty][tx]; l++)
                                        {
                                            if (channelTypes[k] == 0)
                                                ((unsigned int*)data[k][ty][tx])[l] =
                                                        (dwy * width + dwx) % 2049;
                                            if (channelTypes[k] == 1)
                                                ((half*)data[k][ty][tx])[l] =
                                                        (dwy * width + dwx) % 2049;
                                            if (channelTypes[k] == 2)
                                                ((float*)data[k][ty][tx])[l] =
                                                        (dwy * width + dwx) % 2049;
                                        }
                                    }
                                }
                            file.writeTile(i, j, lx, ly);

                            for (int y = box.min.y; y <= box.max.y; y++)
                                for (int x = box.min.x; x <= box.max.x; x++)
                                    for (int k = 0; k < channelCount; k++)
                                    {
                                        int ty = y - box.min.y;
                                        int tx = x - box.min.x;
                                        if (channelTypes[k] == 0)
                                            delete[] (unsigned int*) data[k][ty][tx];
                                        if (channelTypes[k] == 1)
                                            delete[] (half*) data[k][ty][tx];
                                        if (channelTypes[k] == 2)
                                            delete[] (float*) data[k][ty][tx];
                                    }
                        }
                    }
                }
            }

            if (relativeCoords == false)
            {
                for (int i = 0; i < file.levelHeight(ly); i++)
                    for (int j = 0; j < file.levelWidth(lx); j++)
                        for (int k = 0; k < channelCount; k++)
                        {
                            if (channelTypes[k] == 0)
                                delete[] (unsigned int*) data[k][i][j];
                            if (channelTypes[k] == 1)
                                delete[] (half*) data[k][i][j];
                            if (channelTypes[k] == 2)
                                delete[] (float*) data[k][i][j];
                        }
            }
        }
}