ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Low res output possible?
@ 2023-08-22  2:19 Benjamin Buchmuller
  2023-08-22  7:46 ` [NTG-context] " Henning Hraban Ramm
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Buchmuller @ 2023-08-22  2:19 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Julian,

I had the same question a couple of days ago and I found a thread on the mailing list, which I'm reposting here. Although this worked for me, I found downsampling of the pdf with gs more convenient.

Hope this helps,


Benjamin

%%%%%

if not modules then modules = { } end modules ['grph-downsample'] = {
  version   = 1.101,
  comment   = "companion to grph-inc.mkiv",
  author    = "Peter Münster", -- adapted to LMTX by Hraban
  copyright = "PRAGMA ADE / ConTeXt Development Team",
  license   = "see context related readme files"
}

assert(not figures.getinfo2)

figures.getinfo2 = function(name, page) -- [ [NTG-context] Pdf info with Lua/Ctx API ]
 if type(name) == "string" then
   name = { name = name, page = page }
 end

 if name.name then
   local data = figures.push(name)
   local info = figures.identify()

   if info.status.status ~= 0 then
     figures.check() -- !Counts pages here!
   end

   figures.pop()

   return --data
          info
 end
end


local format = string.format
-- figures.cachepaths.path = "cache" -- should be setup-option
local function sample_down(oldname, newname, resolution)
  print("DOWNSAMPLE sample_down " .. oldname .. " to " .. newname)
  local request = figures.current().request
  local width = request.width
  local height = request.height
  if resolution == "" then -- or (not width and not height) then
    print(format("DOWNSAMPLE Nothing to do: %s, %s, %s dpi, %s x %s px", 
oldname, newname, resolution, width, height))
    return
  end
  local TEXpt = 65536
  local inch = 72.27

  -- MkIV:
  -- local image = img.scan{filename = oldname}

  -- LMTX:
  local image = figures.getinfo(oldname,1)
  image = image.status.private

  local xy = image.xsize / image.ysize
  if (not width and not height) then
    -- no size requested? use default width
    width = 300 * TEXpt
  end
  if not width then
    height = height / TEXpt
    width = height * xy
  end
  if not height then
    width = width / TEXpt
    height = width / xy
  end
  width = math.floor(width)
  height = math.floor(height)
  print(format("DOWNSAMPLE image size %dx%dpx. requested %dx%d?", image.xsize, 
image.ysize, width, height))
  local xsize = math.floor(resolution * width / inch)
  local ysize = math.floor(resolution * height / inch)
  print(format("DOWNSAMPLE size %d x %d to %d x %d", image.xsize, image.ysize, 
xsize, ysize))
  if xsize < image.xsize or ysize < image.ysize then
    local s = format("gm convert -resize %dx%d -resample %dx%d \"%s\" \"%s\"",
                     xsize, ysize, resolution, resolution, oldname, newname)
    print("DOWNSAMPLE Conversion: " .. s)
    os.execute(s)
  else
    print(format("DOWNSAMPLE Nothing to do: %s, %s, %s dpi, %d x %d px", 
oldname, newname, resolution, width, height))
    print(format("DOWNSAMPLE xsize = %d, ysize = %d", xsize, ysize))
  end
end

local formats = {"png", "jpg", "gif"}

for _, s in ipairs(formats) do
  figures.converters[s] = figures.converters[s] or {}
  figures.converters[s]["lowres." .. s] = sample_down
end

local function sample_down_pdf(oldname, newname, resolution)
	print("DOWNSAMPLE sample_down " .. oldname .. " to " .. newname)
	
    -- MkIV:
    -- local image = img.scan{filename = oldname}

    -- LMTX:
    local image = figures.getinfo(oldname,1)
    image = image.status.private

    local s = format("gs \
-o \"%s\" \
-sDEVICE=pdfwrite \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=%s \
-dGrayImageResolution=%s \
-dMonoImageResolution=%s \
-dColorImageDownsampleThreshold=1.0 \
-dGrayImageDownsampleThreshold=1.0 \
-dMonoImageDownsampleThreshold=1.0 \
\"%s\"",
		oldname, resolution, resolution, resolution, newname)
					   print("DOWNSAMPLE Conversion: " .. s)
	  os.execute(s)
end

figures.converters["pdf"]["lowres." .. "pdf"] = sample_down_pdf

%%%%%

\loadluafile[grph-downsample]
\doifmodeelse{print}{%
        \def\Resolution{300}
}{%
        \def\Resolution{96}
}%
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Low res output possible?
  2023-08-22  2:19 [NTG-context] Low res output possible? Benjamin Buchmuller
@ 2023-08-22  7:46 ` Henning Hraban Ramm
  0 siblings, 0 replies; 3+ messages in thread
From: Henning Hraban Ramm @ 2023-08-22  7:46 UTC (permalink / raw)
  To: ntg-context

Am 22.08.23 um 04:19 schrieb Benjamin Buchmuller:
> I had the same question a couple of days ago and I found a thread on the mailing list, which I'm reposting here. Although this worked for me, I found downsampling of the pdf with gs more convenient.

Here’s a shorter solution:

\startluacode
local function downsampler(oldname, newname, resolution)
     if not resolution or resolution == "" then
         resolution = 72
     end
     local cmd = string.format(
       [[gm convert -resample %ix%i %s %s]],
       resolution, resolution, oldname, newname)
     -- print(cmd)
     os.execute(cmd)
end

-- Set the PDF and default JPEG converters to the above function.
figures.converters.jpg.pdf = downsampler
figures.converters.jpg.default = downsampler
\stopluacode

You can set the resolution in \setupexternalfigure this way.
But the calculation doesn’t take scaling into account, I have this on my 
todo list for too long…
And it should be possible to hook this into "conversion"…

Hraban
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Low res output possible?
@ 2023-08-21 22:49 jbf
  0 siblings, 0 replies; 3+ messages in thread
From: jbf @ 2023-08-21 22:49 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Just wondering: in programs like InDesign, Viva Designer, it is trivial 
to produce a low resolution pdf output (useful when exchanging a hi-res 
file with authors and editors prior to pre-print is difficult). I assume 
it might be possible to do so in ConTeXt. Currently I use a script (gs) 
to achieve that once I have the ConTeXt output so it is no difficulty, 
but if that could be done at time of compiling it would make the process 
just that bit simpler. Possible?

Julian

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-22  7:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-22  2:19 [NTG-context] Low res output possible? Benjamin Buchmuller
2023-08-22  7:46 ` [NTG-context] " Henning Hraban Ramm
  -- strict thread matches above, loose matches on Subject: below --
2023-08-21 22:49 [NTG-context] " jbf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).