Skip to content

Commit

Permalink
jxl: fix indentation and add copyright / author
Browse files Browse the repository at this point in the history
  • Loading branch information
oupson authored and hpjansson committed Jan 23, 2024
1 parent fac1544 commit c523a81
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 138 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Expand Up @@ -7,7 +7,7 @@ git log. For a complete list, you could try:

git log --pretty="format:%an <%ae>" | sort -f | uniq

Per 2024-01-08, this yields the following (sans duplicates):
Per 2024-01-23, this yields the following (sans duplicates):

alkahest <cyphergothic@protonmail.com>
begasus <begasus@gmail.com>
Expand Down Expand Up @@ -35,3 +35,4 @@ Sudhakar Verma <svermaiiti@gmail.com>
Tim Gates <tim.gates@iress.com>
Wu Zhenyu <wuzhenyu@ustc.edu>
Øyvind Kolås <pippin@gimp.org>
oupson <oupson1er@gmail.com>
269 changes: 136 additions & 133 deletions tools/chafa/jxl-loader.c
@@ -1,5 +1,22 @@
/* -*- Mode: C; tab-width: 4; ent su: nil; c-basic-offset: 4 -*- */

/* Copyright (C) 2024 oupson
*
* This file is part of Chafa, a program that shows pictures on text terminals.
*
* Chafa is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chafa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Chafa. If not, see <http://www.gnu.org/licenses/>. */

#include "chafa.h"
#include "file-mapping.h"
#include "glib.h"
Expand All @@ -12,8 +29,7 @@

#define BUFFER_SIZE (1024)

GList *jxl_get_frames (JxlDecoder *dec, JxlParallelRunner *runner,
struct FileMapping *mapping);
GList *jxl_get_frames (JxlDecoder *dec, JxlParallelRunner *runner, FileMapping *mapping);

void jxl_cleanup_frame_list (GList *frame_list);

Expand Down Expand Up @@ -41,27 +57,25 @@ jxl_loader_new (void)
}

GList *
jxl_get_frames (JxlDecoder *dec, JxlParallelRunner *runner,
FileMapping *mapping)
jxl_get_frames (JxlDecoder *dec, JxlParallelRunner *runner, FileMapping *mapping)
{
if (JXL_DEC_SUCCESS
!= JxlDecoderSetParallelRunner (dec, JxlResizableParallelRunner,
runner))
{
return NULL;
}
!= JxlDecoderSetParallelRunner (dec, JxlResizableParallelRunner, runner))
{
return NULL;
}

if (JXL_DEC_SUCCESS
!= JxlDecoderSubscribeEvents (
dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE | JXL_DEC_FRAME))
{
return NULL;
}
!= JxlDecoderSubscribeEvents (dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE
| JXL_DEC_FRAME))
{
return NULL;
}

JxlPixelFormat format = { 4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0 };

JxlDecoderStatus decode_status = JXL_DEC_NEED_MORE_INPUT;
uint8_t buffer[BUFFER_SIZE];
uint8_t buffer [BUFFER_SIZE];

gpointer image_buffer = NULL;
GList *frame_list = NULL;
Expand All @@ -71,96 +85,89 @@ jxl_get_frames (JxlDecoder *dec, JxlParallelRunner *runner,
int read = 0;

while (JXL_DEC_ERROR != decode_status)
{
if (JXL_DEC_NEED_MORE_INPUT == decode_status)
{
if (JXL_DEC_NEED_MORE_INPUT == decode_status)
{
JxlDecoderReleaseInput (dec);
const gsize nbr = file_mapping_read (mapping, buffer, read,
BUFFER_SIZE);
read += nbr;
if (nbr > 0)
{
if (JXL_DEC_SUCCESS
!= JxlDecoderSetInput (dec, buffer, nbr))
{
break;
}
}
else
{
break;
}
}
else if (JXL_DEC_BASIC_INFO == decode_status)
{
if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo (dec, &info))
{
break;
}
if (!info.alpha_bits)
{
format.num_channels = 3;
}
image_buffer = g_malloc (info.xsize * info.ysize
* format.num_channels);
}
else if (JXL_DEC_NEED_IMAGE_OUT_BUFFER == decode_status)
{
if (JXL_DEC_SUCCESS
!= JxlDecoderSetImageOutBuffer (
dec, &format, image_buffer,
info.xsize * info.ysize * format.num_channels))
{
break;
}
}
else if (JXL_DEC_FRAME == decode_status)
JxlDecoderReleaseInput (dec);
const gsize nbr = file_mapping_read (mapping, buffer, read, BUFFER_SIZE);
read += nbr;
if (nbr > 0)
{
if (JXL_DEC_SUCCESS != JxlDecoderSetInput (dec, buffer, nbr))
{
if (JXL_DEC_SUCCESS
!= JxlDecoderGetFrameHeader (dec, &frame_header))
{
break;
}
break;
}
else if (JXL_DEC_FULL_IMAGE == decode_status)
{
const uint32_t num = info.animation.tps_numerator == 0
? 1
: info.animation.tps_numerator;
JxlFrame *frame = g_new (JxlFrame, 1);
frame->buffer = image_buffer;
frame->width = info.xsize;
frame->height = info.ysize;
frame->have_alpha = format.num_channels == 4;
frame->is_premul = info.alpha_premultiplied;
frame->frame_duration = frame_header.duration * 1000
* info.animation.tps_denominator
/ num;
frame_list = g_list_prepend (frame_list, frame);

image_buffer = g_malloc (info.xsize * info.ysize
* format.num_channels);
}
else if (JXL_DEC_SUCCESS == decode_status)
{
frame_list = g_list_reverse (frame_list);
return frame_list;
}

decode_status = JxlDecoderProcessInput (dec);
}
else
{
break;
}
}
else if (JXL_DEC_BASIC_INFO == decode_status)
{
if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo (dec, &info))
{
break;
}
if (!info.alpha_bits)
{
format.num_channels = 3;
}
image_buffer = g_malloc (info.xsize * info.ysize * format.num_channels);
}
else if (JXL_DEC_NEED_IMAGE_OUT_BUFFER == decode_status)
{
if (JXL_DEC_SUCCESS
!= JxlDecoderSetImageOutBuffer (dec, &format, image_buffer,
info.xsize * info.ysize
* format.num_channels))
{
break;
}
}
else if (JXL_DEC_FRAME == decode_status)
{
if (JXL_DEC_SUCCESS != JxlDecoderGetFrameHeader (dec, &frame_header))
{
break;
}
}
else if (JXL_DEC_FULL_IMAGE == decode_status)
{
const uint32_t num
= info.animation.tps_numerator == 0 ? 1 : info.animation.tps_numerator;
JxlFrame *frame = g_new (JxlFrame, 1);
frame->buffer = image_buffer;
frame->width = info.xsize;
frame->height = info.ysize;
frame->have_alpha = format.num_channels == 4;
frame->is_premul = info.alpha_premultiplied;
frame->frame_duration
= frame_header.duration * 1000 * info.animation.tps_denominator / num;
frame_list = g_list_prepend (frame_list, frame);

image_buffer = g_malloc (info.xsize * info.ysize * format.num_channels);
}
else if (JXL_DEC_SUCCESS == decode_status)
{
frame_list = g_list_reverse (frame_list);
return frame_list;
}

decode_status = JxlDecoderProcessInput (dec);
}

// Decoding failed
if (image_buffer != NULL)
{
g_free (image_buffer);
}
{
g_free (image_buffer);
}

if (frame_list != NULL)
{
jxl_cleanup_frame_list (frame_list);
g_list_free (frame_list);
}
{
jxl_cleanup_frame_list (frame_list);
g_list_free (frame_list);
}

return NULL;
}
Expand All @@ -175,11 +182,10 @@ jxl_loader_new_from_mapping (FileMapping *mapping)

if (!file_mapping_has_magic (mapping, 0, "\xFF\x0A", 2)
&& !file_mapping_has_magic (
mapping, 0, "\x00\x00\x00\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A",
12))
{
return NULL;
}
mapping, 0, "\x00\x00\x00\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12))
{
return NULL;
}

loader = jxl_loader_new ();

Expand All @@ -194,19 +200,19 @@ jxl_loader_new_from_mapping (FileMapping *mapping)
success = frames != NULL;

if (success)
{
loader->frames = frames;
loader->frame_count = g_list_length (frames);
loader->index = 0;
}
{
loader->frames = frames;
loader->frame_count = g_list_length (frames);
loader->index = 0;
}
else
{
if (loader)
{
if (loader)
{
g_free (loader);
loader = NULL;
}
g_free (loader);
loader = NULL;
}
}

return loader;
};
Expand All @@ -217,16 +223,16 @@ jxl_cleanup_frame_list (GList *frame_list)
GList *frame = frame_list;

while (frame != NULL)
{
if (frame->data != NULL)
{
if (frame->data != NULL)
{
JxlFrame *jxl_frame = frame->data;
g_free (jxl_frame->buffer);
g_free (jxl_frame);
frame->data = NULL;
}
frame = frame->next;
JxlFrame *jxl_frame = frame->data;
g_free (jxl_frame->buffer);
g_free (jxl_frame);
frame->data = NULL;
}
frame = frame->next;
}
}

void
Expand All @@ -246,25 +252,23 @@ jxl_loader_get_is_animation (JxlLoader *loader)

gconstpointer
jxl_loader_get_frame_data (JxlLoader *loader, ChafaPixelType *pixel_type_out,
gint *width_out, gint *height_out,
gint *rowstride_out)
gint *width_out, gint *height_out, gint *rowstride_out)
{
g_return_val_if_fail (loader != NULL, NULL);
const JxlFrame *frame = g_list_nth_data (loader->frames, loader->index);

int num_channels;
if (frame->have_alpha)
{
num_channels = 4;
*pixel_type_out = frame->is_premul
? CHAFA_PIXEL_RGBA8_PREMULTIPLIED
: CHAFA_PIXEL_RGBA8_UNASSOCIATED;
}
{
num_channels = 4;
*pixel_type_out = frame->is_premul ? CHAFA_PIXEL_RGBA8_PREMULTIPLIED :
CHAFA_PIXEL_RGBA8_UNASSOCIATED;
}
else
{
num_channels = 3;
*pixel_type_out = CHAFA_PIXEL_RGB8;
}
{
num_channels = 3;
*pixel_type_out = CHAFA_PIXEL_RGB8;
}

*width_out = frame->width;
*height_out = frame->height;
Expand All @@ -277,8 +281,7 @@ gint
jxl_loader_get_frame_delay (JxlLoader *loader)
{
g_return_val_if_fail (loader != NULL, 0);
return ((JxlFrame *)g_list_nth_data (loader->frames, loader->index))
->frame_duration;
return ((JxlFrame *) g_list_nth_data (loader->frames, loader->index))->frame_duration;
}

void
Expand Down
7 changes: 3 additions & 4 deletions tools/chafa/jxl-loader.h
@@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Copyright (C) 2018-2023 Hans Petter Jansson
/* Copyright (C) 2024 oupson
*
* This file is part of Chafa, a program that shows pictures on text terminals.
*
Expand Down Expand Up @@ -33,9 +33,8 @@ void jxl_loader_destroy (JxlLoader *loader);
gboolean jxl_loader_get_is_animation (JxlLoader *loader);

gconstpointer jxl_loader_get_frame_data (JxlLoader *loader,
ChafaPixelType *pixel_type_out,
gint *width_out, gint *height_out,
gint *rowstride_out);
ChafaPixelType *pixel_type_out, gint *width_out,
gint *height_out, gint *rowstride_out);
gint jxl_loader_get_frame_delay (JxlLoader *loader);

void jxl_loader_goto_first_frame (JxlLoader *loader);
Expand Down

0 comments on commit c523a81

Please sign in to comment.