code: def extract_image_jbig2(
      *, pdf: Pdf, root: Path, image: Stream, xref: Xref, options
  ) -> XrefExt | None:
      """Extract an image, saving it as a JBIG2 file."""
      del options  # unused arg
  
      result = extract_image_filter(image, xref)
      if result is None:
          return None
      pim, filtdp = result
  
      if (
          pim.bits_per_component == 1
          and filtdp[0] != Name.JBIG2Decode
          and jbig2enc.available()
      ):
          # Save any colorspace associated with the image, so that we
          # will export a pure 1-bit PNG with no palette or ICC profile.
          # Showing the palette or ICC to jbig2enc will cause it to perform
          # colorspace transform to 1bpp, which will conflict the palette or
          # ICC if it exists.
          colorspace = pim.obj.get(Name.ColorSpace, None)
          if colorspace is not None or pim.image_mask:
              try:
                  # Set to DeviceGray temporarily; we already in 1 bpc.
                  pim.obj.ColorSpace = Name.DeviceGray
                  imgname = root / f"{xref:08d}"
                  with imgname.open("wb") as f:
                      ext = pim.extract_to(stream=f)
                  imgname.rename(imgname.with_suffix(ext))
              except UnsupportedImageTypeError:
                  return None
              finally:
                  # Restore image colorspace after temporarily setting it to DeviceGray
                  if colorspace is not None:
                      pim.obj.ColorSpace = colorspace
                  else:
                      del pim.obj.ColorSpace
              return XrefExt(xref, ext)
      return None
  
imports: from __future__ import annotations
  import logging
  import sys
  import tempfile
  import threading
  from collections import defaultdict
  from collections.abc import Iterator, MutableSet, Sequence
  from os import fspath
  from pathlib import Path
  from typing import Any, Callable, NamedTuple, NewType
  from warnings import warn
  from zlib import compress
  import img2pdf
  from pikepdf import Dictionary, Name, Object, ObjectStreamMode, Pdf, PdfError, PdfImage, Stream, UnsupportedImageTypeError
  from PIL import Image
  from ocrmypdf._concurrent import Executor, SerialExecutor
  from ocrmypdf._exec import jbig2enc, pngquant
  from ocrmypdf._jobcontext import PdfContext
  from ocrmypdf._progressbar import ProgressBar
  from ocrmypdf.exceptions import OutputFileAccessError
  from ocrmypdf.helpers import IMG2PDF_KWARGS, safe_symlink
label_desc: Use image node_id 5 for calling a global function with example usage: extract_image_jbig2() and returns: None, None, XrefExt, None