ERL_NIF_TERM _hh_to_binary_uncompressed(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    hh_ctx_t* ctx = NULL;
    ErlNifBinary target;

    ErlNifResourceType* ctx_type = get_hh_ctx_type(env);
    if (argc != 1 ||
        ctx_type == NULL ||
        !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx))
    {
        return enif_make_badarg(env);
    }
    int size = 0;
    uint8_t* data = NULL;
    int success = hdr_encode_uncompressed(ctx->data, &data, &size);

    if (!enif_alloc_binary(size, &target))
    {
        return make_error(env, "bad_hdr_binary_alloc");
    }
    target.size = size;
    memcpy(target.data,data,size);
    free(data);

    if (success != 0)
    {
        return make_error(env, "bad_hdr_binary");
    }

    return enif_make_binary(env, &target);
}
ERL_NIF_TERM _hh_rotate(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    hh_ctx_t* ctx = NULL;
    hh_ctx_t* to = NULL;
    ErlNifBinary target;

    ErlNifResourceType* ctx_type = get_hh_ctx_type(env);
    if (argc != 2 ||
        ctx_type == NULL ||
        !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx) ||
        ctx->data == NULL ||
        !enif_get_resource(env, argv[1], ctx_type, (void **)&to) ||
        to->data == NULL)
    {
        return enif_make_badarg(env);
    }

    hdr_histogram_t* diff_histogram;
    int rc = 0;
    rc = hdr_alloc(ctx->highest_trackable_value, ctx->significant_figures, &diff_histogram);

    if (ENOMEM == rc)
    {
        return make_error(env, "not_enough_memory");
    }

    hdr_rotate(ctx->data, to->data, diff_histogram);

    int size = 0;
    uint8_t* data = NULL;
    int success = hdr_encode_uncompressed(diff_histogram, &data, &size);

    if (!enif_alloc_binary(size, &target))
    {
        return make_error(env, "bad_hdr_binary_alloc");
    }
    target.size = size;
    memcpy(target.data, data, size);
    free(data);
    free(diff_histogram);

    if (success != 0)
    {
        return make_error(env, "bad_hdr_binary");
    }

    return enif_make_binary(env, &target);
}