tiffsep1_prn_close(gx_device * pdev)
{
    tiffsep1_device * const tfdev = (tiffsep1_device *) pdev;
    int num_dev_comp = tfdev->color_info.num_components;
    int num_std_colorants = tfdev->devn_params.num_std_colorant_names;
    int num_order = tfdev->devn_params.num_separation_order_names;
    int num_spot = tfdev->devn_params.separations.num_separations;
    char *name= NULL;
    int code = gdev_prn_close(pdev);
    short map_comp_to_sep[GX_DEVICE_COLOR_MAX_COMPONENTS];
    int comp_num;
    int num_comp = number_output_separations(num_dev_comp, num_std_colorants,
                                        num_order, num_spot);
    const char *fmt;
    gs_parsed_file_name_t parsed;

    if (code < 0)
        return code;

    name = (char *)gs_alloc_bytes(pdev->memory, gp_file_name_sizeof, "tiffsep1_prn_close(name)");
    if (!name)
        return_error(gs_error_VMerror);

    code = gx_parse_output_file_name(&parsed, &fmt, tfdev->fname,
                                     strlen(tfdev->fname), pdev->memory);
    if (code < 0) {
        goto done;
    }

    /* If we are doing separate pages, delete the old default file */
    if (parsed.iodev == iodev_default(pdev->memory)) {          /* filename includes "%nnd" */
        char *compname = (char *)gs_alloc_bytes(pdev->memory, gp_file_name_sizeof, "tiffsep1_prn_close(compname)");
        if (!compname) {
            code = gs_note_error(gs_error_VMerror);
            goto done;
        }

        if (fmt) {
            long count1 = pdev->PageCount;

            while (*fmt != 'l' && *fmt != '%')
                --fmt;
            if (*fmt == 'l')
                gs_sprintf(compname, parsed.fname, count1);
            else
                gs_sprintf(compname, parsed.fname, (int)count1);
            parsed.iodev->procs.delete_file(parsed.iodev, compname);
        } else {
            parsed.iodev->procs.delete_file(parsed.iodev, tfdev->fname);
        }
        gs_free_object(pdev->memory, compname, "tiffsep1_prn_close(compname)");
    }

    build_comp_to_sep_map((tiffsep_device *)tfdev, map_comp_to_sep);
    /* Close the separation files */
    for (comp_num = 0; comp_num < num_comp; comp_num++ ) {
        if (tfdev->sep_file[comp_num] != NULL) {
            int sep_num = map_comp_to_sep[comp_num];

            code = create_separation_file_name((tiffsep_device *)tfdev, name,
                                                gp_file_name_sizeof, sep_num, true);
            if (code < 0) {
                goto done;
            }
            code = gx_device_close_output_file(pdev, name, tfdev->sep_file[comp_num]);
            if (code >= 0)
                code = gs_remove_outputfile_control_path(pdev->memory, name);
            if (code < 0) {
                goto done;
            }
            tfdev->sep_file[comp_num] = NULL;
        }
        if (tfdev->tiff[comp_num]) {
            TIFFCleanup(tfdev->tiff[comp_num]);
            tfdev->tiff[comp_num] = NULL;
        }
    }
    /* If we have thresholds, free them and clear the pointers */
    if( tfdev->thresholds[0].dstart != NULL) {
        sep1_free_thresholds(tfdev);
    }

done:

    if (name)
        gs_free_object(pdev->memory, name, "tiffsep1_prn_close(name)");
    return code;
}